UNPKG

@barchart/common-js

Version:
123 lines (121 loc) 3.84 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var object_exports = {}; __export(object_exports, { clone: () => clone, empty: () => empty, equals: () => equals, keys: () => keys, merge: () => merge }); module.exports = __toCommonJS(object_exports); var array = __toESM(require("./array.js")); var is = __toESM(require("./is.js")); function equals(a, b) { let returnVal; if (a === b) { returnVal = true; } else if (is.array(a) && is.array(b)) { if (a.length === b.length) { returnVal = a.length === 0 || a.every((x, i) => equals(x, b[i])); } else { returnVal = false; } } else if (is.object(a) && is.object(b)) { if (is.fn(a.equals) && is.fn(b.equals)) { returnVal = a.equals(b); } else { const keysA = keys(a); const keysB = keys(b); returnVal = array.differenceSymmetric(keysA, keysB).length === 0 && keysA.every((key) => { const valueA = a[key]; const valueB = b[key]; return equals(valueA, valueB); }); } } else { returnVal = false; } return returnVal; } function clone(source, canExtract, extractor) { let c; if (is.fn(canExtract) && canExtract(source)) { c = extractor(source); } else if (is.date(source)) { c = new Date(source.getTime()); } else if (is.array(source)) { c = source.map((sourceItem) => { return clone(sourceItem, canExtract, extractor); }); } else if (is.object(source)) { c = keys(source).reduce((accumulator, key) => { accumulator[key] = clone(source[key], canExtract, extractor); return accumulator; }, {}); } else { c = source; } return c; } function merge(a, b) { let m; const mergeTarget = is.object(a) && !is.array(a); const mergeSource = is.object(b) && !is.array(b); if (mergeTarget && mergeSource) { const properties = array.unique(keys(a).concat(keys(b))); m = properties.reduce((accumulator, property) => { accumulator[property] = merge(a[property], b[property]); return accumulator; }, {}); } else if (is.undef(b)) { m = clone(a); } else { m = clone(b); } return m; } function keys(target) { const keys2 = []; for (let k in target) { if (Object.prototype.hasOwnProperty.call(target, k)) { keys2.push(k); } } return keys2; } function empty(target) { let empty2 = true; for (let k in target) { if (Object.prototype.hasOwnProperty.call(target, k)) { empty2 = false; break; } } return empty2; }