UNPKG

@synstack/resolved

Version:

Type-safe piping of synchronous or asynchronous values

1 lines 5.52 kB
{"version":3,"sources":["../src/resolved.index.ts","../src/resolvable.lib.ts","../src/resolved.bundle.ts"],"sourcesContent":["export { Resolver, type Resolvable } from \"./resolvable.lib.ts\";\nexport * from \"./resolved.bundle.ts\";\nexport * as resolvable from \"./resolved.bundle.ts\";\n","export type Resolvable<T> = Promise<T> | T;\n\nexport declare namespace Resolvable {\n export type Infer<T> = Awaited<T>;\n export type IsPromise<T> = T extends Promise<any> ? true : never;\n\n export type ArrayOf<T> = Array<Resolvable<T>>;\n\n export namespace ArrayOf {\n export type Infer<T> = T extends readonly any[]\n ? {\n [K in keyof T]: Resolvable.Infer<T[K]>;\n }\n : never;\n\n export type HasPromise<T> = T extends readonly any[]\n ? {\n [K in keyof T]: Resolvable.IsPromise<T[K]>;\n }[number]\n : never;\n }\n\n export type MaybeArray<T> = Resolvable.ArrayOf<T> | Resolvable<T>;\n\n export namespace MaybeArray {\n export type Infer<T> = T extends readonly any[]\n ? { [K in keyof T]: Resolvable.Infer<T[K]> }\n : Resolvable.Infer<T>;\n\n export type IsPromise<T> = T extends readonly any[]\n ? { [K in keyof T]: Resolvable.IsPromise<T[K]> }[number]\n : Resolvable.IsPromise<T>;\n }\n}\n\n/**\n * Resolves all values in the array in parallel\n * @param value The array to resolve\n * @returns If the array contains promises, a promise of an array of values. Otherwise, the array.\n */\nexport const resolveAll = <U extends readonly any[]>(\n value: U,\n): true extends Resolvable.ArrayOf.HasPromise<U>\n ? Promise<Resolvable.ArrayOf.Infer<U>>\n : U => {\n if (!Array.isArray(value)) throw new Error(\"Expected an array\");\n // @ts-expect-error - We know that the value is not a promise\n if (value.some((v) => v instanceof Promise)) return Promise.all(value);\n // @ts-expect-error - We know that the value is not a promise\n return value;\n};\n\nexport class Resolver<T extends Resolvable<any>> {\n private readonly _value: T;\n\n public constructor(value: T) {\n this._value = value;\n }\n\n /**\n * Get the value of the resolver\n * @returns The value or a single promise of the value\n *\n * - If the value is an array containing promises, the array will be resolved with `Promise.all`\n */\n public get $(): T extends readonly any[]\n ? true extends Resolvable.ArrayOf.HasPromise<T>\n ? Promise<Resolvable.ArrayOf.Infer<T>>\n : T\n : T {\n if (Array.isArray(this._value)) {\n // @ts-expect-error - We know that the value is an array\n return resolveAll(this._value);\n }\n // @ts-expect-error - We know that the value is not an array\n return this._value;\n }\n\n public valueOf(): T {\n return this._value;\n }\n\n /**\n * Apply a function to the value\n * @param fn the function to apply to the value\n * @returns a new Resolver instance with the result of the function, either a value or a promise of a value\n */\n public _<R>(\n fn: (value: Resolvable.MaybeArray.Infer<T>) => R,\n ): true extends Resolvable.MaybeArray.IsPromise<T>\n ? true extends Resolvable.MaybeArray.IsPromise<R>\n ? Resolver<R>\n : Resolver<Promise<R>>\n : Resolver<R> {\n if (Array.isArray(this._value)) {\n const hasPromise = this._value.some((v) => v instanceof Promise);\n if (hasPromise)\n // @ts-expect-error - We know that the value is a promise\n return new Resolver(Promise.all(this._value).then(fn));\n // @ts-expect-error - We know that the value is not a promise\n return new Resolver(fn(this._value));\n }\n if (this._value instanceof Promise)\n // @ts-expect-error - We know that the value is a promise\n return new Resolver(this._value.then(fn));\n // @ts-expect-error - We know that the value is not a promise\n return new Resolver(fn(this._value as Resolvable.Infer<T>));\n }\n}\n\n/**\n * A piping utility which preserves the sync/async state of the value\n * @param value The value to pipe\n * @returns A new Resolver instance\n *\n * ```ts\n * import { pipe } from \"@synstack/resolved\";\n *\n * // Sync\n * const value: string = pipe(\"Hello World\")._((v) => v.toUpperCase()).$;\n *\n * // Async\n * const promiseValue: Promise<string> = pipe(\"Hello World\")._((v) => Promise.resolve(v.toUpperCase())).$;\n * ```\n */\nexport const pipe = <T>(value: T) => {\n return new Resolver<T>(value);\n};\n","export { pipe, resolveAll } from \"./resolvable.lib.ts\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACwCO,IAAM,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