UNPKG

type-plus

Version:
32 lines 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.context = void 0; /** * 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. */ function context(init) { // eslint-disable-next-line @typescript-eslint/no-unsafe-return return typeof init === 'function' ? contextBuilder({}, [[init]]) : contextBuilder(init ?? {}, []); } exports.context = context; /* 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