forked from zicloud/bigscreen_admin
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.8 KiB
JavaScript
86 lines
2.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const cloneDeep = require("lodash.clonedeep");
|
|
const isEqual = require("lodash.isequal");
|
|
var AttributeMap;
|
|
(function (AttributeMap) {
|
|
function compose(a = {}, b = {}, keepNull = false) {
|
|
if (typeof a !== 'object') {
|
|
a = {};
|
|
}
|
|
if (typeof b !== 'object') {
|
|
b = {};
|
|
}
|
|
let attributes = cloneDeep(b);
|
|
if (!keepNull) {
|
|
attributes = Object.keys(attributes).reduce((copy, key) => {
|
|
if (attributes[key] != null) {
|
|
copy[key] = attributes[key];
|
|
}
|
|
return copy;
|
|
}, {});
|
|
}
|
|
for (const key in a) {
|
|
if (a[key] !== undefined && b[key] === undefined) {
|
|
attributes[key] = a[key];
|
|
}
|
|
}
|
|
return Object.keys(attributes).length > 0 ? attributes : undefined;
|
|
}
|
|
AttributeMap.compose = compose;
|
|
function diff(a = {}, b = {}) {
|
|
if (typeof a !== 'object') {
|
|
a = {};
|
|
}
|
|
if (typeof b !== 'object') {
|
|
b = {};
|
|
}
|
|
const attributes = Object.keys(a)
|
|
.concat(Object.keys(b))
|
|
.reduce((attrs, key) => {
|
|
if (!isEqual(a[key], b[key])) {
|
|
attrs[key] = b[key] === undefined ? null : b[key];
|
|
}
|
|
return attrs;
|
|
}, {});
|
|
return Object.keys(attributes).length > 0 ? attributes : undefined;
|
|
}
|
|
AttributeMap.diff = diff;
|
|
function invert(attr = {}, base = {}) {
|
|
attr = attr || {};
|
|
const baseInverted = Object.keys(base).reduce((memo, key) => {
|
|
if (base[key] !== attr[key] && attr[key] !== undefined) {
|
|
memo[key] = base[key];
|
|
}
|
|
return memo;
|
|
}, {});
|
|
return Object.keys(attr).reduce((memo, key) => {
|
|
if (attr[key] !== base[key] && base[key] === undefined) {
|
|
memo[key] = null;
|
|
}
|
|
return memo;
|
|
}, baseInverted);
|
|
}
|
|
AttributeMap.invert = invert;
|
|
function transform(a, b, priority = false) {
|
|
if (typeof a !== 'object') {
|
|
return b;
|
|
}
|
|
if (typeof b !== 'object') {
|
|
return undefined;
|
|
}
|
|
if (!priority) {
|
|
return b; // b simply overwrites us without priority
|
|
}
|
|
const attributes = Object.keys(b).reduce((attrs, key) => {
|
|
if (a[key] === undefined) {
|
|
attrs[key] = b[key]; // null is a valid value
|
|
}
|
|
return attrs;
|
|
}, {});
|
|
return Object.keys(attributes).length > 0 ? attributes : undefined;
|
|
}
|
|
AttributeMap.transform = transform;
|
|
})(AttributeMap || (AttributeMap = {}));
|
|
exports.default = AttributeMap;
|
|
//# sourceMappingURL=AttributeMap.js.map
|