-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
101 lines (93 loc) · 3.47 KB
/
helper.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @param entries { [[ String, String ]] } array of arrays, each containing a key-value pair: [["key", "value"],..]
*
* return { Object } with key: value pairs
*/
function entriesToObject(entries, skipFirst = 0) {
// If skipFirst is 1, skip first element of the array, because it is the heading of the list
if (skipFirst < 0 || skipFirst > 1 || skipFirst !== Math.floor(skipFirst)) {
throw new Error('Second argument should be either 0 or 1');
}
return entries.slice(skipFirst).reduce(function(acc, next) {
acc[next[0]] = next[1];
return acc;
}, {});
}
function getArrayDepth(value) {
return Array.isArray(value) ? 1 + Math.max(...value.map(getArrayDepth)) : 0;
}
function getIndexOfLongestArray(array2d) {
return array2d.reduce((acc, values, index) => {
return array2d[acc].length >= values.length ? acc : index;
}, 0);
}
/**
*
* @param {{headings, table}} Object headings is a N array, table is an N array of M arrays (an N:M matrix). 1..N x N..M should return a an array of length M
* return array of length M with elements {key1: value1, ..., keyN, valueN}
*/
function tabularDataToObjects({ headings, table }) {
if (!Array.isArray(headings) || !Array.isArray(table)) {
throw new Error('Function tabularDataToObjects: headings and table arguments should be arrays');
}
if (headings.length !== table.length) {
throw new Error(
'Function tabularDataToObjects: headings and table arguments should have the same length'
);
}
if (getArrayDepth(table) !== 2) {
throw new Error('Function tabularDataToObjects: table argument is expected to be of depth 2.');
}
return table[getIndexOfLongestArray(table)].reduce((items, _, valuesIndex) => {
const itemData = headings.reduce((obj, heading, headingIndex) => {
const value = table[headingIndex][valuesIndex];
if (typeof value === 'undefined') {
return obj;
}
Object.assign(obj, { [heading]: table[headingIndex][valuesIndex] });
return obj;
}, {});
items.push(itemData);
return items;
}, []);
}
/**
*
* @param {Array[val]} filteree Array with values to filter
* @param {Array[val]} filterer Array with truthy / falsy values which determine which values to filter based on index
* @return {Array[val]} Sub array of filteree
* e.g.
* filteree [1, 2, 3, 4]
* filterer ['', 'truthy', 1, false]
* Returns [ 2, 3 ]
*/
function filterArrayByFiltererArray(filteree, filterer) {
if (!Array.isArray(filteree) || !Array.isArray(filterer)) {
throw new Error('Function filterArrayByFiltererArray: Arguments should be two arrays');
}
if (filteree.length !== filterer.length) {
throw new Error('Function filterArrayByFiltererArray: Arguments should have the same length');
}
return filteree.filter((value, index) => filterer[index]);
}
function mapArrayPairsToObject(keysArray, valuesArray) {
if (!Array.isArray(keysArray) || !Array.isArray(valuesArray)) {
throw new Error('Function mapArrayPairsToObject: Arguments should be two arrays');
}
if (keysArray.length !== valuesArray.length) {
throw new Error('Function mapArrayPairsToObject: Arguments should have the same length');
}
return keysArray.reduce((acc, key, index) => {
Object.assign(acc, { [key]: valuesArray[index] });
return acc;
}, {});
}
// Comment when running Google Apps Script
// module.exports = {
// entriesToObject,
// tabularDataToObjects,
// getIndexOfLongestArray,
// getArrayDepth,
// filterArrayByFiltererArray,
// mapArrayPairsToObject
// };