-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
36 lines (35 loc) · 891 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const extractKeys = array => {
if (array.length === 0) {
return [];
}
let keys = Object.keys(array[0]);
let isCopy = array.every(element => {
if (typeof element !== 'object') {
throw new Error(`Array must be made entirely of objects`);
}
return Object.keys(element).every(key => keys.indexOf(key) !== -1);
})
if (isCopy) {
return keys;
} else {
throw new Error(`Array's object's keys do not match`);
}
}
const printQueryResults = array => {
if (typeof array !== 'object') {
console.log(array);
return array;
}
if (!Array.isArray(array)) {
array = [array]
}
let keys = extractKeys(array);
let output = [];
output.push(`\t${keys.join('\t')}`);
array.forEach(row => {
output.push(`\t${Object.keys(row).map(key => row[key]).join('\t')}`);
});
output = output.join('\n');
console.log(output);
return output;
}