UNPKG

@nova-ui/dashboards

Version:

Nova Dashboards is a framework designed to provide feature developers with a common solution for presenting data coming from various sources within a single view, as well as a set of predefined widget visualizations that are 100% configuration-driven and

115 lines 5.09 kB
"use strict"; // © 2022 SolarWinds Worldwide, LLC. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeChanges = void 0; const tslib_1 = require("tslib"); const isArray_1 = tslib_1.__importDefault(require("lodash/isArray")); const some_1 = tslib_1.__importDefault(require("lodash/some")); const union_1 = tslib_1.__importDefault(require("lodash/union")); /** * This function takes a source object with a prioritized list of changes. Goes through and detects the changes on every one of these structures and builds * a new object that respects immutability and updates object references only where necessary. This is used to merge lasagna overlays into a component state * that maintains its immutability and high performance of change detection. * * @param result * @param changes */ function mergeChanges(result, ...changes) { const validChanges = changes.filter((c) => typeof c.previousValue !== "undefined" || typeof c.currentValue !== "undefined"); // there were no changes in the provided values, so just return the input object if (!(0, some_1.default)(validChanges, (c) => c.currentValue !== c.previousValue)) { return result; } // if there is only one layer of change just return the currentValue to preserve references if (validChanges.length === 1) { return validChanges[0].currentValue; } // first we need to determine the value type const valueType = findType(validChanges); // if it's a primitive type or an array, then we don't go further and we take a new value with the highest priority if (!["object", "object[]"].includes(valueType)) { // we take the value with the last priority return takeHighestPriorityValue(validChanges); } // collect all the keys of all the objects in given changes const keys = (0, union_1.default)(...validChanges.map((c) => typeof c.currentValue === "object" ? Object.keys(c.currentValue || {}) : [])); const accTemplate = valueType === "object[]" ? [] : {}; const ret = keys.reduce((acc, key) => { const checkedResult = typeof result === "undefined" || result === null || typeof result[key] === "undefined" ? undefined : result[key]; const nestedChanges = validChanges.map((c) => ({ previousValue: typeof c.previousValue === "undefined" || c.previousValue == null ? undefined : c.previousValue[key], currentValue: typeof c.currentValue === "undefined" || c.currentValue == null ? undefined : c.currentValue[key], })); // recursively execute this method for given property acc[key] = mergeChanges(checkedResult, ...nestedChanges); return acc; }, accTemplate); return ret; } exports.mergeChanges = mergeChanges; /** * Take current value from given changes with highest priority - the last one wins * * @param changes */ function takeHighestPriorityValue(changes) { for (let i = changes.length - 1; i >= 0; i--) { const value = changes[i].currentValue; if (typeof value !== "undefined") { return value; } } return undefined; } function findType(changes) { for (const c of changes) { const type = typeof c.currentValue; if (type !== "undefined" && c.currentValue != null) { if ((0, isArray_1.default)(c.currentValue)) { const arrElemType = typeof c.currentValue[0]; if (arrElemType === "object") { return "object[]"; } else { return "array"; } } if (c.currentValue.constructor !== Object) { return "advanced"; } return type; } } return typeof undefined; } //# sourceMappingURL=merge-changes.js.map