-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.js
158 lines (139 loc) · 5.62 KB
/
lib.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var isPlainObj = require('./3thparty/isplainobj');
var id = function id(argument) { return argument; };
var toPairs = require('./3thparty/topairs');
var tag = require('./3thparty/buildtag');
var slug = require('./3thparty/slug');
var lib = module.exports = {
/**
* finds the tbody data and extracts it to an array if we were passed an object,
* and then iterates the the row data for links
*
* @param data mixed
* @return bool
*/
isDataCorrect: function (data) {
return data instanceof Array && (data.length === 0 || isPlainObj(data[0]));
},
isCellValCorrect: function (celldata) {
// we only accept strings, or numbers as arguments
return typeof celldata !== 'string' && typeof celldata !== 'number';
},
prismData: function (rows, headers, prisms) {
return rows.map(function (row) {
return headers.map(function (header) {
var headerTitle = header[1], columnName = header[0];
var cellValue = (prisms[columnName] || id)(row[columnName], row);
return [columnName, isPlainObj(cellValue) ? cellValue : {
presentation: cellValue,
raw: row[columnName]
}];
});
});
},
buildBody: function (rowsGroups, groupValueFn, totalsValueFn) {
return tag('tbody', {}, rowsGroups.map(function (group) {
var rows = group[1], groupTitle = group[0];
if (!rows) return '';
return lib.buildGroupTitle(rows, groupTitle, groupValueFn, totalsValueFn) + lib.buildRows(rows);
})
.join('\n'));
},
buildGroupTitle: function buildGroupTitle(rows, groupTitle, groupValueFn, totalsValueFn) {
if (!groupTitle) return '';
var groupTitleValue = groupValueFn(groupTitle, rows.length, lib.getTotals(rows[0], rows, totalsValueFn).value());
var groupTitleCell = tag('td', { class: 'group-name-td', colspan: String(rows[0].length) }, groupTitleValue);
return tag('tr', {}, groupTitleCell);
},
buildRows: function buildRows(rows) {
return rows.map(function (row) {
return row.map(function (cell) {
var cellValue = cell[1], colName = cell[0];
var className = slug(colName) + '-td ' + (isNaN(+(cellValue.raw)) ? 'td_text' : 'td_num');
return tag('td', {'class': className}, cellValue.presentation);
})
.join('');
})
.map(function (tr, i) {
var trId = rows[i][0][0] === '__vert_header__' && rows[i][0][1];
var trAttrs = trId ? {'class': slug(trId.raw) + '-tr', 'data-id': slug(trId.raw)} : {};
return tag('tr', trAttrs, tr);
})
.join('\n');
},
/**
* takes an array of and produces <thead><tr><th> ... </th></tr></thead> with one th
* for each item of the array
*
* @param {Object|String[][]} headers - {k:v,,,} or [[k,v],,,]
*/
buildHeaders: function (headers) {
var content = headers.map(function (header) {
var headerContent = header[1], headerKey = header[0];
return tag('th', {'class': slug(headerKey) + '-th'}, headerContent);
});
return '<thead><tr>' + content.join('') + '</tr></thead>';
},
buildFooter: function (headers, rows, totalsFn) {
var content = lib.getTotals(headers, rows, totalsFn).map(function (tdValue) {
return tag('td', {}, tdValue);
});
// check if there is a totals function, only return footer if required
if (Object.keys(totalsFn) < 1) {
return '';
} else {
return '<tfoot><tr>' + content.join('') + '</tr></tfoot>';
}
},
/**
*
* @param headers
* @param rows
* @param totalsFnCollection
*/
getTotals: function (headers, rows, totalsFnCollection) {
return headers.map(function (header) {
var columnName = header[0];
var columnCells = rows.map(function(row) {
return row.reduce(function (res, cell) { return cell[0] === columnName ? cell[1].raw : res; });
});
var calcTotal = function () { return ''; }
// same totals for all headers
if (totalsFnCollection['*'] && columnName !== '__vert_header__') calcTotal = totalsFnCollection['*'];
else if (totalsFnCollection[columnName]) calcTotal = totalsFnCollection[columnName];
return calcTotal(columnCells, rows)
});
},
/**
*
* @param data
* @param groupingField
* @param unnamedSubstitution
*/
groupData: function (data, groupingField, unnamedSubstitution) {
var groupedData = {};
if (groupingField) {
data.forEach(function (item) {
var group = groupedData[item[groupingField].presentation] || [];
group.push(item);
groupedData[item[groupingField].presentation] = group;
delete item[groupingField];
});
} else {
groupedData[unnamedSubstitution || ''] = data;
}
return toPairs(groupedData);
},
joinVertHeaders: function joinVertHeaders(data, vertHeaders) {
if (!vertHeaders) return data;
var joinVerHeaderToRow = function(row, i) {
return [['__vert_header__', {
raw: vertHeaders[i],
presentation: vertHeaders[i]
}]].concat(row.slice(1))
};
return data.map(joinVerHeaderToRow);
},
deletedByKey: function (pairs, key) {
return pairs.filter(function(tuple) { return tuple[0] !== key; })
}
};