|
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- var lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
- var lodash_isequal_1 = __importDefault(require("lodash.isequal"));
- var AttributeMap;
- (function (AttributeMap) {
- function compose(a, b, keepNull) {
- if (a === void 0) { a = {}; }
- if (b === void 0) { b = {}; }
- if (typeof a !== 'object') {
- a = {};
- }
- if (typeof b !== 'object') {
- b = {};
- }
- var attributes = lodash_clonedeep_1.default(b);
- if (!keepNull) {
- attributes = Object.keys(attributes).reduce(function (copy, key) {
- if (attributes[key] != null) {
- copy[key] = attributes[key];
- }
- return copy;
- }, {});
- }
- for (var 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 (a === void 0) { a = {}; }
- if (b === void 0) { b = {}; }
- if (typeof a !== 'object') {
- a = {};
- }
- if (typeof b !== 'object') {
- b = {};
- }
- var attributes = Object.keys(a)
- .concat(Object.keys(b))
- .reduce(function (attrs, key) {
- if (!lodash_isequal_1.default(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) {
- if (attr === void 0) { attr = {}; }
- if (base === void 0) { base = {}; }
- attr = attr || {};
- var baseInverted = Object.keys(base).reduce(function (memo, key) {
- if (base[key] !== attr[key] && attr[key] !== undefined) {
- memo[key] = base[key];
- }
- return memo;
- }, {});
- return Object.keys(attr).reduce(function (memo, key) {
- if (attr[key] !== base[key] && base[key] === undefined) {
- memo[key] = null;
- }
- return memo;
- }, baseInverted);
- }
- AttributeMap.invert = invert;
- function transform(a, b, priority) {
- if (priority === void 0) { priority = false; }
- if (typeof a !== 'object') {
- return b;
- }
- if (typeof b !== 'object') {
- return undefined;
- }
- if (!priority) {
- return b; // b simply overwrites us without priority
- }
- var attributes = Object.keys(b).reduce(function (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
|