@synstack/resolved 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { R as Resolvable } from '../resolvable.lib-CU3Va7aC.cjs';
1
+ import { R as Resolvable } from '../resolvable.lib-D1t15KWc.cjs';
2
2
 
3
3
  type Callable<T> = T | (() => T);
4
4
  declare namespace Callable {
@@ -1,4 +1,4 @@
1
- import { R as Resolvable } from '../resolvable.lib-CU3Va7aC.js';
1
+ import { R as Resolvable } from '../resolvable.lib-D1t15KWc.js';
2
2
 
3
3
  type Callable<T> = T | (() => T);
4
4
  declare namespace Callable {
@@ -2,6 +2,7 @@ type Resolvable<T> = Promise<T> | T;
2
2
  declare namespace Resolvable {
3
3
  type Infer<T> = Awaited<T>;
4
4
  type IsPromise<T> = T extends Promise<any> ? true : never;
5
+ type ArrayOf<T> = Array<Resolvable<T>>;
5
6
  namespace ArrayOf {
6
7
  type Infer<T> = T extends readonly any[] ? {
7
8
  [K in keyof T]: Resolvable.Infer<T[K]>;
@@ -10,6 +11,7 @@ declare namespace Resolvable {
10
11
  [K in keyof T]: Resolvable.IsPromise<T[K]>;
11
12
  }[number] : never;
12
13
  }
14
+ type MaybeArray<T> = Resolvable.ArrayOf<T> | Resolvable<T>;
13
15
  namespace MaybeArray {
14
16
  type Infer<T> = T extends readonly any[] ? {
15
17
  [K in keyof T]: Resolvable.Infer<T[K]>;
@@ -2,6 +2,7 @@ type Resolvable<T> = Promise<T> | T;
2
2
  declare namespace Resolvable {
3
3
  type Infer<T> = Awaited<T>;
4
4
  type IsPromise<T> = T extends Promise<any> ? true : never;
5
+ type ArrayOf<T> = Array<Resolvable<T>>;
5
6
  namespace ArrayOf {
6
7
  type Infer<T> = T extends readonly any[] ? {
7
8
  [K in keyof T]: Resolvable.Infer<T[K]>;
@@ -10,6 +11,7 @@ declare namespace Resolvable {
10
11
  [K in keyof T]: Resolvable.IsPromise<T[K]>;
11
12
  }[number] : never;
12
13
  }
14
+ type MaybeArray<T> = Resolvable.ArrayOf<T> | Resolvable<T>;
13
15
  namespace MaybeArray {
14
16
  type Infer<T> = T extends readonly any[] ? {
15
17
  [K in keyof T]: Resolvable.Infer<T[K]>;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/resolved.index.ts","../src/resolved.bundle.ts","../src/resolvable.lib.ts"],"sourcesContent":["export type { Resolvable, Resolver } from \"./resolvable.lib.ts\";\nexport * from \"./resolved.bundle.ts\";\nexport * as resolvable from \"./resolved.bundle.ts\";\n","export { pipe, resolveAll } from \"./resolvable.lib.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 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 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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;;;ACoCO,IAAM,aAAa,CACxB,UAGO;AACP,MAAI,CAAC,MAAM,QAAQ,KAAK,EAAG,OAAM,IAAI,MAAM,mBAAmB;AAE9D,MAAI,MAAM,KAAK,CAAC,MAAM,aAAa,OAAO,EAAG,QAAO,QAAQ,IAAI,KAAK;AAErE,SAAO;AACT;AAEO,IAAM,WAAN,MAAM,UAAoC;AAAA,EAC9B;AAAA,EAEV,YAAY,OAAU;AAC3B,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAW,IAIL;AACJ,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAE9B,aAAO,WAAW,KAAK,MAAM;AAAA,IAC/B;AAEA,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,UAAa;AAClB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,EACL,IAKc;AACd,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAC9B,YAAM,aAAa,KAAK,OAAO,KAAK,CAAC,MAAM,aAAa,OAAO;AAC/D,UAAI;AAEF,eAAO,IAAI,UAAS,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,EAAE,CAAC;AAEvD,aAAO,IAAI,UAAS,GAAG,KAAK,MAAM,CAAC;AAAA,IACrC;AACA,QAAI,KAAK,kBAAkB;AAEzB,aAAO,IAAI,UAAS,KAAK,OAAO,KAAK,EAAE,CAAC;AAE1C,WAAO,IAAI,UAAS,GAAG,KAAK,MAA6B,CAAC;AAAA,EAC5D;AACF;AAiBO,IAAM,OAAO,CAAI,UAAa;AACnC,SAAO,IAAI,SAAY,KAAK;AAC9B;","names":[]}
1
+ {"version":3,"sources":["../src/resolved.index.ts","../src/resolved.bundle.ts","../src/resolvable.lib.ts"],"sourcesContent":["export type { Resolvable, Resolver } from \"./resolvable.lib.ts\";\nexport * from \"./resolved.bundle.ts\";\nexport * as resolvable from \"./resolved.bundle.ts\";\n","export { pipe, resolveAll } from \"./resolvable.lib.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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;;;ACwCO,IAAM,aAAa,CACxB,UAGO;AACP,MAAI,CAAC,MAAM,QAAQ,KAAK,EAAG,OAAM,IAAI,MAAM,mBAAmB;AAE9D,MAAI,MAAM,KAAK,CAAC,MAAM,aAAa,OAAO,EAAG,QAAO,QAAQ,IAAI,KAAK;AAErE,SAAO;AACT;AAEO,IAAM,WAAN,MAAM,UAAoC;AAAA,EAC9B;AAAA,EAEV,YAAY,OAAU;AAC3B,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAW,IAIL;AACJ,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAE9B,aAAO,WAAW,KAAK,MAAM;AAAA,IAC/B;AAEA,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,UAAa;AAClB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,EACL,IAKc;AACd,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAC9B,YAAM,aAAa,KAAK,OAAO,KAAK,CAAC,MAAM,aAAa,OAAO;AAC/D,UAAI;AAEF,eAAO,IAAI,UAAS,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,EAAE,CAAC;AAEvD,aAAO,IAAI,UAAS,GAAG,KAAK,MAAM,CAAC;AAAA,IACrC;AACA,QAAI,KAAK,kBAAkB;AAEzB,aAAO,IAAI,UAAS,KAAK,OAAO,KAAK,EAAE,CAAC;AAE1C,WAAO,IAAI,UAAS,GAAG,KAAK,MAA6B,CAAC;AAAA,EAC5D;AACF;AAiBO,IAAM,OAAO,CAAI,UAAa;AACnC,SAAO,IAAI,SAAY,KAAK;AAC9B;","names":[]}
@@ -1,5 +1,5 @@
1
- import { p as pipe, r as resolveAll } from './resolvable.lib-CU3Va7aC.cjs';
2
- export { R as Resolvable, a as Resolver } from './resolvable.lib-CU3Va7aC.cjs';
1
+ import { p as pipe, r as resolveAll } from './resolvable.lib-D1t15KWc.cjs';
2
+ export { R as Resolvable, a as Resolver } from './resolvable.lib-D1t15KWc.cjs';
3
3
 
4
4
  declare const resolved_bundle_pipe: typeof pipe;
5
5
  declare const resolved_bundle_resolveAll: typeof resolveAll;
@@ -1,5 +1,5 @@
1
- import { p as pipe, r as resolveAll } from './resolvable.lib-CU3Va7aC.js';
2
- export { R as Resolvable, a as Resolver } from './resolvable.lib-CU3Va7aC.js';
1
+ import { p as pipe, r as resolveAll } from './resolvable.lib-D1t15KWc.js';
2
+ export { R as Resolvable, a as Resolver } from './resolvable.lib-D1t15KWc.js';
3
3
 
4
4
  declare const resolved_bundle_pipe: typeof pipe;
5
5
  declare const resolved_bundle_resolveAll: typeof resolveAll;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/resolved.bundle.ts","../src/resolvable.lib.ts"],"sourcesContent":["export { pipe, resolveAll } from \"./resolvable.lib.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 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 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"],"mappings":";;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACoCO,IAAM,aAAa,CACxB,UAGO;AACP,MAAI,CAAC,MAAM,QAAQ,KAAK,EAAG,OAAM,IAAI,MAAM,mBAAmB;AAE9D,MAAI,MAAM,KAAK,CAAC,MAAM,aAAa,OAAO,EAAG,QAAO,QAAQ,IAAI,KAAK;AAErE,SAAO;AACT;AAEO,IAAM,WAAN,MAAM,UAAoC;AAAA,EAC9B;AAAA,EAEV,YAAY,OAAU;AAC3B,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAW,IAIL;AACJ,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAE9B,aAAO,WAAW,KAAK,MAAM;AAAA,IAC/B;AAEA,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,UAAa;AAClB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,EACL,IAKc;AACd,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAC9B,YAAM,aAAa,KAAK,OAAO,KAAK,CAAC,MAAM,aAAa,OAAO;AAC/D,UAAI;AAEF,eAAO,IAAI,UAAS,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,EAAE,CAAC;AAEvD,aAAO,IAAI,UAAS,GAAG,KAAK,MAAM,CAAC;AAAA,IACrC;AACA,QAAI,KAAK,kBAAkB;AAEzB,aAAO,IAAI,UAAS,KAAK,OAAO,KAAK,EAAE,CAAC;AAE1C,WAAO,IAAI,UAAS,GAAG,KAAK,MAA6B,CAAC;AAAA,EAC5D;AACF;AAiBO,IAAM,OAAO,CAAI,UAAa;AACnC,SAAO,IAAI,SAAY,KAAK;AAC9B;","names":[]}
1
+ {"version":3,"sources":["../src/resolved.bundle.ts","../src/resolvable.lib.ts"],"sourcesContent":["export { pipe, resolveAll } from \"./resolvable.lib.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"],"mappings":";;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACwCO,IAAM,aAAa,CACxB,UAGO;AACP,MAAI,CAAC,MAAM,QAAQ,KAAK,EAAG,OAAM,IAAI,MAAM,mBAAmB;AAE9D,MAAI,MAAM,KAAK,CAAC,MAAM,aAAa,OAAO,EAAG,QAAO,QAAQ,IAAI,KAAK;AAErE,SAAO;AACT;AAEO,IAAM,WAAN,MAAM,UAAoC;AAAA,EAC9B;AAAA,EAEV,YAAY,OAAU;AAC3B,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAW,IAIL;AACJ,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAE9B,aAAO,WAAW,KAAK,MAAM;AAAA,IAC/B;AAEA,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,UAAa;AAClB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,EACL,IAKc;AACd,QAAI,MAAM,QAAQ,KAAK,MAAM,GAAG;AAC9B,YAAM,aAAa,KAAK,OAAO,KAAK,CAAC,MAAM,aAAa,OAAO;AAC/D,UAAI;AAEF,eAAO,IAAI,UAAS,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,EAAE,CAAC;AAEvD,aAAO,IAAI,UAAS,GAAG,KAAK,MAAM,CAAC;AAAA,IACrC;AACA,QAAI,KAAK,kBAAkB;AAEzB,aAAO,IAAI,UAAS,KAAK,OAAO,KAAK,EAAE,CAAC;AAE1C,WAAO,IAAI,UAAS,GAAG,KAAK,MAA6B,CAAC;AAAA,EAC5D;AACF;AAiBO,IAAM,OAAO,CAAI,UAAa;AACnC,SAAO,IAAI,SAAY,KAAK;AAC9B;","names":[]}
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "1.1.2",
7
+ "version": "1.2.0",
8
8
  "author": {
9
9
  "name": "pAIrprog",
10
10
  "url": "https://pairprog.io"
@@ -20,8 +20,8 @@
20
20
  "build": "tsup",
21
21
  "build:watch": "tsup --watch",
22
22
  "test:types": "tsc --noEmit",
23
- "test:unit": "node --experimental-strip-types --test src/**/*.test.ts",
24
- "test:unit:watch": "node --experimental-strip-types --watch --test --watch src/**/*.test.ts",
23
+ "test:unit": "node --experimental-strip-types --experimental-test-snapshots --no-warnings --test src/**/*.test.ts",
24
+ "test:unit:watch": "node --experimental-strip-types --experimental-test-snapshots --no-warnings --watch --test --watch src/**/*.test.ts",
25
25
  "test": "yarn test:types && yarn test:unit",
26
26
  "prepare": "yarn test && yarn build"
27
27
  },
@@ -57,5 +57,5 @@
57
57
  "!src/**/*.test.ts",
58
58
  "dist/**/*"
59
59
  ],
60
- "gitHead": "34bad542796c3d22efe0b4f7f4aabb05ec72aebd"
60
+ "gitHead": "348d1987aba7aee8fbc5469d071e3c71663fc775"
61
61
  }
@@ -4,7 +4,9 @@ export declare namespace Resolvable {
4
4
  export type Infer<T> = Awaited<T>;
5
5
  export type IsPromise<T> = T extends Promise<any> ? true : never;
6
6
 
7
- namespace ArrayOf {
7
+ export type ArrayOf<T> = Array<Resolvable<T>>;
8
+
9
+ export namespace ArrayOf {
8
10
  export type Infer<T> = T extends readonly any[]
9
11
  ? {
10
12
  [K in keyof T]: Resolvable.Infer<T[K]>;
@@ -18,7 +20,9 @@ export declare namespace Resolvable {
18
20
  : never;
19
21
  }
20
22
 
21
- namespace MaybeArray {
23
+ export type MaybeArray<T> = Resolvable.ArrayOf<T> | Resolvable<T>;
24
+
25
+ export namespace MaybeArray {
22
26
  export type Infer<T> = T extends readonly any[]
23
27
  ? { [K in keyof T]: Resolvable.Infer<T[K]> }
24
28
  : Resolvable.Infer<T>;