@xylabs/promise 5.1.1 → 5.1.3

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,90 +1,7 @@
1
- import { TypedValue } from '@xylabs/typeof';
2
-
3
- /**
4
- * For use with Promise.allSettled to filter only successful results
5
- * @param val
6
- * @returns
7
- */
8
- declare const fulfilled: <T>(val: PromiseSettledResult<T>) => val is PromiseFulfilledResult<T>;
9
-
10
- /**
11
- * For use with Promise.allSettled to reduce to only successful result values
12
- * @example <caption>Casting the initialValue provided to reduce</caption>
13
- * const resolved = Promise.resolve('resolved')
14
- * const rejected = Promise.reject('rejected')
15
- * const settled = await Promise.allSettled([resolved, rejected])
16
- * const results = settled.reduce(fulfilledValues, [] as string[])
17
- * // results === [ 'resolved' ]
18
- * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>
19
- * const resolved = Promise.resolve('resolved')
20
- * const rejected = Promise.reject('rejected')
21
- * const settled = await Promise.allSettled([resolved, rejected])
22
- * const results = settled.reduce<string[]>(fulfilledValues, [])
23
- * // results === [ 'resolved' ]
24
- * @param previousValue
25
- * @param currentValue
26
- * @returns
27
- */
28
- declare const fulfilledValues: <T>(previousValue: T[], currentValue: PromiseSettledResult<T>) => T[];
29
-
30
- /** A resolve/reject callback used within PromiseEx. */
31
- type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult;
32
- /** The executor function passed to the PromiseEx constructor. */
33
- type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void;
34
- /** A callback that inspects the attached value and returns whether to cancel the promise. */
35
- type PromiseExValueFunc<V> = (value?: V) => boolean;
36
- /**
37
- * An extended Promise that carries an optional attached value and supports cancellation.
38
- * The value can be inspected via the `then` or `value` methods to conditionally cancel.
39
- */
40
- declare class PromiseEx<T, V = void> extends Promise<T> {
41
- /** Whether the promise has been cancelled via a value callback. */
42
- cancelled?: boolean;
43
- private _value?;
44
- constructor(func: PromiseExFunc<T>, value?: V);
45
- then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined, onvalue?: (value?: V) => boolean): Promise<TResult1 | TResult2>;
46
- /**
47
- * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.
48
- * @param onvalue - A callback that receives the attached value and returns whether to cancel.
49
- * @returns This instance for chaining.
50
- */
51
- value(onvalue?: (value?: V) => boolean): this;
52
- }
53
-
54
- /**
55
- * For use with Promise.allSettled to filter only rejected results
56
- * @param val
57
- * @returns
58
- */
59
- declare const rejected: <T>(val: PromiseSettledResult<T>) => val is PromiseRejectedResult;
60
-
61
- /** A value that may be a Promise, PromiseEx, or a plain synchronous value. */
62
- type Promisable<T, V = never> = PromiseEx<T, V> | Promise<T> | T;
63
- /** A Promisable that resolves to an array. */
64
- type PromisableArray<T, V = never> = Promisable<T[], V>;
65
- /** A Promisable that may resolve to undefined. */
66
- type OptionalPromisable<T, V = never> = Promisable<T | undefined, V>;
67
- /** A Promisable array where elements may be undefined. */
68
- type OptionalPromisableArray<T, V = never> = PromisableArray<T | undefined, V>;
69
- /** A Promisable that may resolve to null. */
70
- type NullablePromisable<T, V = never> = Promisable<T | null, V>;
71
- /** A Promisable array where elements may be null. */
72
- type NullablePromisableArray<T, V = never> = PromisableArray<T | null, V>;
73
- /** @description Used to document promises that are being used as Mutexes */
74
- type AsyncMutex<T> = Promise<T>;
75
- /** An interface representing any thenable (promise-like) object. */
76
- interface PromiseType {
77
- then: () => unknown;
78
- }
79
- /** Any non-promise typed value, excluding thenables. */
80
- type AnyNonPromise = Exclude<TypedValue, PromiseType>;
81
-
82
- /**
83
- * Wraps a value in a Promise if it is not already one.
84
- * @param value - A value that may or may not be a Promise.
85
- * @returns A Promise resolving to the value.
86
- */
87
- declare function toPromise<T>(value: Promisable<T>): Promise<T>;
88
-
89
- export { PromiseEx, fulfilled, fulfilledValues, rejected, toPromise };
90
- export type { AnyNonPromise, AsyncMutex, NullablePromisable, NullablePromisableArray, OptionalPromisable, OptionalPromisableArray, Promisable, PromisableArray, PromiseExFunc, PromiseExSubFunc, PromiseExValueFunc, PromiseType };
1
+ export { fulfilled } from './fulfilled.ts';
2
+ export { fulfilledValues } from './fulfilledValues.ts';
3
+ export * from './PromiseEx.ts';
4
+ export { rejected } from './rejected.ts';
5
+ export * from './toPromise.ts';
6
+ export * from './types.ts';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -54,4 +54,4 @@ export {
54
54
  rejected,
55
55
  toPromise
56
56
  };
