pragmatic-fp-ts
Version:
Opinionated functional programming library with easy use in mind
31 lines (30 loc) • 1.14 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assocIn = void 0;
const main_1 = require("./main");
const Queue_1 = __importDefault(require("./tools/Queue"));
const _assocIn = (path, value, coll) => {
const key = path.peek();
const nextColl = path.length > 1 && !coll ? (typeof key === "number" ? [] : {}) : coll;
path.pop();
return (0, main_1.assoc)(key, path.isEmpty() ? value : _assocIn(path, value, nextColl[key]), coll);
};
function assocIn(path, value, coll) {
if (arguments.length === 1) {
return function (_value, _coll) {
return arguments.length === 1 ? assocIn(path, _value) : assocIn(path, _value, _coll);
};
}
else if (arguments.length === 2) {
return (_coll) => assocIn(path, value, _coll);
}
const p = new Queue_1.default((0, main_1.getValueOr)([], path));
if (p.length === 0) {
return coll;
}
return _assocIn(p, value, coll);
}
exports.assocIn = assocIn;