UNPKG

typedux

Version:

Slightly adjusted Redux (awesome by default) for TS

39 lines 1.31 kB
import { isNumber, toNumber } from "@3fv/guard"; export 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 = toNumber(key); if (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]); } }); } /** * Start a new prop chain of the type specified * * @param state */ export function propertyChain(state = undefined) { return continuePropertyChain(state, state); } //# sourceMappingURL=PropertyChain.js.map