57
- //# sourceMappingURL=index.mjs.map
57
+ //# sourceMappingURL=index.mjs.map
@@ -1 +1,7 @@
1
- {"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/toPromise.ts"],"sourcesContent":["/**\n * For use with Promise.allSettled to filter only successful results\n * @param val\n * @returns\n */\nexport const fulfilled = <T>(val: PromiseSettledResult<T>): val is PromiseFulfilledResult<T> => {\n return val.status === 'fulfilled'\n}\n","/**\n * For use with Promise.allSettled to reduce to only successful result values\n * @example <caption>Casting the initialValue provided to reduce</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce(fulfilledValues, [] as string[])\n * // results === [ 'resolved' ]\n * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce<string[]>(fulfilledValues, [])\n * // results === [ 'resolved' ]\n * @param previousValue\n * @param currentValue\n * @returns\n */\nexport const fulfilledValues = <T>(previousValue: T[], currentValue: PromiseSettledResult<T>): T[] => {\n if (currentValue.status === 'fulfilled') previousValue.push(currentValue.value)\n return previousValue\n}\n","/** A resolve/reject callback used within PromiseEx. */\nexport type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult\n\n/** The executor function passed to the PromiseEx constructor. */\nexport type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void\n\n/** A callback that inspects the attached value and returns whether to cancel the promise. */\nexport type PromiseExValueFunc<V> = (value?: V) => boolean\n\n/**\n * An extended Promise that carries an optional attached value and supports cancellation.\n * The value can be inspected via the `then` or `value` methods to conditionally cancel.\n */\nexport class PromiseEx<T, V = void> extends Promise<T> {\n /** Whether the promise has been cancelled via a value callback. */\n cancelled?: boolean\n private _value?: V\n\n constructor(func: PromiseExFunc<T>, value?: V) {\n super(func)\n this._value = value\n }\n\n // eslint-disable-next-line unicorn/no-thenable\n override then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n onvalue?: (value?: V) => boolean,\n ): Promise<TResult1 | TResult2> {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return super.then(onfulfilled, onrejected)\n }\n\n /**\n * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.\n * @param onvalue - A callback that receives the attached value and returns whether to cancel.\n * @returns This instance for chaining.\n */\n value(onvalue?: (value?: V) => boolean) {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return this\n }\n}\n","/**\n * For use with Promise.allSettled to filter only rejected results\n * @param val\n * @returns\n */\nexport const rejected = <T>(val: PromiseSettledResult<T>): val is PromiseRejectedResult => {\n return val.status === 'rejected'\n}\n","import type { Promisable } from './types.ts'\n\n/**\n * Wraps a value in a Promise if it is not already one.\n * @param value - A value that may or may not be a Promise.\n * @returns A Promise resolving to the value.\n */\nexport function toPromise<T>(value: Promisable<T>): Promise<T> {\n return value instanceof Promise ? value : Promise.resolve(value)\n}\n"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW,YAAa,eAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACRO,IAAM,YAAN,cAAqC,QAAW;AAAA;AAAA,EAErD;AAAA,EACQ;AAAA,EAER,YAAY,MAAwB,OAAW;AAC7C,UAAM,IAAI;AACV,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA,EAGS,KACP,aACA,YACA,SAC8B;AAC9B,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,SAAkC;AACtC,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;ACzCO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACAO,SAAS,UAAa,OAAkC;AAC7D,SAAO,iBAAiB,UAAU,QAAQ,QAAQ,QAAQ,KAAK;AACjE;","names":[]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/fulfilled.ts", "../../src/fulfilledValues.ts", "../../src/PromiseEx.ts", "../../src/rejected.ts", "../../src/toPromise.ts"],
4
+ "sourcesContent": ["/**\n * For use with Promise.allSettled to filter only successful results\n * @param val\n * @returns\n */\nexport const fulfilled = <T>(val: PromiseSettledResult<T>): val is PromiseFulfilledResult<T> => {\n return val.status === 'fulfilled'\n}\n", "/**\n * For use with Promise.allSettled to reduce to only successful result values\n * @example <caption>Casting the initialValue provided to reduce</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce(fulfilledValues, [] as string[])\n * // results === [ 'resolved' ]\n * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce<string[]>(fulfilledValues, [])\n * // results === [ 'resolved' ]\n * @param previousValue\n * @param currentValue\n * @returns\n */\nexport const fulfilledValues = <T>(previousValue: T[], currentValue: PromiseSettledResult<T>): T[] => {\n if (currentValue.status === 'fulfilled') previousValue.push(currentValue.value)\n return previousValue\n}\n", "/** A resolve/reject callback used within PromiseEx. */\nexport type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult\n\n/** The executor function passed to the PromiseEx constructor. */\nexport type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void\n\n/** A callback that inspects the attached value and returns whether to cancel the promise. */\nexport type PromiseExValueFunc<V> = (value?: V) => boolean\n\n/**\n * An extended Promise that carries an optional attached value and supports cancellation.\n * The value can be inspected via the `then` or `value` methods to conditionally cancel.\n */\nexport class PromiseEx<T, V = void> extends Promise<T> {\n /** Whether the promise has been cancelled via a value callback. */\n cancelled?: boolean\n private _value?: V\n\n constructor(func: PromiseExFunc<T>, value?: V) {\n super(func)\n this._value = value\n }\n\n // eslint-disable-next-line unicorn/no-thenable\n override then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n onvalue?: (value?: V) => boolean,\n ): Promise<TResult1 | TResult2> {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return super.then(onfulfilled, onrejected)\n }\n\n /**\n * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.\n * @param onvalue - A callback that receives the attached value and returns whether to cancel.\n * @returns This instance for chaining.\n */\n value(onvalue?: (value?: V) => boolean) {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return this\n }\n}\n", "/**\n * For use with Promise.allSettled to filter only rejected results\n * @param val\n * @returns\n */\nexport const rejected = <T>(val: PromiseSettledResult<T>): val is PromiseRejectedResult => {\n return val.status === 'rejected'\n}\n", "import type { Promisable } from './types.ts'\n\n/**\n * Wraps a value in a Promise if it is not already one.\n * @param value - A value that may or may not be a Promise.\n * @returns A Promise resolving to the value.\n */\nexport function toPromise<T>(value: Promisable<T>): Promise<T> {\n return value instanceof Promise ? value : Promise.resolve(value)\n}\n"],
5
+ "mappings": ";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW,YAAa,eAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACRO,IAAM,YAAN,cAAqC,QAAW;AAAA;AAAA,EAErD;AAAA,EACQ;AAAA,EAER,YAAY,MAAwB,OAAW;AAC7C,UAAM,IAAI;AACV,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA,EAGS,KACP,aACA,YACA,SAC8B;AAC9B,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,SAAkC;AACtC,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;ACzCO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACAO,SAAS,UAAa,OAAkC;AAC7D,SAAO,iBAAiB,UAAU,QAAQ,QAAQ,QAAQ,KAAK;AACjE;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/promise",
3
- "version": "5.1.1",
3
+ "version": "5.1.3",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "promise",
@@ -41,16 +41,16 @@
41
41
  "README.md"
42
42
  ],
43
43
  "dependencies": {
44
- "@xylabs/typeof": "~5.1.1"
44
+ "@xylabs/typeof": "~5.1.3"
45
45
  },
46
46
  "devDependencies": {
47
- "@xylabs/toolchain": "~7.13.2",
48
- "@xylabs/tsconfig": "~7.13.2",
47
+ "@xylabs/toolchain": "~8.0.4",
48
+ "@xylabs/tsconfig": "~8.0.4",
49
49
  "eslint": "^10.3.0",
50
- "typescript": "^5.9.3",
51
- "vite": "^8.0.10",
52
- "vitest": "^4.1.5",
53
- "@xylabs/vitest-extended": "~5.1.1"
50
+ "typescript": "^6.0.3",
51
+ "vite": "^8.0.13",
52
+ "vitest": "^4.1.6",
53
+ "@xylabs/vitest-extended": "~5.1.3"
54
54
  },
55
55
  "engines": {
56
56
  "node": ">=18"