UNPKG

movex-core-util

Version:

Movex Core Util is the library of utilities for Movex

42 lines 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkedStateEquals = exports.computeCheckedState = void 0; const md5_1 = require("./md5"); const misc_1 = require("./misc"); /** * @param {object} obj Object to sort. * @returns {object} Copy of object with keys sorted in all nested objects */ const deepSort = (obj) => { if (Array.isArray(obj)) { return obj.map((item) => deepSort(item)); } if (obj && typeof obj === 'object') { return Object.keys(obj) .sort() .reduce((out, key) => { out[key] = deepSort(obj[key]); return out; }, {}); } return obj; }; const computeCheckedState = (state) => [ state, state === undefined ? '' : hashObject(state), ]; exports.computeCheckedState = computeCheckedState; const hashObject = (s) => { if (Array.isArray(s)) { return (0, md5_1.md5)(JSON.stringify(s)); } if ((0, misc_1.isObject)(s)) { return (0, md5_1.md5)(JSON.stringify(deepSort(s))); } return (0, md5_1.md5)(s); }; const checkedStateEquals = (a, b // They are the same if the instances are the same [0] or if the checksums are the same [1] ) => a[0] === b[0] || a[1] === b[1]; exports.checkedStateEquals = checkedStateEquals; //# sourceMappingURL=checkedState.js.map