Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
});
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);
}
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;
});
}
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;
}