@synstack/resolved
Version:
Type-safe piping of synchronous or asynchronous values
90 lines (87 loc) • 2.75 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/resolved.index.ts
var resolved_index_exports = {};
__export(resolved_index_exports, {
Resolver: () => Resolver,
pipe: () => pipe,
resolvable: () => resolved_bundle_exports,
resolveAll: () => resolveAll
});
module.exports = __toCommonJS(resolved_index_exports);
// 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
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Resolver,
pipe,
resolvable,
resolveAll
});
//# sourceMappingURL=resolved.index.cjs.map