type-plus
Version:
Provides additional types for TypeScript.
28 lines • 906 B
JavaScript
/**
* Creates a context builder.
*
* @param init The initial context or an context initializer.
* @return the context builder where you can
* use `extend()` to add context, and
* use `build()` to build the context.
*/
export function context(init) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return typeof init === 'function' ? contextBuilder({}, [[init]]) : contextBuilder(init ?? {}, []);
}
/* eslint-disable */
function contextBuilder(init, extenders) {
return {
extend(extender) {
return contextBuilder(init, extenders.concat([[extender]]));
},
build() {
return extenders.reduce((p, [t], i) => {
const v = typeof t === 'function' ? (extenders[i][0] = t(p)) : t;
return { ...p, ...v };
}, init);
}
};
}
/* eslint-enable */
//# sourceMappingURL=context.js.map