pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
19 lines (18 loc) • 656 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.update = void 0;
const main_1 = require("./main");
const updateArray = (idx, fn, arr) => {
const result = Array.from(arr);
result[idx] = fn(result[idx]);
return result;
};
function update(key, fn, coll) {
if (arguments.length === 1)
return (fn_) => update(key, fn_);
else if (arguments.length === 2)
return (coll_) => update(key, fn, coll_);
const d = (0, main_1.getValueOr)({}, coll);
return d instanceof Array ? updateArray(key, fn, d) : Object.assign(Object.assign({}, d), { [key]: fn(d[key]) });
}
exports.update = update;