How to use the @politie/sherlock.derive function in @politie/sherlock

To help you get started, we’ve selected a few @politie/sherlock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github politie / sherlock / extensions / sherlock-utils / src / derivable-cache.spec.ts View on Github external
it('should support constants as output of the derivable factory', () => {
        const derivableFactory = spy((v: number) => constant(v));
        const identityCache = derivableCache({ derivableFactory });
        derive(() => identityCache(1).get() + identityCache(2).get()).react(() => 0);
        expect(derivableFactory).to.have.been.calledTwice;
        identityCache(1).get();
        identityCache(2).get();
        expect(derivableFactory).to.have.been.calledTwice;
    });
github politie / sherlock / extensions / sherlock-utils / src / struct.ts View on Github external
export function struct(obj: any) {
    if (isDerivable(obj)) {
        return obj;
    }
    if (!Array.isArray(obj) && !utils.isPlainObject(obj)) {
        throw new Error('"struct" only accepts Derivables, plain Objects and Arrays');
    }
    return derive(deepUnwrap, obj);
}
github politie / sherlock / extensions / sherlock-utils / src / template.ts View on Github external
export function template(parts: TemplateStringsArray, ...args: any[]): Derivable {
    return derive(() => {
        let s = '';
        for (let i = 0; i < parts.length; i++) {
            s += parts[i];
            if (i < args.length) {
                s += unwrap(args[i]);
            }
        }
        return s;
    });
}
github politie / sherlock / extensions / sherlock-utils / src / control-flow.ts View on Github external
function prepareOptions(base: Derivable, opts?: ControlFlowOptions, ): PreparedOptions {
    const result: PreparedOptions = {};
    if (opts) {
        for (const key of Object.keys(opts) as Array) {
            const opt = opts[key];
            result[key] = typeof opt === 'function' ? derive(() => unwrap(opt(base))) : opt;
        }
    }
    return result;
}