typedux
Version: 
Slightly adjusted Redux (awesome by default) for TS
37 lines • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.selector = void 0;
/**
 * Optional chaining with default values. To inspect a property value in
 * a tree-like structure, invoke it as a function, optionally passing a default value.
 *
 * @example
 *   // Given:
 *   const x = oc<T>({
 *     a: 'hello',
 *     b: { d: 'world' },
 *     c: [-100, 200, -300],
 *   });
 *
 *   // Then:
 *   x.a() === 'hello'
 *   x.b.d() === 'world'
 *   x.c[0]() === -100
 *   x.c[100]() === undefined
 *   x.c[100](1234) === 1234
 *   x.c.map((e) => e()) === [-100, 200, -300]
 *   x.d.e() === undefined
 *   x.d.e('optional default value') === 'optional default value'
 *   (x as any).y.z.a.b.c.d.e.f.g.h.i.j.k() === undefined
 */
//export declare function oc<T>(data?: T): SelectorChainType<T>;
function selector(data) {
    return new Proxy(((defaultValue) => (data == null ? defaultValue : data)), {
        get: (target, key) => {
            const obj = target();
            return selector(typeof obj === "object" ? obj[key] : undefined);
        }
    });
}
exports.selector = selector;
//# sourceMappingURL=SelectorBuilder.js.map