@synstack/resolved
Version:
Type-safe piping of synchronous or asynchronous values
66 lines (64 loc) • 1.79 kB
JavaScript
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/resolvable.lib.ts
var resolveAll = (value) => {
if (!Array.isArray(value)) throw new Error("Expected an array");
if (value.some((v) => v instanceof Promise)) return Promise.all(value);
return value;
};
var Resolver = class _Resolver {
_value;
constructor(value) {
this._value = value;
}
/**
* Get the value of the resolver
* @returns The value or a single promise of the value
*
* - If the value is an array containing promises, the array will be resolved with `Promise.all`
*/
get $() {
if (Array.isArray(this._value)) {
return resolveAll(this._value);
}
return this._value;
}
valueOf() {
return this._value;
}
/**
* Apply a function to the value
* @param fn the function to apply to the value
* @returns a new Resolver instance with the result of the function, either a value or a promise of a value
*/
_(fn) {
if (Array.isArray(this._value)) {
const hasPromise = this._value.some((v) => v instanceof Promise);
if (hasPromise)
return new _Resolver(Promise.all(this._value).then(fn));
return new _Resolver(fn(this._value));
}
if (this._value instanceof Promise)
return new _Resolver(this._value.then(fn));
return new _Resolver(fn(this._value));
}
};
var pipe = (value) => {
return new Resolver(value);
};
// src/resolved.bundle.ts
var resolved_bundle_exports = {};
__export(resolved_bundle_exports, {
pipe: () => pipe,
resolveAll: () => resolveAll
});
export {
Resolver,
pipe,
resolved_bundle_exports as resolvable,
resolveAll
};
//# sourceMappingURL=resolved.index.js.map