@xylabs/promise 4.3.4 → 4.3.5
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.
- package/dist/neutral/index.mjs +1 -25
- package/dist/neutral/index.mjs.map +1 -1
- package/package.json +8 -5
- package/src/Typed.ts +1 -1
- package/dist/neutral/isType.d.ts +0 -4
- package/dist/neutral/isType.d.ts.map +0 -1
- package/src/isType.ts +0 -29
package/dist/neutral/index.mjs
CHANGED
|
@@ -45,32 +45,8 @@ var rejected = (val) => {
|
|
|
45
45
|
return val.status === "rejected";
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
// src/isType.ts
|
|
49
|
-
var isType = (value, expectedType) => {
|
|
50
|
-
const typeofValue = typeof value;
|
|
51
|
-
switch (expectedType) {
|
|
52
|
-
case "array": {
|
|
53
|
-
return Array.isArray(value);
|
|
54
|
-
}
|
|
55
|
-
case "null": {
|
|
56
|
-
return value === null;
|
|
57
|
-
}
|
|
58
|
-
case "undefined": {
|
|
59
|
-
return value === void 0;
|
|
60
|
-
}
|
|
61
|
-
case "object": {
|
|
62
|
-
if (value === null) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
return typeofValue === "object" && !Array.isArray(value);
|
|
66
|
-
}
|
|
67
|
-
default: {
|
|
68
|
-
return typeofValue === expectedType;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
48
|
// src/Typed.ts
|
|
49
|
+
import { isType } from "@xylabs/typeof";
|
|
74
50
|
var isTypedKey = (value) => {
|
|
75
51
|
switch (typeof value) {
|
|
76
52
|
case "string":
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/isPromise.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/isPromise.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/Typed.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","import type { TypedValue } from './Typed.ts'\n\nexport interface PromiseType {\n then: () => unknown\n}\n\nexport type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>\n\nexport const isPromise = (value: unknown): value is Promise<unknown> => {\n if (typeof value === 'object' && !Array.isArray(value)) {\n return typeof (value as PromiseType).then === 'function'\n }\n return false\n}\n\n// type test\n/*\nconst x = Promise.resolve()\n\nconst f = (x: AnyNonPromise) => {\n return x\n}\n\nf(x)\n*/\n","export type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult\nexport type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void\nexport type PromiseExValueFunc<V> = (value?: V) => boolean\n\nexport class PromiseEx<T, V = void> extends Promise<T> {\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 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 { isType } from '@xylabs/typeof'\n\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nexport type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined\nexport type TypedKey<T extends string | void = void> = T extends string ? T : string | number | symbol\nexport type TypedObject = { [key: TypedKey]: TypedValue } | object\nexport type TypedArray = TypedValue[]\n\nexport const isTypedKey = (value: unknown): value is TypedKey => {\n switch (typeof value) {\n case 'string':\n case 'bigint':\n case 'number':\n case 'symbol': {\n return true\n }\n default: {\n return false\n }\n }\n}\n\nexport const isTypedValue = (value: unknown): value is TypedValue => {\n switch (typeof value) {\n case 'string':\n case 'number':\n case 'boolean': {\n return true\n }\n default: {\n return value === null || isTypedObject(value) || isTypedArray(value)\n }\n }\n}\n\nexport const isTypedArray = (value: unknown): value is TypedArray => {\n return Array.isArray(value) && !value.some(item => !isTypedValue(item))\n}\n\nexport const isValidTypedFieldPair = (pair: [key: unknown, value: unknown]): pair is [key: TypedKey, value: TypedValue] => {\n const [key, value] = pair\n return isTypedKey(key) && isTypedValue(value)\n}\n\nexport const isTypedObject = (value: unknown): value is TypedObject => {\n return (\n isType(value, 'object')\n // check if all keys are strings\n && !Object.entries(value as object).some(item => !isValidTypedFieldPair(item))\n )\n}\n\n// Object Type Test\n/*\ninterface TestObject {\n value: number\n}\n\nconst x: TestObject = { value: 1 }\n\nconst f = (p: TypedValue): void => {\n console.log(p)\n}\n\nf(x)\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;;;ACbO,IAAM,YAAY,CAAC,UAA8C;AACtE,MAAI,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GAAG;AACtD,WAAO,OAAQ,MAAsB,SAAS;AAAA,EAChD;AACA,SAAO;AACT;;;ACTO,IAAM,YAAN,cAAqC,QAAW;AAAA,EACrD;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,EAEA,MAAM,SAAkC;AACtC,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;AC1BO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACPA,SAAS,cAAc;AAQhB,IAAM,aAAa,CAAC,UAAsC;AAC/D,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK,UAAU;AACb,aAAO;AAAA,IACT;AAAA,IACA,SAAS;AACP,aAAO;AAAA,IACT;AAAA,EACF;AACF;AAEO,IAAM,eAAe,CAAC,UAAwC;AACnE,UAAQ,OAAO,OAAO;AAAA,IACpB,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK,WAAW;AACd,aAAO;AAAA,IACT;AAAA,IACA,SAAS;AACP,aAAO,UAAU,QAAQ,cAAc,KAAK,KAAK,aAAa,KAAK;AAAA,IACrE;AAAA,EACF;AACF;AAEO,IAAM,eAAe,CAAC,UAAwC;AACnE,SAAO,MAAM,QAAQ,KAAK,KAAK,CAAC,MAAM,KAAK,UAAQ,CAAC,aAAa,IAAI,CAAC;AACxE;AAEO,IAAM,wBAAwB,CAAC,SAAqF;AACzH,QAAM,CAAC,KAAK,KAAK,IAAI;AACrB,SAAO,WAAW,GAAG,KAAK,aAAa,KAAK;AAC9C;AAEO,IAAM,gBAAgB,CAAC,UAAyC;AACrE,SACE,OAAO,OAAO,QAAQ,KAEnB,CAAC,OAAO,QAAQ,KAAe,EAAE,KAAK,UAAQ,CAAC,sBAAsB,IAAI,CAAC;AAEjF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/promise",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.5",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"promise",
|
|
@@ -35,12 +35,15 @@
|
|
|
35
35
|
},
|
|
36
36
|
"module": "./dist/neutral/index.mjs",
|
|
37
37
|
"types": "./dist/neutral/index.d.ts",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@xylabs/typeof": "^4.3.5"
|
|
40
|
+
},
|
|
38
41
|
"devDependencies": {
|
|
39
|
-
"@xylabs/ts-scripts-yarn3": "^4.2.
|
|
40
|
-
"@xylabs/tsconfig": "^4.2.
|
|
41
|
-
"@xylabs/vitest-extended": "^4.3.
|
|
42
|
+
"@xylabs/ts-scripts-yarn3": "^4.2.4",
|
|
43
|
+
"@xylabs/tsconfig": "^4.2.4",
|
|
44
|
+
"@xylabs/vitest-extended": "^4.3.5",
|
|
42
45
|
"typescript": "^5.6.3",
|
|
43
|
-
"vitest": "^2.1.
|
|
46
|
+
"vitest": "^2.1.5"
|
|
44
47
|
},
|
|
45
48
|
"engines": {
|
|
46
49
|
"node": ">=18"
|
package/src/Typed.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isType } from '
|
|
1
|
+
import { isType } from '@xylabs/typeof'
|
|
2
2
|
|
|
3
3
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
4
4
|
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined
|
package/dist/neutral/isType.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function';
|
|
2
|
-
export type ObjectTypeShape = Record<string | number | symbol, FieldType>;
|
|
3
|
-
export declare const isType: (value: unknown, expectedType: FieldType) => boolean;
|
|
4
|
-
//# sourceMappingURL=isType.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,WAAW,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;AAE1H,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,SAAS,CAAC,CAAA;AAEzE,eAAO,MAAM,MAAM,UAAW,OAAO,gBAAgB,SAAS,YAwB7D,CAAA"}
|
package/src/isType.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'
|
|
2
|
-
|
|
3
|
-
export type ObjectTypeShape = Record<string | number | symbol, FieldType>
|
|
4
|
-
|
|
5
|
-
export const isType = (value: unknown, expectedType: FieldType) => {
|
|
6
|
-
const typeofValue = typeof value
|
|
7
|
-
switch (expectedType) {
|
|
8
|
-
case 'array': {
|
|
9
|
-
return Array.isArray(value)
|
|
10
|
-
}
|
|
11
|
-
case 'null': {
|
|
12
|
-
return value === null
|
|
13
|
-
}
|
|
14
|
-
case 'undefined': {
|
|
15
|
-
return value === undefined
|
|
16
|
-
}
|
|
17
|
-
case 'object': {
|
|
18
|
-
// nulls resolve to objects, so exclude them
|
|
19
|
-
if (value === null) {
|
|
20
|
-
return false
|
|
21
|
-
}
|
|
22
|
-
// arrays resolve to objects, so exclude them
|
|
23
|
-
return typeofValue === 'object' && !Array.isArray(value)
|
|
24
|
-
}
|
|
25
|
-
default: {
|
|
26
|
-
return typeofValue === expectedType
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|