UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

44 lines 1.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.propertyChain = exports.continuePropertyChain = void 0; const guard_1 = require("@3fv/guard"); function continuePropertyChain(state, data, keyPath = []) { keyPath = keyPath || []; // noinspection DuplicatedCode return new Proxy((overrideCallback) => { // TRACK FIRST PROP ACCESS const firstGet = keyPath.map(() => true); // CHECK IF KEY SHOULD BE NUMBER function resolveKey(value, key, index) { if (firstGet[index]) { if (Array.isArray(value)) { const keyNum = guard_1.toNumber(key); if (guard_1.isNumber(keyNum)) { key = keyPath[index] = keyNum; } } firstGet[index] = false; } return key; } const getter = (state) => keyPath.reduce((value, key, index) => { return value[resolveKey(value, key, index)]; }, state); return overrideCallback(getter, keyPath); }, { get: (target, key) => { return continuePropertyChain(state, undefined, [...keyPath, key]); } }); } exports.continuePropertyChain = continuePropertyChain; /** * Start a new prop chain of the type specified * * @param state */ function propertyChain(state = undefined) { return continuePropertyChain(state, state); } exports.propertyChain = propertyChain; //# sourceMappingURL=PropertyChain.js.map