UNPKG

1.29 kBJavaScriptView Raw
1/* eslint-disable @typescript-eslint/consistent-type-assertions */
2import warning from 'tiny-warning';
3export function toItemArray(a) {
4 if (Array.isArray(a)) return a;
5 return [];
6}
7export const makeArray = (obj, excludeNull = true) => {
8 const result = [];
9 return excludeNull ? obj == null ? result : result.concat(obj) : result.concat(obj);
10};
11export const has = (o, key) => o ? Object.prototype.hasOwnProperty.call(o, key) : false;
12export function chunk(array, chunkSize) {
13 let index = 0;
14 let length = array ? array.length : 0;
15 let result = [];
16 chunkSize = Math.max(+chunkSize || 1, 1);
17
18 while (index < length) result.push(array.slice(index, index += chunkSize));
19
20 return result;
21}
22export function groupBySortedKeys(groupBy, data, _keys = []) {
23 const iter = typeof groupBy === 'function' ? groupBy : item => item[groupBy];
24 warning(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`);
25 const groups = new Map();
26 data.forEach(item => {
27 let group = iter(item);
28 if (groups.has(group)) groups.get(group).push(item);else groups.set(group, [item]);
29 });
30 return Array.from(groups);
31}
\No newline at end of file