@checksub_team/react-widgets
Version:
77 lines (59 loc) • 2.34 kB
JavaScript
;
exports.__esModule = true;
exports.isShallowEqual = isShallowEqual;
exports.chunk = chunk;
exports.groupBySortedKeys = groupBySortedKeys;
exports.has = exports.makeArray = void 0;
var _warning = _interopRequireDefault(require("warning"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var makeArray = function makeArray(obj) {
return obj == null ? [] : [].concat(obj);
};
exports.makeArray = makeArray;
var has = function has(o, k) {
return o ? Object.prototype.hasOwnProperty.call(o, k) : false;
};
exports.has = has;
function isShallowEqual(a, b) {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) return +a === +b;
if (typeof a !== 'object' && typeof b !== 'object') return a === b;
if (typeof a !== typeof b) return false;
if (a == null || b == null) return false; // if they were both null we wouldn't be here
var keysA = Object.keys(a);
var keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (var i = 0; i < keysA.length; i++) {
if (!has(b, keysA[i]) || a[keysA[i]] !== b[keysA[i]]) return false;
}
return true;
}
function chunk(array, chunkSize) {
var index = 0,
length = array ? array.length : 0;
var result = [];
chunkSize = Math.max(+chunkSize || 1, 1);
while (index < length) {
result.push(array.slice(index, index += chunkSize));
}
return result;
}
function groupBySortedKeys(groupBy, data, keys) {
var iter = typeof groupBy === 'function' ? groupBy : function (item) {
return item[groupBy];
}; // the keys array ensures that groups are rendered in the order they came in
// which means that if you sort the data array it will render sorted,
// so long as you also sorted by group
keys = keys || [];
process.env.NODE_ENV !== "production" ? (0, _warning.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")) : void 0;
return data.reduce(function (grps, item) {
var group = iter(item);
if (has(grps, group)) {
grps[group].push(item);
} else {
keys.push(group);
grps[group] = [item];
}
return grps;
}, {});
}