map-fns
Version:
<h1 align="center"> <code>map-fns</code> </h1>
121 lines (114 loc) • 3.93 kB
JavaScript
;
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function isNotMergable(value) {
return (typeof value !== "object" || // Primitives
!value || // null
value.constructor !== Object // Arrays, Sets, etc
);
}
function deepMerge(target, toMerge) {
if (typeof toMerge === "function") {
return deepMerge(target, toMerge(target));
}
var out = __assign({}, target);
var kList = Object.keys(toMerge);
for (var _i = 0, kList_1 = kList; _i < kList_1.length; _i++) {
var k = kList_1[_i];
var valueToMerge = toMerge[k];
if (typeof valueToMerge === "function" && typeof out[k] !== "function") {
// The value to merge is a function, but the target is not a function.
//
// The value is a function that returns the actual value to merge.
valueToMerge = valueToMerge(out[k]);
}
if (isNotMergable(valueToMerge)) {
out[k] = valueToMerge;
continue;
}
out[k] = deepMerge(out[k], valueToMerge);
}
return out;
}
/**
* Creates a new `map` populated with every key in the original `map` where the
* value behind each `k` in `keys` is the value `map[k]` shallowly merged with
* the return value of `fn(map[k])`.
*
* ```tsx
* newMap[k] = { ...map[k], ...fn(map[k]) };
* ```
*
* Example usage:
*
* ```tsx
* const out = modifyInMap(
* { alice: { age: 34, gender: "f" }, bob: { age: 42, gender: "m" } },
* "alice",
* person => ({ age: person.age + 1 }),
* );
* console.log(out.alice); // { age: 35, gender: "f" }
* console.log(out.bob); // { age: 42, gender: "m" }
* ```
*
* The original `map` is not modified.
*
* An error is thrown if any key in `keys` does not exist in the map.
*
* @param map - The map to copy and modify.
* @param keys - The keys to merge within the map.
* @returns A new map with modified values.
*/
function mergeInMap(map, keys,
/**
* Expects a partial value of `map[key]`.
*
* Supports returning functions to compute properties like so:
*
* ```tsx
* const map = { key: { count: 1 } };
*
* // "Normal" syntax
* mergeInMap(map, "key", (item) => ({
* count: item.count + 1,
* }));
*
* // Compute function
* mergeInMap(map, "key", () => ({
* count: (count) => count + 1,
* }));
* ```
*/
toMerge) {
var obj = __assign({}, map);
var keyList = (Array.isArray(keys) ? keys : [keys]);
for (var _i = 0, keyList_1 = keyList; _i < keyList_1.length; _i++) {
var key = keyList_1[_i];
if (!obj.hasOwnProperty(key)) {
throw new Error("Key '".concat(key, "' does not exist in map."));
}
obj[key] = deepMerge(obj[key], toMerge);
}
return obj;
}
module.exports = mergeInMap;