@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
51 lines (50 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.extendPropsWithContext = extendPropsWithContext;
exports.extendPropsWithContextInClassComponent = extendPropsWithContextInClassComponent;
exports.reduceContext = reduceContext;
function extendPropsWithContext(props, defaults = {}, ...contexts) {
props = {
...defaults,
...props
};
return {
...props,
...reduceContextHasValue(props, defaults, contexts)
};
}
function extendPropsWithContextInClassComponent(props, defaults = {}, ...contexts) {
return {
...props,
...reduceContextHasValue(props, defaults, contexts, {
onlyMergeExistingProps: true
})
};
}
function reduceContext(contexts) {
return contexts.reduce((acc, cur) => {
if (cur) {
acc = {
...acc,
...cur
};
}
return acc;
}, {});
}
function reduceContextHasValue(props, defaults, contexts, {
onlyMergeExistingProps = false
} = {}) {
const context = reduceContext(contexts);
return Object.entries(context).reduce((acc, [key, value]) => {
if (!onlyMergeExistingProps || Object.prototype.hasOwnProperty.call(props, key)) {
if (props[key] === (defaults === null || defaults === void 0 ? void 0 : defaults[key])) {
acc[key] = value;
}
}
return acc;
}, {});
}
//# sourceMappingURL=extendPropsWithContext.js.map