@synstack/resolved 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @synstack/resolved
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/callable/callable.index.ts
21
+ var callable_index_exports = {};
22
+ __export(callable_index_exports, {
23
+ callable: () => callable_bundle_exports,
24
+ default: () => callable_bundle_exports,
25
+ resolveNested: () => resolveNested
26
+ });
27
+ module.exports = __toCommonJS(callable_index_exports);
28
+
29
+ // src/callable/callable.bundle.ts
30
+ var callable_bundle_exports = {};
31
+ __export(callable_bundle_exports, {
32
+ resolveNested: () => resolveNested
33
+ });
34
+
35
+ // src/callable/callable.lib.ts
36
+ var resolveNested = (args) => {
37
+ if (args.length === 0) return [];
38
+ const unresolvedValues = args.map((v) => {
39
+ if (typeof v === "function") return v();
40
+ if (Array.isArray(v))
41
+ return v.map((sv) => typeof sv === "function" ? sv() : sv);
42
+ return v;
43
+ });
44
+ const isPromise = unresolvedValues.some((v) => {
45
+ if (v instanceof Promise) return true;
46
+ if (Array.isArray(v)) return v.some((v2) => v2 instanceof Promise);
47
+ return false;
48
+ });
49
+ if (!isPromise) return unresolvedValues;
50
+ return Promise.all(
51
+ unresolvedValues.map((v) => Array.isArray(v) ? Promise.all(v) : v)
52
+ );
53
+ };
54
+ // Annotate the CommonJS export names for ESM import in node:
55
+ 0 && (module.exports = {
56
+ callable,
57
+ resolveNested
58
+ });
59
+ //# sourceMappingURL=callable.index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/callable/callable.index.ts","../../src/callable/callable.bundle.ts","../../src/callable/callable.lib.ts"],"sourcesContent":["export * as callable from \"./callable.bundle\";\nexport { default } from \"./callable.default\";\nexport { resolveNested } from \"./callable.lib\";\nexport type { Callable, CallableResolvable } from \"./callable.lib\";\n","export { resolveNested } from \"./callable.lib\";\n","import { Resolvable } from \"../resolvable.lib\";\n\nexport type Callable<T> = T | (() => T);\nexport declare namespace Callable {\n export type Infer<T> = T extends () => infer U ? U : T;\n export type IsPromise<T> = T extends () => infer U\n ? Resolvable.IsPromise<U>\n : Resolvable.IsPromise<T>;\n}\n\nexport type CallableResolvable<T> = Callable<Resolvable<T>>;\n\nexport declare namespace CallableResolvable {\n export type Infer<T> = Resolvable.Infer<Callable.Infer<T>>;\n\n export type IsPromise<T> = Resolvable.IsPromise<Callable.Infer<T>>;\n\n export type Return<T> =\n true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;\n\n export type MaybeArray<T> =\n | CallableResolvable<T>\n | Array<CallableResolvable<T>>;\n\n export namespace MaybeArray {\n export type InferExact<T> = T extends readonly any[]\n ? { [K in keyof T]: CallableResolvable.Infer<T[K]> }\n : CallableResolvable.Infer<T>;\n export type Infer<T> = T extends readonly any[]\n ? Array<{ [K in keyof T]: CallableResolvable.Infer<T[K]> }[number]>\n : CallableResolvable.Infer<T>;\n export type IsPromise<T> = T extends readonly any[]\n ? { [K in keyof T]: CallableResolvable.IsPromise<T[K]> }[number]\n : CallableResolvable.IsPromise<T>;\n export type Return<T> =\n true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;\n\n export type ArrayOf<T> = Array<MaybeArray<T>>;\n export namespace ArrayOf {\n export type InferExact<T extends readonly any[]> = {\n [K in keyof T]: MaybeArray.InferExact<T[K]>;\n };\n export type Infer<T extends readonly any[]> = Array<\n {\n [K in keyof T]: MaybeArray.Infer<T[K]>;\n }[number]\n >;\n export type IsPromise<T extends readonly any[]> = {\n [K in keyof T]: MaybeArray.IsPromise<T[K]>;\n }[number];\n export type Return<T extends readonly any[]> =\n true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;\n }\n }\n}\n\nexport const resolveNested = <T extends readonly any[]>(\n args: T,\n): CallableResolvable.MaybeArray.ArrayOf.Return<T> => {\n // @ts-expect-error - Fix later\n if (args.length === 0) return [];\n\n const unresolvedValues = args.map((v) => {\n if (typeof v === \"function\") return v();\n if (Array.isArray(v))\n return v.map((sv) => (typeof sv === \"function\" ? sv() : sv));\n return v;\n });\n\n const isPromise = unresolvedValues.some((v) => {\n if (v instanceof Promise) return true;\n if (Array.isArray(v)) return v.some((v) => v instanceof Promise);\n return false;\n });\n\n // @ts-expect-error - We know that the values is not a promise\n if (!isPromise) return unresolvedValues;\n\n // @ts-expect-error - We know that the values is a promise\n return Promise.all(\n unresolvedValues.map((v) => (Array.isArray(v) ? Promise.all(v) : v)),\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;;;ACwDO,IAAM,gBAAgB,CAC3B,SACoD;AAEpD,MAAI,KAAK,WAAW,EAAG,QAAO,CAAC;AAE/B,QAAM,mBAAmB,KAAK,IAAI,CAAC,MAAM;AACvC,QAAI,OAAO,MAAM,WAAY,QAAO,EAAE;AACtC,QAAI,MAAM,QAAQ,CAAC;AACjB,aAAO,EAAE,IAAI,CAAC,OAAQ,OAAO,OAAO,aAAa,GAAG,IAAI,EAAG;AAC7D,WAAO;AAAA,EACT,CAAC;AAED,QAAM,YAAY,iBAAiB,KAAK,CAAC,MAAM;AAC7C,QAAI,aAAa,QAAS,QAAO;AACjC,QAAI,MAAM,QAAQ,CAAC,EAAG,QAAO,EAAE,KAAK,CAACA,OAAMA,cAAa,OAAO;AAC/D,WAAO;AAAA,EACT,CAAC;AAGD,MAAI,CAAC,UAAW,QAAO;AAGvB,SAAO,QAAQ;AAAA,IACb,iBAAiB,IAAI,CAAC,MAAO,MAAM,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,IAAI,CAAE;AAAA,EACrE;AACF;","names":["v"]}
@@ -0,0 +1,47 @@
1
+ import { R as Resolvable } from '../resolvable.lib-CU3Va7aC.cjs';
2
+
3
+ type Callable<T> = T | (() => T);
4
+ declare namespace Callable {
5
+ type Infer<T> = T extends () => infer U ? U : T;
6
+ type IsPromise<T> = T extends () => infer U ? Resolvable.IsPromise<U> : Resolvable.IsPromise<T>;
7
+ }
8
+ type CallableResolvable<T> = Callable<Resolvable<T>>;
9
+ declare namespace CallableResolvable {
10
+ type Infer<T> = Resolvable.Infer<Callable.Infer<T>>;
11
+ type IsPromise<T> = Resolvable.IsPromise<Callable.Infer<T>>;
12
+ type Return<T> = true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
13
+ type MaybeArray<T> = CallableResolvable<T> | Array<CallableResolvable<T>>;
14
+ namespace MaybeArray {
15
+ type InferExact<T> = T extends readonly any[] ? {
16
+ [K in keyof T]: CallableResolvable.Infer<T[K]>;
17
+ } : CallableResolvable.Infer<T>;
18
+ type Infer<T> = T extends readonly any[] ? Array<{
19
+ [K in keyof T]: CallableResolvable.Infer<T[K]>;
20
+ }[number]> : CallableResolvable.Infer<T>;
21
+ type IsPromise<T> = T extends readonly any[] ? {
22
+ [K in keyof T]: CallableResolvable.IsPromise<T[K]>;
23
+ }[number] : CallableResolvable.IsPromise<T>;
24
+ type Return<T> = true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
25
+ type ArrayOf<T> = Array<MaybeArray<T>>;
26
+ namespace ArrayOf {
27
+ type InferExact<T extends readonly any[]> = {
28
+ [K in keyof T]: MaybeArray.InferExact<T[K]>;
29
+ };
30
+ type Infer<T extends readonly any[]> = Array<{
31
+ [K in keyof T]: MaybeArray.Infer<T[K]>;
32
+ }[number]>;
33
+ type IsPromise<T extends readonly any[]> = {
34
+ [K in keyof T]: MaybeArray.IsPromise<T[K]>;
35
+ }[number];
36
+ type Return<T extends readonly any[]> = true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
37
+ }
38
+ }
39
+ }
40
+ declare const resolveNested: <T extends readonly any[]>(args: T) => CallableResolvable.MaybeArray.ArrayOf.Return<T>;
41
+
42
+ declare const callable_bundle_resolveNested: typeof resolveNested;
43
+ declare namespace callable_bundle {
44
+ export { callable_bundle_resolveNested as resolveNested };
45
+ }
46
+
47
+ export { Callable, CallableResolvable, callable_bundle as callable, callable_bundle as default, resolveNested };
@@ -0,0 +1,47 @@
1
+ import { R as Resolvable } from '../resolvable.lib-CU3Va7aC.js';
2
+
3
+ type Callable<T> = T | (() => T);
4
+ declare namespace Callable {
5
+ type Infer<T> = T extends () => infer U ? U : T;
6
+ type IsPromise<T> = T extends () => infer U ? Resolvable.IsPromise<U> : Resolvable.IsPromise<T>;
7
+ }
8
+ type CallableResolvable<T> = Callable<Resolvable<T>>;
9
+ declare namespace CallableResolvable {
10
+ type Infer<T> = Resolvable.Infer<Callable.Infer<T>>;
11
+ type IsPromise<T> = Resolvable.IsPromise<Callable.Infer<T>>;
12
+ type Return<T> = true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
13
+ type MaybeArray<T> = CallableResolvable<T> | Array<CallableResolvable<T>>;
14
+ namespace MaybeArray {
15
+ type InferExact<T> = T extends readonly any[] ? {
16
+ [K in keyof T]: CallableResolvable.Infer<T[K]>;
17
+ } : CallableResolvable.Infer<T>;
18
+ type Infer<T> = T extends readonly any[] ? Array<{
19
+ [K in keyof T]: CallableResolvable.Infer<T[K]>;
20
+ }[number]> : CallableResolvable.Infer<T>;
21
+ type IsPromise<T> = T extends readonly any[] ? {
22
+ [K in keyof T]: CallableResolvable.IsPromise<T[K]>;
23
+ }[number] : CallableResolvable.IsPromise<T>;
24
+ type Return<T> = true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
25
+ type ArrayOf<T> = Array<MaybeArray<T>>;
26
+ namespace ArrayOf {
27
+ type InferExact<T extends readonly any[]> = {
28
+ [K in keyof T]: MaybeArray.InferExact<T[K]>;
29
+ };
30
+ type Infer<T extends readonly any[]> = Array<{
31
+ [K in keyof T]: MaybeArray.Infer<T[K]>;
32
+ }[number]>;
33
+ type IsPromise<T extends readonly any[]> = {
34
+ [K in keyof T]: MaybeArray.IsPromise<T[K]>;
35
+ }[number];
36
+ type Return<T extends readonly any[]> = true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
37
+ }
38
+ }
39
+ }
40
+ declare const resolveNested: <T extends readonly any[]>(args: T) => CallableResolvable.MaybeArray.ArrayOf.Return<T>;
41
+
42
+ declare const callable_bundle_resolveNested: typeof resolveNested;
43
+ declare namespace callable_bundle {
44
+ export { callable_bundle_resolveNested as resolveNested };
45
+ }
46
+
47
+ export { Callable, CallableResolvable, callable_bundle as callable, callable_bundle as default, resolveNested };
@@ -0,0 +1,37 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/callable/callable.bundle.ts
8
+ var callable_bundle_exports = {};
9
+ __export(callable_bundle_exports, {
10
+ resolveNested: () => resolveNested
11
+ });
12
+
13
+ // src/callable/callable.lib.ts
14
+ var resolveNested = (args) => {
15
+ if (args.length === 0) return [];
16
+ const unresolvedValues = args.map((v) => {
17
+ if (typeof v === "function") return v();
18
+ if (Array.isArray(v))
19
+ return v.map((sv) => typeof sv === "function" ? sv() : sv);
20
+ return v;
21
+ });
22
+ const isPromise = unresolvedValues.some((v) => {
23
+ if (v instanceof Promise) return true;
24
+ if (Array.isArray(v)) return v.some((v2) => v2 instanceof Promise);
25
+ return false;
26
+ });
27
+ if (!isPromise) return unresolvedValues;
28
+ return Promise.all(
29
+ unresolvedValues.map((v) => Array.isArray(v) ? Promise.all(v) : v)
30
+ );
31
+ };
32
+ export {
33
+ callable_bundle_exports as callable,
34
+ callable_bundle_exports as default,
35
+ resolveNested
36
+ };
37
+ //# sourceMappingURL=callable.index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/callable/callable.bundle.ts","../../src/callable/callable.lib.ts"],"sourcesContent":["export { resolveNested } from \"./callable.lib\";\n","import { Resolvable } from \"../resolvable.lib\";\n\nexport type Callable<T> = T | (() => T);\nexport declare namespace Callable {\n export type Infer<T> = T extends () => infer U ? U : T;\n export type IsPromise<T> = T extends () => infer U\n ? Resolvable.IsPromise<U>\n : Resolvable.IsPromise<T>;\n}\n\nexport type CallableResolvable<T> = Callable<Resolvable<T>>;\n\nexport declare namespace CallableResolvable {\n export type Infer<T> = Resolvable.Infer<Callable.Infer<T>>;\n\n export type IsPromise<T> = Resolvable.IsPromise<Callable.Infer<T>>;\n\n export type Return<T> =\n true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;\n\n export type MaybeArray<T> =\n | CallableResolvable<T>\n | Array<CallableResolvable<T>>;\n\n export namespace MaybeArray {\n export type InferExact<T> = T extends readonly any[]\n ? { [K in keyof T]: CallableResolvable.Infer<T[K]> }\n : CallableResolvable.Infer<T>;\n export type Infer<T> = T extends readonly any[]\n ? Array<{ [K in keyof T]: CallableResolvable.Infer<T[K]> }[number]>\n : CallableResolvable.Infer<T>;\n export type IsPromise<T> = T extends readonly any[]\n ? { [K in keyof T]: CallableResolvable.IsPromise<T[K]> }[number]\n : CallableResolvable.IsPromise<T>;\n export type Return<T> =\n true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;\n\n export type ArrayOf<T> = Array<MaybeArray<T>>;\n export namespace ArrayOf {\n export type InferExact<T extends readonly any[]> = {\n [K in keyof T]: MaybeArray.InferExact<T[K]>;\n };\n export type Infer<T extends readonly any[]> = Array<\n {\n [K in keyof T]: MaybeArray.Infer<T[K]>;\n }[number]\n >;\n export type IsPromise<T extends readonly any[]> = {\n [K in keyof T]: MaybeArray.IsPromise<T[K]>;\n }[number];\n export type Return<T extends readonly any[]> =\n true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;\n }\n }\n}\n\nexport const resolveNested = <T extends readonly any[]>(\n args: T,\n): CallableResolvable.MaybeArray.ArrayOf.Return<T> => {\n // @ts-expect-error - Fix later\n if (args.length === 0) return [];\n\n const unresolvedValues = args.map((v) => {\n if (typeof v === \"function\") return v();\n if (Array.isArray(v))\n return v.map((sv) => (typeof sv === \"function\" ? sv() : sv));\n return v;\n });\n\n const isPromise = unresolvedValues.some((v) => {\n if (v instanceof Promise) return true;\n if (Array.isArray(v)) return v.some((v) => v instanceof Promise);\n return false;\n });\n\n // @ts-expect-error - We know that the values is not a promise\n if (!isPromise) return unresolvedValues;\n\n // @ts-expect-error - We know that the values is a promise\n return Promise.all(\n unresolvedValues.map((v) => (Array.isArray(v) ? Promise.all(v) : v)),\n );\n};\n"],"mappings":";;;;;;;AAAA;AAAA;AAAA;AAAA;;;ACwDO,IAAM,gBAAgB,CAC3B,SACoD;AAEpD,MAAI,KAAK,WAAW,EAAG,QAAO,CAAC;AAE/B,QAAM,mBAAmB,KAAK,IAAI,CAAC,MAAM;AACvC,QAAI,OAAO,MAAM,WAAY,QAAO,EAAE;AACtC,QAAI,MAAM,QAAQ,CAAC;AACjB,aAAO,EAAE,IAAI,CAAC,OAAQ,OAAO,OAAO,aAAa,GAAG,IAAI,EAAG;AAC7D,WAAO;AAAA,EACT,CAAC;AAED,QAAM,YAAY,iBAAiB,KAAK,CAAC,MAAM;AAC7C,QAAI,aAAa,QAAS,QAAO;AACjC,QAAI,MAAM,QAAQ,CAAC,EAAG,QAAO,EAAE,KAAK,CAACA,OAAMA,cAAa,OAAO;AAC/D,WAAO;AAAA,EACT,CAAC;AAGD,MAAI,CAAC,UAAW,QAAO;AAGvB,SAAO,QAAQ;AAAA,IACb,iBAAiB,IAAI,CAAC,MAAO,MAAM,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,IAAI,CAAE;AAAA,EACrE;AACF;","names":["v"]}
@@ -0,0 +1,63 @@
1
+ type Resolvable<T> = Promise<T> | T;
2
+ declare namespace Resolvable {
3
+ type Infer<T> = Awaited<T>;
4
+ type IsPromise<T> = T extends Promise<any> ? true : never;
5
+ namespace ArrayOf {
6
+ type Infer<T> = T extends readonly any[] ? {
7
+ [K in keyof T]: Resolvable.Infer<T[K]>;
8
+ } : never;
9
+ type HasPromise<T> = T extends readonly any[] ? {
10
+ [K in keyof T]: Resolvable.IsPromise<T[K]>;
11
+ }[number] : never;
12
+ }
13
+ namespace MaybeArray {
14
+ type Infer<T> = T extends readonly any[] ? {
15
+ [K in keyof T]: Resolvable.Infer<T[K]>;
16
+ } : Resolvable.Infer<T>;
17
+ type IsPromise<T> = T extends readonly any[] ? {
18
+ [K in keyof T]: Resolvable.IsPromise<T[K]>;
19
+ }[number] : Resolvable.IsPromise<T>;
20
+ }
21
+ }
22
+ /**
23
+ * Resolves all values in the array in parallel
24
+ * @param value The array to resolve
25
+ * @returns If the array contains promises, a promise of an array of values. Otherwise, the array.
26
+ */
27
+ declare const resolveAll: <U extends readonly any[]>(value: U) => true extends Resolvable.ArrayOf.HasPromise<U> ? Promise<Resolvable.ArrayOf.Infer<U>> : U;
28
+ declare class Resolver<T extends Resolvable<any>> {
29
+ private readonly _value;
30
+ constructor(value: T);
31
+ /**
32
+ * Get the value of the resolver
33
+ * @returns The value or a single promise of the value
34
+ *
35
+ * - If the value is an array containing promises, the array will be resolved with `Promise.all`
36
+ */
37
+ get $(): T extends readonly any[] ? true extends Resolvable.ArrayOf.HasPromise<T> ? Promise<Resolvable.ArrayOf.Infer<T>> : T : T;
38
+ valueOf(): T;
39
+ /**
40
+ * Apply a function to the value
41
+ * @param fn the function to apply to the value
42
+ * @returns a new Resolver instance with the result of the function, either a value or a promise of a value
43
+ */
44
+ _<R>(fn: (value: Resolvable.MaybeArray.Infer<T>) => R): true extends Resolvable.MaybeArray.IsPromise<T> ? true extends Resolvable.MaybeArray.IsPromise<R> ? Resolver<R> : Resolver<Promise<R>> : Resolver<R>;
45
+ }
46
+ /**
47
+ * A piping utility which preserves the sync/async state of the value
48
+ * @param value The value to pipe
49
+ * @returns A new Resolver instance
50
+ *
51
+ * ```ts
52
+ * import { pipe } from "@synstack/resolved";
53
+ *
54
+ * // Sync
55
+ * const value: string = pipe("Hello World")._((v) => v.toUpperCase()).$;
56
+ *
57
+ * // Async
58
+ * const promiseValue: Promise<string> = pipe("Hello World")._((v) => Promise.resolve(v.toUpperCase())).$;
59
+ * ```
60
+ */
61
+ declare const pipe: <T>(value: T) => Resolver<T>;
62
+
63
+ export { Resolvable as R, Resolver as a, pipe as p, resolveAll as r };
@@ -0,0 +1,63 @@
1
+ type Resolvable<T> = Promise<T> | T;
2
+ declare namespace Resolvable {
3
+ type Infer<T> = Awaited<T>;
4
+ type IsPromise<T> = T extends Promise<any> ? true : never;
5
+ namespace ArrayOf {
6
+ type Infer<T> = T extends readonly any[] ? {
7
+ [K in keyof T]: Resolvable.Infer<T[K]>;
8
+ } : never;
9
+ type HasPromise<T> = T extends readonly any[] ? {
10
+ [K in keyof T]: Resolvable.IsPromise<T[K]>;
11
+ }[number] : never;
12
+ }
13
+ namespace MaybeArray {
14
+ type Infer<T> = T extends readonly any[] ? {
15
+ [K in keyof T]: Resolvable.Infer<T[K]>;
16
+ } : Resolvable.Infer<T>;
17
+ type IsPromise<T> = T extends readonly any[] ? {
18
+ [K in keyof T]: Resolvable.IsPromise<T[K]>;
19
+ }[number] : Resolvable.IsPromise<T>;
20
+ }
21
+ }
22
+ /**
23
+ * Resolves all values in the array in parallel
24
+ * @param value The array to resolve
25
+ * @returns If the array contains promises, a promise of an array of values. Otherwise, the array.
26
+ */
27
+ declare const resolveAll: <U extends readonly any[]>(value: U) => true extends Resolvable.ArrayOf.HasPromise<U> ? Promise<Resolvable.ArrayOf.Infer<U>> : U;
28
+ declare class Resolver<T extends Resolvable<any>> {
29
+ private readonly _value;
30
+ constructor(value: T);
31
+ /**
32
+ * Get the value of the resolver
33
+ * @returns The value or a single promise of the value
34
+ *
35
+ * - If the value is an array containing promises, the array will be resolved with `Promise.all`
36
+ */
37
+ get $(): T extends readonly any[] ? true extends Resolvable.ArrayOf.HasPromise<T> ? Promise<Resolvable.ArrayOf.Infer<T>> : T : T;
38
+ valueOf(): T;
39
+ /**
40
+ * Apply a function to the value
41
+ * @param fn the function to apply to the value
42
+ * @returns a new Resolver instance with the result of the function, either a value or a promise of a value
43
+ */
44
+ _<R>(fn: (value: Resolvable.MaybeArray.Infer<T>) => R): true extends Resolvable.MaybeArray.IsPromise<T> ? true extends Resolvable.MaybeArray.IsPromise<R> ? Resolver<R> : Resolver<Promise<R>> : Resolver<R>;
45
+ }
46
+ /**
47
+ * A piping utility which preserves the sync/async state of the value
48
+ * @param value The value to pipe
49
+ * @returns A new Resolver instance
50
+ *
51
+ * ```ts
52
+ * import { pipe } from "@synstack/resolved";
53
+ *
54
+ * // Sync
55
+ * const value: string = pipe("Hello World")._((v) => v.toUpperCase()).$;
56
+ *
57
+ * // Async
58
+ * const promiseValue: Promise<string> = pipe("Hello World")._((v) => Promise.resolve(v.toUpperCase())).$;
59
+ * ```
60
+ */
61
+ declare const pipe: <T>(value: T) => Resolver<T>;
62
+
63
+ export { Resolvable as R, Resolver as a, pipe as p, resolveAll as r };
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/resolved.index.ts
21
+ var resolved_index_exports = {};
22
+ __export(resolved_index_exports, {
23
+ pipe: () => pipe,
24
+ resolvable: () => resolved_bundle_exports,
25
+ resolveAll: () => resolveAll
26
+ });
27
+ module.exports = __toCommonJS(resolved_index_exports);
28
+
29
+ // src/resolved.bundle.ts
30
+ var resolved_bundle_exports = {};
31
+ __export(resolved_bundle_exports, {
32
+ pipe: () => pipe,
33
+ resolveAll: () => resolveAll
34
+ });
35
+
36
+ // src/resolvable.lib.ts
37
+ var resolveAll = (value) => {
38
+ if (!Array.isArray(value)) throw new Error("Expected an array");
39
+ if (value.some((v) => v instanceof Promise)) return Promise.all(value);
40
+ return value;
41
+ };
42
+ var Resolver = class _Resolver {
43
+ _value;
44
+ constructor(value) {
45
+ this._value = value;
46
+ }
47
+ /**
48
+ * Get the value of the resolver
49
+ * @returns The value or a single promise of the value
50
+ *
51
+ * - If the value is an array containing promises, the array will be resolved with `Promise.all`
52
+ */
53
+ get $() {
54
+ if (Array.isArray(this._value)) {
55
+ return resolveAll(this._value);
56
+ }
57
+ return this._value;
58
+ }
59
+ valueOf() {
60
+ return this._value;
61
+ }
62
+ /**
63
+ * Apply a function to the value
64
+ * @param fn the function to apply to the value
65
+ * @returns a new Resolver instance with the result of the function, either a value or a promise of a value
66
+ */
67
+ _(fn) {
68
+ if (Array.isArray(this._value)) {
69
+ const hasPromise = this._value.some((v) => v instanceof Promise);
70
+ if (hasPromise)
71
+ return new _Resolver(Promise.all(this._value).then(fn));
72
+ return new _Resolver(fn(this._value));
73
+ }
74
+ if (this._value instanceof Promise)
75
+ return new _Resolver(this._value.then(fn));
76
+ return new _Resolver(fn(this._value));
77
+ }
78
+ };
79
+ var pipe = (value) => {
80
+ return new Resolver(value);
81
+ };
82
+ // Annotate the CommonJS export names for ESM import in node:
83
+ 0 && (module.exports = {
84
+ pipe,
85
+ resolvable,
86
+ resolveAll
87
+ });
88
+ //# sourceMappingURL=resolved.index.cjs.map
@@ -0,0 +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\";\nexport * from \"./resolved.bundle\";\nexport * as resolvable from \"./resolved.bundle\";\n","export { pipe, resolveAll } from \"./resolvable.lib\";\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":[]}
@@ -0,0 +1,10 @@
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';
3
+
4
+ declare const resolved_bundle_pipe: typeof pipe;
5
+ declare const resolved_bundle_resolveAll: typeof resolveAll;
6
+ declare namespace resolved_bundle {
7
+ export { resolved_bundle_pipe as pipe, resolved_bundle_resolveAll as resolveAll };
8
+ }
9
+
10
+ export { pipe, resolved_bundle as resolvable, resolveAll };
@@ -0,0 +1,10 @@
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';
3
+
4
+ declare const resolved_bundle_pipe: typeof pipe;
5
+ declare const resolved_bundle_resolveAll: typeof resolveAll;
6
+ declare namespace resolved_bundle {
7
+ export { resolved_bundle_pipe as pipe, resolved_bundle_resolveAll as resolveAll };
8
+ }
9
+
10
+ export { pipe, resolved_bundle as resolvable, resolveAll };
@@ -0,0 +1,65 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/resolved.bundle.ts
8
+ var resolved_bundle_exports = {};
9
+ __export(resolved_bundle_exports, {
10
+ pipe: () => pipe,
11
+ resolveAll: () => resolveAll
12
+ });
13
+
14
+ // src/resolvable.lib.ts
15
+ var resolveAll = (value) => {
16
+ if (!Array.isArray(value)) throw new Error("Expected an array");
17
+ if (value.some((v) => v instanceof Promise)) return Promise.all(value);
18
+ return value;
19
+ };
20
+ var Resolver = class _Resolver {
21
+ _value;
22
+ constructor(value) {
23
+ this._value = value;
24
+ }
25
+ /**
26
+ * Get the value of the resolver
27
+ * @returns The value or a single promise of the value
28
+ *
29
+ * - If the value is an array containing promises, the array will be resolved with `Promise.all`
30
+ */
31
+ get $() {
32
+ if (Array.isArray(this._value)) {
33
+ return resolveAll(this._value);
34
+ }
35
+ return this._value;
36
+ }
37
+ valueOf() {
38
+ return this._value;
39
+ }
40
+ /**
41
+ * Apply a function to the value
42
+ * @param fn the function to apply to the value
43
+ * @returns a new Resolver instance with the result of the function, either a value or a promise of a value
44
+ */
45
+ _(fn) {
46
+ if (Array.isArray(this._value)) {
47
+ const hasPromise = this._value.some((v) => v instanceof Promise);
48
+ if (hasPromise)
49
+ return new _Resolver(Promise.all(this._value).then(fn));
50
+ return new _Resolver(fn(this._value));
51
+ }
52
+ if (this._value instanceof Promise)
53
+ return new _Resolver(this._value.then(fn));
54
+ return new _Resolver(fn(this._value));
55
+ }
56
+ };
57
+ var pipe = (value) => {
58
+ return new Resolver(value);
59
+ };
60
+ export {
61
+ pipe,
62
+ resolved_bundle_exports as resolvable,
63
+ resolveAll
64
+ };
65
+ //# sourceMappingURL=resolved.index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/resolved.bundle.ts","../src/resolvable.lib.ts"],"sourcesContent":["export { pipe, resolveAll } from \"./resolvable.lib\";\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":[]}
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@synstack/resolved",
3
+ "type": "module",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "packageManager": "yarn@4.4.0",
8
+ "version": "1.0.0",
9
+ "author": {
10
+ "name": "pAIrprog",
11
+ "url": "https://pairprog.io"
12
+ },
13
+ "homepage": "https://github.com/pAIrprogio/synscript/tree/main/packages/resolved",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/pAIrprogio/syn-stack.git",
17
+ "directory": "packages/resolved"
18
+ },
19
+ "license": "Apache-2.0",
20
+ "scripts": {
21
+ "publish": "yarn npm publish --access public",
22
+ "prepublish": "yarn test && yarn build",
23
+ "build": "tsup",
24
+ "build:watch": "tsup --watch",
25
+ "test:types": "tsc --noEmit",
26
+ "test:unit": "node --import tsx --test src/**/*.test.ts",
27
+ "test:unit:watch": "node --import tsx --watch --test src/**/*.test.ts",
28
+ "test": "yarn test:types && yarn test:unit"
29
+ },
30
+ "exports": {
31
+ ".": {
32
+ "import": {
33
+ "types": "./dist/resolved.index.d.ts",
34
+ "default": "./dist/resolved.index.js"
35
+ },
36
+ "require": {
37
+ "types": "./dist/resolved.index.d.cts",
38
+ "default": "./dist/resolved.index.cjs"
39
+ }
40
+ },
41
+ "./callable": {
42
+ "import": {
43
+ "types": "./src/callable/callable.index.ts",
44
+ "default": "./dist/callable/callable.index.js"
45
+ },
46
+ "require": {
47
+ "types": "./dist/callable/callable.index.d.cts",
48
+ "default": "./dist/callable/callable.index.cjs"
49
+ }
50
+ }
51
+ },
52
+ "devDependencies": {
53
+ "@types/node": "^22.7.0",
54
+ "tsup": "^8.3.0",
55
+ "tsx": "^4.19.1",
56
+ "typescript": "^5.6.2"
57
+ },
58
+ "files": [
59
+ "src/**/*.ts",
60
+ "!src/**/*.test.ts",
61
+ "dist/**/*"
62
+ ],
63
+ "gitHead": "c668ecfd7fe387e978a68324e760d1ed13182d11"
64
+ }
@@ -0,0 +1 @@
1
+ export { resolveNested } from "./callable.lib";
@@ -0,0 +1 @@
1
+ export * as default from "./callable.bundle";
@@ -0,0 +1,4 @@
1
+ export * as callable from "./callable.bundle";
2
+ export { default } from "./callable.default";
3
+ export { resolveNested } from "./callable.lib";
4
+ export type { Callable, CallableResolvable } from "./callable.lib";
@@ -0,0 +1,83 @@
1
+ import { Resolvable } from "../resolvable.lib";
2
+
3
+ export type Callable<T> = T | (() => T);
4
+ export declare namespace Callable {
5
+ export type Infer<T> = T extends () => infer U ? U : T;
6
+ export type IsPromise<T> = T extends () => infer U
7
+ ? Resolvable.IsPromise<U>
8
+ : Resolvable.IsPromise<T>;
9
+ }
10
+
11
+ export type CallableResolvable<T> = Callable<Resolvable<T>>;
12
+
13
+ export declare namespace CallableResolvable {
14
+ export type Infer<T> = Resolvable.Infer<Callable.Infer<T>>;
15
+
16
+ export type IsPromise<T> = Resolvable.IsPromise<Callable.Infer<T>>;
17
+
18
+ export type Return<T> =
19
+ true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
20
+
21
+ export type MaybeArray<T> =
22
+ | CallableResolvable<T>
23
+ | Array<CallableResolvable<T>>;
24
+
25
+ export namespace MaybeArray {
26
+ export type InferExact<T> = T extends readonly any[]
27
+ ? { [K in keyof T]: CallableResolvable.Infer<T[K]> }
28
+ : CallableResolvable.Infer<T>;
29
+ export type Infer<T> = T extends readonly any[]
30
+ ? Array<{ [K in keyof T]: CallableResolvable.Infer<T[K]> }[number]>
31
+ : CallableResolvable.Infer<T>;
32
+ export type IsPromise<T> = T extends readonly any[]
33
+ ? { [K in keyof T]: CallableResolvable.IsPromise<T[K]> }[number]
34
+ : CallableResolvable.IsPromise<T>;
35
+ export type Return<T> =
36
+ true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
37
+
38
+ export type ArrayOf<T> = Array<MaybeArray<T>>;
39
+ export namespace ArrayOf {
40
+ export type InferExact<T extends readonly any[]> = {
41
+ [K in keyof T]: MaybeArray.InferExact<T[K]>;
42
+ };
43
+ export type Infer<T extends readonly any[]> = Array<
44
+ {
45
+ [K in keyof T]: MaybeArray.Infer<T[K]>;
46
+ }[number]
47
+ >;
48
+ export type IsPromise<T extends readonly any[]> = {
49
+ [K in keyof T]: MaybeArray.IsPromise<T[K]>;
50
+ }[number];
51
+ export type Return<T extends readonly any[]> =
52
+ true extends IsPromise<T> ? Promise<Infer<T>> : Infer<T>;
53
+ }
54
+ }
55
+ }
56
+
57
+ export const resolveNested = <T extends readonly any[]>(
58
+ args: T,
59
+ ): CallableResolvable.MaybeArray.ArrayOf.Return<T> => {
60
+ // @ts-expect-error - Fix later
61
+ if (args.length === 0) return [];
62
+
63
+ const unresolvedValues = args.map((v) => {
64
+ if (typeof v === "function") return v();
65
+ if (Array.isArray(v))
66
+ return v.map((sv) => (typeof sv === "function" ? sv() : sv));
67
+ return v;
68
+ });
69
+
70
+ const isPromise = unresolvedValues.some((v) => {
71
+ if (v instanceof Promise) return true;
72
+ if (Array.isArray(v)) return v.some((v) => v instanceof Promise);
73
+ return false;
74
+ });
75
+
76
+ // @ts-expect-error - We know that the values is not a promise
77
+ if (!isPromise) return unresolvedValues;
78
+
79
+ // @ts-expect-error - We know that the values is a promise
80
+ return Promise.all(
81
+ unresolvedValues.map((v) => (Array.isArray(v) ? Promise.all(v) : v)),
82
+ );
83
+ };
@@ -0,0 +1,124 @@
1
+ export type Resolvable<T> = Promise<T> | T;
2
+
3
+ export declare namespace Resolvable {
4
+ export type Infer<T> = Awaited<T>;
5
+ export type IsPromise<T> = T extends Promise<any> ? true : never;
6
+
7
+ namespace ArrayOf {
8
+ export type Infer<T> = T extends readonly any[]
9
+ ? {
10
+ [K in keyof T]: Resolvable.Infer<T[K]>;
11
+ }
12
+ : never;
13
+
14
+ export type HasPromise<T> = T extends readonly any[]
15
+ ? {
16
+ [K in keyof T]: Resolvable.IsPromise<T[K]>;
17
+ }[number]
18
+ : never;
19
+ }
20
+
21
+ namespace MaybeArray {
22
+ export type Infer<T> = T extends readonly any[]
23
+ ? { [K in keyof T]: Resolvable.Infer<T[K]> }
24
+ : Resolvable.Infer<T>;
25
+
26
+ export type IsPromise<T> = T extends readonly any[]
27
+ ? { [K in keyof T]: Resolvable.IsPromise<T[K]> }[number]
28
+ : Resolvable.IsPromise<T>;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Resolves all values in the array in parallel
34
+ * @param value The array to resolve
35
+ * @returns If the array contains promises, a promise of an array of values. Otherwise, the array.
36
+ */
37
+ export const resolveAll = <U extends readonly any[]>(
38
+ value: U,
39
+ ): true extends Resolvable.ArrayOf.HasPromise<U>
40
+ ? Promise<Resolvable.ArrayOf.Infer<U>>
41
+ : U => {
42
+ if (!Array.isArray(value)) throw new Error("Expected an array");
43
+ // @ts-expect-error - We know that the value is not a promise
44
+ if (value.some((v) => v instanceof Promise)) return Promise.all(value);
45
+ // @ts-expect-error - We know that the value is not a promise
46
+ return value;
47
+ };
48
+
49
+ export class Resolver<T extends Resolvable<any>> {
50
+ private readonly _value: T;
51
+
52
+ public constructor(value: T) {
53
+ this._value = value;
54
+ }
55
+
56
+ /**
57
+ * Get the value of the resolver
58
+ * @returns The value or a single promise of the value
59
+ *
60
+ * - If the value is an array containing promises, the array will be resolved with `Promise.all`
61
+ */
62
+ public get $(): T extends readonly any[]
63
+ ? true extends Resolvable.ArrayOf.HasPromise<T>
64
+ ? Promise<Resolvable.ArrayOf.Infer<T>>
65
+ : T
66
+ : T {
67
+ if (Array.isArray(this._value)) {
68
+ // @ts-expect-error - We know that the value is an array
69
+ return resolveAll(this._value);
70
+ }
71
+ // @ts-expect-error - We know that the value is not an array
72
+ return this._value;
73
+ }
74
+
75
+ public valueOf(): T {
76
+ return this._value;
77
+ }
78
+
79
+ /**
80
+ * Apply a function to the value
81
+ * @param fn the function to apply to the value
82
+ * @returns a new Resolver instance with the result of the function, either a value or a promise of a value
83
+ */
84
+ public _<R>(
85
+ fn: (value: Resolvable.MaybeArray.Infer<T>) => R,
86
+ ): true extends Resolvable.MaybeArray.IsPromise<T>
87
+ ? true extends Resolvable.MaybeArray.IsPromise<R>
88
+ ? Resolver<R>
89
+ : Resolver<Promise<R>>
90
+ : Resolver<R> {
91
+ if (Array.isArray(this._value)) {
92
+ const hasPromise = this._value.some((v) => v instanceof Promise);
93
+ if (hasPromise)
94
+ // @ts-expect-error - We know that the value is a promise
95
+ return new Resolver(Promise.all(this._value).then(fn));
96
+ // @ts-expect-error - We know that the value is not a promise
97
+ return new Resolver(fn(this._value));
98
+ }
99
+ if (this._value instanceof Promise)
100
+ // @ts-expect-error - We know that the value is a promise
101
+ return new Resolver(this._value.then(fn));
102
+ // @ts-expect-error - We know that the value is not a promise
103
+ return new Resolver(fn(this._value as Resolvable.Infer<T>));
104
+ }
105
+ }
106
+
107
+ /**
108
+ * A piping utility which preserves the sync/async state of the value
109
+ * @param value The value to pipe
110
+ * @returns A new Resolver instance
111
+ *
112
+ * ```ts
113
+ * import { pipe } from "@synstack/resolved";
114
+ *
115
+ * // Sync
116
+ * const value: string = pipe("Hello World")._((v) => v.toUpperCase()).$;
117
+ *
118
+ * // Async
119
+ * const promiseValue: Promise<string> = pipe("Hello World")._((v) => Promise.resolve(v.toUpperCase())).$;
120
+ * ```
121
+ */
122
+ export const pipe = <T>(value: T) => {
123
+ return new Resolver<T>(value);
124
+ };
@@ -0,0 +1 @@
1
+ export { pipe, resolveAll } from "./resolvable.lib";
@@ -0,0 +1,3 @@
1
+ export type { Resolvable, Resolver } from "./resolvable.lib";
2
+ export * from "./resolved.bundle";
3
+ export * as resolvable from "./resolved.bundle";