UNPKG

1.65 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.toItemArray = toItemArray;
5exports.chunk = chunk;
6exports.groupBySortedKeys = groupBySortedKeys;
7exports.has = exports.makeArray = void 0;
8
9var _tinyWarning = _interopRequireDefault(require("tiny-warning"));
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13/* eslint-disable @typescript-eslint/consistent-type-assertions */
14function toItemArray(a) {
15 if (Array.isArray(a)) return a;
16 return [];
17}
18
19const makeArray = (obj, excludeNull = true) => {
20 const result = [];
21 return excludeNull ? obj == null ? result : result.concat(obj) : result.concat(obj);
22};
23
24exports.makeArray = makeArray;
25
26const has = (o, key) => o ? Object.prototype.hasOwnProperty.call(o, key) : false;
27
28exports.has = has;
29
30function chunk(array, chunkSize) {
31 let index = 0;
32 let length = array ? array.length : 0;
33 let result = [];
34 chunkSize = Math.max(+chunkSize || 1, 1);
35
36 while (index < length) result.push(array.slice(index, index += chunkSize));
37
38 return result;
39}
40
41function groupBySortedKeys(groupBy, data, _keys = []) {
42 const iter = typeof groupBy === 'function' ? groupBy : item => item[groupBy];
43 (0, _tinyWarning.default)(typeof groupBy !== 'string' || !data.length || has(data[0], groupBy), `[React Widgets] You seem to be trying to group this list by a ` + `property \`${groupBy}\` that doesn't exist in the dataset items, this may be a typo`);
44 const groups = new Map();
45 data.forEach(item => {
46 let group = iter(item);
47 if (groups.has(group)) groups.get(group).push(item);else groups.set(group, [item]);
48 });
49 return Array.from(groups);
50}
\No newline at end of file