@xylabs/promise 3.0.13 → 3.0.15
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/browser/Typed.d.cts +12 -0
- package/dist/browser/Typed.d.cts.map +1 -0
- package/dist/browser/Typed.d.mts +12 -0
- package/dist/browser/Typed.d.mts.map +1 -0
- package/dist/browser/Typed.d.ts +12 -0
- package/dist/browser/Typed.d.ts.map +1 -0
- package/dist/browser/index.cjs +77 -0
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.d.cts +2 -0
- package/dist/browser/index.d.cts.map +1 -1
- package/dist/browser/index.d.mts +2 -0
- package/dist/browser/index.d.mts.map +1 -1
- package/dist/browser/index.d.ts +2 -0
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js +77 -0
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/isPromise.d.cts +7 -0
- package/dist/browser/isPromise.d.cts.map +1 -0
- package/dist/browser/isPromise.d.mts +7 -0
- package/dist/browser/isPromise.d.mts.map +1 -0
- package/dist/browser/isPromise.d.ts +7 -0
- package/dist/browser/isPromise.d.ts.map +1 -0
- package/dist/browser/isType.d.cts +4 -0
- package/dist/browser/isType.d.cts.map +1 -0
- package/dist/browser/isType.d.mts +4 -0
- package/dist/browser/isType.d.mts.map +1 -0
- package/dist/browser/isType.d.ts +4 -0
- package/dist/browser/isType.d.ts.map +1 -0
- package/dist/node/Typed.d.cts +12 -0
- package/dist/node/Typed.d.cts.map +1 -0
- package/dist/node/Typed.d.mts +12 -0
- package/dist/node/Typed.d.mts.map +1 -0
- package/dist/node/Typed.d.ts +12 -0
- package/dist/node/Typed.d.ts.map +1 -0
- package/dist/node/index.cjs +83 -0
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.cts +2 -0
- package/dist/node/index.d.cts.map +1 -1
- package/dist/node/index.d.mts +2 -0
- package/dist/node/index.d.mts.map +1 -1
- package/dist/node/index.d.ts +2 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +77 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/isPromise.d.cts +7 -0
- package/dist/node/isPromise.d.cts.map +1 -0
- package/dist/node/isPromise.d.mts +7 -0
- package/dist/node/isPromise.d.mts.map +1 -0
- package/dist/node/isPromise.d.ts +7 -0
- package/dist/node/isPromise.d.ts.map +1 -0
- package/dist/node/isType.d.cts +4 -0
- package/dist/node/isType.d.cts.map +1 -0
- package/dist/node/isType.d.mts +4 -0
- package/dist/node/isType.d.mts.map +1 -0
- package/dist/node/isType.d.ts +4 -0
- package/dist/node/isType.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/Typed.ts +51 -0
- package/src/index.ts +2 -0
- package/src/isPromise.ts +16 -0
- package/src/isType.ts +31 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey = string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
};
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAC/C,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,CAAA;AACzD,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,sBAYxC,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAW1C,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAE1C,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,+CAGzE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,yBAM3C,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey = string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
};
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAC/C,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,CAAA;AACzD,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,sBAYxC,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAW1C,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAE1C,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,+CAGzE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,yBAM3C,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey = string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
};
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAC/C,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,CAAA;AACzD,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,sBAYxC,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAW1C,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAE1C,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,+CAGzE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,yBAM3C,CAAA"}
|
package/dist/browser/index.cjs
CHANGED
|
@@ -23,6 +23,12 @@ __export(src_exports, {
|
|
|
23
23
|
PromiseEx: () => PromiseEx,
|
|
24
24
|
fulfilled: () => fulfilled,
|
|
25
25
|
fulfilledValues: () => fulfilledValues,
|
|
26
|
+
isPromise: () => isPromise,
|
|
27
|
+
isTypedArray: () => isTypedArray,
|
|
28
|
+
isTypedKey: () => isTypedKey,
|
|
29
|
+
isTypedObject: () => isTypedObject,
|
|
30
|
+
isTypedValue: () => isTypedValue,
|
|
31
|
+
isValidTypedFieldPair: () => isValidTypedFieldPair,
|
|
26
32
|
rejected: () => rejected
|
|
27
33
|
});
|
|
28
34
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -39,6 +45,14 @@ var fulfilledValues = (previousValue, currentValue) => {
|
|
|
39
45
|
return previousValue;
|
|
40
46
|
};
|
|
41
47
|
|
|
48
|
+
// src/isPromise.ts
|
|
49
|
+
var isPromise = (value) => {
|
|
50
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
51
|
+
return typeof value.then === "function";
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
|
|
42
56
|
// src/PromiseEx.ts
|
|
43
57
|
var PromiseEx = class extends Promise {
|
|
44
58
|
cancelled;
|
|
@@ -66,4 +80,67 @@ var PromiseEx = class extends Promise {
|
|
|
66
80
|
var rejected = (val) => {
|
|
67
81
|
return val.status === "rejected";
|
|
68
82
|
};
|
|
83
|
+
|
|
84
|
+
// src/isType.ts
|
|
85
|
+
var isType = (value, expectedType) => {
|
|
86
|
+
const typeofValue = typeof value;
|
|
87
|
+
switch (expectedType) {
|
|
88
|
+
case "array": {
|
|
89
|
+
return Array.isArray(value);
|
|
90
|
+
}
|
|
91
|
+
case "null": {
|
|
92
|
+
return value === null;
|
|
93
|
+
}
|
|
94
|
+
case "undefined": {
|
|
95
|
+
return value === void 0;
|
|
96
|
+
}
|
|
97
|
+
case "object": {
|
|
98
|
+
if (value === null) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return typeofValue === "object" && !Array.isArray(value);
|
|
102
|
+
}
|
|
103
|
+
default: {
|
|
104
|
+
return typeofValue === expectedType;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/Typed.ts
|
|
110
|
+
var isTypedKey = (value) => {
|
|
111
|
+
switch (typeof value) {
|
|
112
|
+
case "string":
|
|
113
|
+
case "bigint":
|
|
114
|
+
case "number":
|
|
115
|
+
case "symbol": {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
default: {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
var isTypedValue = (value) => {
|
|
124
|
+
switch (typeof value) {
|
|
125
|
+
case "string":
|
|
126
|
+
case "number":
|
|
127
|
+
case "boolean": {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
default: {
|
|
131
|
+
return value === null || isTypedObject(value) || isTypedArray(value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var isTypedArray = (value) => {
|
|
136
|
+
return Array.isArray(value) && !value.some((item) => !isTypedValue(item));
|
|
137
|
+
};
|
|
138
|
+
var isValidTypedFieldPair = (pair) => {
|
|
139
|
+
const [key, value] = pair;
|
|
140
|
+
return isTypedKey(key) && isTypedValue(value);
|
|
141
|
+
};
|
|
142
|
+
var isTypedObject = (value) => {
|
|
143
|
+
return isType(value, "object") && //check if all keys are strings
|
|
144
|
+
!Object.entries(value).some((item) => !isValidTypedFieldPair(item));
|
|
145
|
+
};
|
|
69
146
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.ts"],"sourcesContent":["export { fulfilled } from './fulfilled'\nexport { fulfilledValues } from './fulfilledValues'\nexport * from './PromiseEx'\nexport { rejected } from './rejected'\nexport * from './types'\n","/**\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","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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/isPromise.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/isType.ts","../../src/Typed.ts"],"sourcesContent":["export { fulfilled } from './fulfilled'\nexport { fulfilledValues } from './fulfilledValues'\nexport * from './isPromise'\nexport * from './PromiseEx'\nexport { rejected } from './rejected'\nexport * from './Typed'\nexport * from './types'\n","/**\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 { TypedValue } from './Typed'\n\nexport interface PromiseType {\n // eslint-disable-next-line @typescript-eslint/ban-types\n then: () => unknown\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\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","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","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'\n\nexport type ObjectTypeShape = Record<string | number | symbol, FieldType>\n\nexport const isType = (value: unknown, expectedType: FieldType) => {\n const typeofValue = typeof value\n switch (expectedType) {\n case 'array': {\n return Array.isArray(value)\n }\n case 'null': {\n return value === null\n }\n case 'undefined': {\n return value === undefined\n }\n case 'object': {\n //nulls resolve to objects, so exclude them\n if (value === null) {\n return false\n }\n //arrays resolve to objects, so exclude them\n return typeofValue === 'object' && !Array.isArray(value)\n }\n default: {\n return typeofValue === expectedType\n }\n }\n}\n","import { isType } from './isType'\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined\nexport type TypedKey = string | number | symbol\nexport type TypedObject = { [key: TypedKey]: TypedValue }\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACXO,IAAM,YAAY,CAAC,UAA8C;AACtE,MAAI,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GAAG;AACtD,WAAO,OAAQ,MAAsB,SAAS;AAAA,EAChD;AACA,SAAO;AACT;;;ACXO,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;;;ACDO,IAAM,SAAS,CAAC,OAAgB,iBAA4B;AACjE,QAAM,cAAc,OAAO;AAC3B,UAAQ,cAAc;AAAA,IACpB,KAAK,SAAS;AACZ,aAAO,MAAM,QAAQ,KAAK;AAAA,IAC5B;AAAA,IACA,KAAK,QAAQ;AACX,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,aAAa;AAChB,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,UAAU;AAEb,UAAI,UAAU,MAAM;AAClB,eAAO;AAAA,MACT;AAEA,aAAO,gBAAgB,YAAY,CAAC,MAAM,QAAQ,KAAK;AAAA,IACzD;AAAA,IACA,SAAS;AACP,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF;AACF;;;ACtBO,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,CAAC,SAAS,CAAC,aAAa,IAAI,CAAC;AAC1E;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;AAAA,EAEtB,CAAC,OAAO,QAAQ,KAAe,EAAE,KAAK,CAAC,SAAS,CAAC,sBAAsB,IAAI,CAAC;AAEhF;","names":[]}
|
package/dist/browser/index.d.cts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { fulfilled } from './fulfilled';
|
|
2
2
|
export { fulfilledValues } from './fulfilledValues';
|
|
3
|
+
export * from './isPromise';
|
|
3
4
|
export * from './PromiseEx';
|
|
4
5
|
export { rejected } from './rejected';
|
|
6
|
+
export * from './Typed';
|
|
5
7
|
export * from './types';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/browser/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { fulfilled } from './fulfilled';
|
|
2
2
|
export { fulfilledValues } from './fulfilledValues';
|
|
3
|
+
export * from './isPromise';
|
|
3
4
|
export * from './PromiseEx';
|
|
4
5
|
export { rejected } from './rejected';
|
|
6
|
+
export * from './Typed';
|
|
5
7
|
export * from './types';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/browser/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { fulfilled } from './fulfilled';
|
|
2
2
|
export { fulfilledValues } from './fulfilledValues';
|
|
3
|
+
export * from './isPromise';
|
|
3
4
|
export * from './PromiseEx';
|
|
4
5
|
export { rejected } from './rejected';
|
|
6
|
+
export * from './Typed';
|
|
5
7
|
export * from './types';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/browser/index.js
CHANGED
|
@@ -10,6 +10,14 @@ var fulfilledValues = (previousValue, currentValue) => {
|
|
|
10
10
|
return previousValue;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
// src/isPromise.ts
|
|
14
|
+
var isPromise = (value) => {
|
|
15
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
16
|
+
return typeof value.then === "function";
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
};
|
|
20
|
+
|
|
13
21
|
// src/PromiseEx.ts
|
|
14
22
|
var PromiseEx = class extends Promise {
|
|
15
23
|
cancelled;
|
|
@@ -37,10 +45,79 @@ var PromiseEx = class extends Promise {
|
|
|
37
45
|
var rejected = (val) => {
|
|
38
46
|
return val.status === "rejected";
|
|
39
47
|
};
|
|
48
|
+
|
|
49
|
+
// src/isType.ts
|
|
50
|
+
var isType = (value, expectedType) => {
|
|
51
|
+
const typeofValue = typeof value;
|
|
52
|
+
switch (expectedType) {
|
|
53
|
+
case "array": {
|
|
54
|
+
return Array.isArray(value);
|
|
55
|
+
}
|
|
56
|
+
case "null": {
|
|
57
|
+
return value === null;
|
|
58
|
+
}
|
|
59
|
+
case "undefined": {
|
|
60
|
+
return value === void 0;
|
|
61
|
+
}
|
|
62
|
+
case "object": {
|
|
63
|
+
if (value === null) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return typeofValue === "object" && !Array.isArray(value);
|
|
67
|
+
}
|
|
68
|
+
default: {
|
|
69
|
+
return typeofValue === expectedType;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/Typed.ts
|
|
75
|
+
var isTypedKey = (value) => {
|
|
76
|
+
switch (typeof value) {
|
|
77
|
+
case "string":
|
|
78
|
+
case "bigint":
|
|
79
|
+
case "number":
|
|
80
|
+
case "symbol": {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
default: {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var isTypedValue = (value) => {
|
|
89
|
+
switch (typeof value) {
|
|
90
|
+
case "string":
|
|
91
|
+
case "number":
|
|
92
|
+
case "boolean": {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
default: {
|
|
96
|
+
return value === null || isTypedObject(value) || isTypedArray(value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var isTypedArray = (value) => {
|
|
101
|
+
return Array.isArray(value) && !value.some((item) => !isTypedValue(item));
|
|
102
|
+
};
|
|
103
|
+
var isValidTypedFieldPair = (pair) => {
|
|
104
|
+
const [key, value] = pair;
|
|
105
|
+
return isTypedKey(key) && isTypedValue(value);
|
|
106
|
+
};
|
|
107
|
+
var isTypedObject = (value) => {
|
|
108
|
+
return isType(value, "object") && //check if all keys are strings
|
|
109
|
+
!Object.entries(value).some((item) => !isValidTypedFieldPair(item));
|
|
110
|
+
};
|
|
40
111
|
export {
|
|
41
112
|
PromiseEx,
|
|
42
113
|
fulfilled,
|
|
43
114
|
fulfilledValues,
|
|
115
|
+
isPromise,
|
|
116
|
+
isTypedArray,
|
|
117
|
+
isTypedKey,
|
|
118
|
+
isTypedObject,
|
|
119
|
+
isTypedValue,
|
|
120
|
+
isValidTypedFieldPair,
|
|
44
121
|
rejected
|
|
45
122
|
};
|
|
46
123
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.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","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"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;
|
|
1
|
+
{"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/isPromise.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/isType.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 { TypedValue } from './Typed'\n\nexport interface PromiseType {\n // eslint-disable-next-line @typescript-eslint/ban-types\n then: () => unknown\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\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","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","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'\n\nexport type ObjectTypeShape = Record<string | number | symbol, FieldType>\n\nexport const isType = (value: unknown, expectedType: FieldType) => {\n const typeofValue = typeof value\n switch (expectedType) {\n case 'array': {\n return Array.isArray(value)\n }\n case 'null': {\n return value === null\n }\n case 'undefined': {\n return value === undefined\n }\n case 'object': {\n //nulls resolve to objects, so exclude them\n if (value === null) {\n return false\n }\n //arrays resolve to objects, so exclude them\n return typeofValue === 'object' && !Array.isArray(value)\n }\n default: {\n return typeofValue === expectedType\n }\n }\n}\n","import { isType } from './isType'\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined\nexport type TypedKey = string | number | symbol\nexport type TypedObject = { [key: TypedKey]: TypedValue }\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"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACXO,IAAM,YAAY,CAAC,UAA8C;AACtE,MAAI,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GAAG;AACtD,WAAO,OAAQ,MAAsB,SAAS;AAAA,EAChD;AACA,SAAO;AACT;;;ACXO,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;;;ACDO,IAAM,SAAS,CAAC,OAAgB,iBAA4B;AACjE,QAAM,cAAc,OAAO;AAC3B,UAAQ,cAAc;AAAA,IACpB,KAAK,SAAS;AACZ,aAAO,MAAM,QAAQ,KAAK;AAAA,IAC5B;AAAA,IACA,KAAK,QAAQ;AACX,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,aAAa;AAChB,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,UAAU;AAEb,UAAI,UAAU,MAAM;AAClB,eAAO;AAAA,MACT;AAEA,aAAO,gBAAgB,YAAY,CAAC,MAAM,QAAQ,KAAK;AAAA,IACzD;AAAA,IACA,SAAS;AACP,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF;AACF;;;ACtBO,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,CAAC,SAAS,CAAC,aAAa,IAAI,CAAC;AAC1E;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;AAAA,EAEtB,CAAC,OAAO,QAAQ,KAAe,EAAE,KAAK,CAAC,SAAS,CAAC,sBAAsB,IAAI,CAAC;AAEhF;","names":[]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedValue } from './Typed';
|
|
2
|
+
export interface PromiseType {
|
|
3
|
+
then: () => unknown;
|
|
4
|
+
}
|
|
5
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>;
|
|
6
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
7
|
+
//# sourceMappingURL=isPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPromise.d.ts","sourceRoot":"","sources":["../../src/isPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAGD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAEjE,eAAO,MAAM,SAAS,UAAW,OAAO,8BAKvC,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedValue } from './Typed';
|
|
2
|
+
export interface PromiseType {
|
|
3
|
+
then: () => unknown;
|
|
4
|
+
}
|
|
5
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>;
|
|
6
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
7
|
+
//# sourceMappingURL=isPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPromise.d.ts","sourceRoot":"","sources":["../../src/isPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAGD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAEjE,eAAO,MAAM,SAAS,UAAW,OAAO,8BAKvC,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedValue } from './Typed';
|
|
2
|
+
export interface PromiseType {
|
|
3
|
+
then: () => unknown;
|
|
4
|
+
}
|
|
5
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>;
|
|
6
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
7
|
+
//# sourceMappingURL=isPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPromise.d.ts","sourceRoot":"","sources":["../../src/isPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAGD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAEjE,eAAO,MAAM,SAAS,UAAW,OAAO,8BAKvC,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAEA,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"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAEA,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"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAEA,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"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey = string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
};
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAC/C,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,CAAA;AACzD,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,sBAYxC,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAW1C,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAE1C,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,+CAGzE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,yBAM3C,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey = string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
};
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAC/C,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,CAAA;AACzD,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,sBAYxC,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAW1C,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAE1C,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,+CAGzE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,yBAM3C,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined;
|
|
2
|
+
export type TypedKey = string | number | symbol;
|
|
3
|
+
export type TypedObject = {
|
|
4
|
+
[key: TypedKey]: TypedValue;
|
|
5
|
+
};
|
|
6
|
+
export type TypedArray = TypedValue[];
|
|
7
|
+
export declare const isTypedKey: (value: unknown) => value is TypedKey;
|
|
8
|
+
export declare const isTypedValue: (value: unknown) => value is TypedValue;
|
|
9
|
+
export declare const isTypedArray: (value: unknown) => value is TypedArray;
|
|
10
|
+
export declare const isValidTypedFieldPair: (pair: [key: unknown, value: unknown]) => pair is [key: TypedKey, value: TypedValue];
|
|
11
|
+
export declare const isTypedObject: (value: unknown) => value is TypedObject;
|
|
12
|
+
//# sourceMappingURL=Typed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Typed.d.ts","sourceRoot":"","sources":["../../src/Typed.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,WAAW,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,GAAG,SAAS,CAAA;AAC7H,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAA;AAC/C,MAAM,MAAM,WAAW,GAAG;IAAE,CAAC,GAAG,EAAE,QAAQ,GAAG,UAAU,CAAA;CAAE,CAAA;AACzD,MAAM,MAAM,UAAU,GAAG,UAAU,EAAE,CAAA;AAErC,eAAO,MAAM,UAAU,UAAW,OAAO,sBAYxC,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAW1C,CAAA;AAED,eAAO,MAAM,YAAY,UAAW,OAAO,wBAE1C,CAAA;AAED,eAAO,MAAM,qBAAqB,SAAU,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,+CAGzE,CAAA;AAED,eAAO,MAAM,aAAa,UAAW,OAAO,yBAM3C,CAAA"}
|
package/dist/node/index.cjs
CHANGED
|
@@ -23,6 +23,12 @@ __export(src_exports, {
|
|
|
23
23
|
PromiseEx: () => PromiseEx,
|
|
24
24
|
fulfilled: () => fulfilled,
|
|
25
25
|
fulfilledValues: () => fulfilledValues,
|
|
26
|
+
isPromise: () => isPromise,
|
|
27
|
+
isTypedArray: () => isTypedArray,
|
|
28
|
+
isTypedKey: () => isTypedKey,
|
|
29
|
+
isTypedObject: () => isTypedObject,
|
|
30
|
+
isTypedValue: () => isTypedValue,
|
|
31
|
+
isValidTypedFieldPair: () => isValidTypedFieldPair,
|
|
26
32
|
rejected: () => rejected
|
|
27
33
|
});
|
|
28
34
|
module.exports = __toCommonJS(src_exports);
|
|
@@ -39,6 +45,14 @@ var fulfilledValues = (previousValue, currentValue) => {
|
|
|
39
45
|
return previousValue;
|
|
40
46
|
};
|
|
41
47
|
|
|
48
|
+
// src/isPromise.ts
|
|
49
|
+
var isPromise = (value) => {
|
|
50
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
51
|
+
return typeof value.then === "function";
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
|
|
42
56
|
// src/PromiseEx.ts
|
|
43
57
|
var PromiseEx = class extends Promise {
|
|
44
58
|
cancelled;
|
|
@@ -66,11 +80,80 @@ var PromiseEx = class extends Promise {
|
|
|
66
80
|
var rejected = (val) => {
|
|
67
81
|
return val.status === "rejected";
|
|
68
82
|
};
|
|
83
|
+
|
|
84
|
+
// src/isType.ts
|
|
85
|
+
var isType = (value, expectedType) => {
|
|
86
|
+
const typeofValue = typeof value;
|
|
87
|
+
switch (expectedType) {
|
|
88
|
+
case "array": {
|
|
89
|
+
return Array.isArray(value);
|
|
90
|
+
}
|
|
91
|
+
case "null": {
|
|
92
|
+
return value === null;
|
|
93
|
+
}
|
|
94
|
+
case "undefined": {
|
|
95
|
+
return value === void 0;
|
|
96
|
+
}
|
|
97
|
+
case "object": {
|
|
98
|
+
if (value === null) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return typeofValue === "object" && !Array.isArray(value);
|
|
102
|
+
}
|
|
103
|
+
default: {
|
|
104
|
+
return typeofValue === expectedType;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/Typed.ts
|
|
110
|
+
var isTypedKey = (value) => {
|
|
111
|
+
switch (typeof value) {
|
|
112
|
+
case "string":
|
|
113
|
+
case "bigint":
|
|
114
|
+
case "number":
|
|
115
|
+
case "symbol": {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
default: {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
var isTypedValue = (value) => {
|
|
124
|
+
switch (typeof value) {
|
|
125
|
+
case "string":
|
|
126
|
+
case "number":
|
|
127
|
+
case "boolean": {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
default: {
|
|
131
|
+
return value === null || isTypedObject(value) || isTypedArray(value);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
var isTypedArray = (value) => {
|
|
136
|
+
return Array.isArray(value) && !value.some((item) => !isTypedValue(item));
|
|
137
|
+
};
|
|
138
|
+
var isValidTypedFieldPair = (pair) => {
|
|
139
|
+
const [key, value] = pair;
|
|
140
|
+
return isTypedKey(key) && isTypedValue(value);
|
|
141
|
+
};
|
|
142
|
+
var isTypedObject = (value) => {
|
|
143
|
+
return isType(value, "object") && //check if all keys are strings
|
|
144
|
+
!Object.entries(value).some((item) => !isValidTypedFieldPair(item));
|
|
145
|
+
};
|
|
69
146
|
// Annotate the CommonJS export names for ESM import in node:
|
|
70
147
|
0 && (module.exports = {
|
|
71
148
|
PromiseEx,
|
|
72
149
|
fulfilled,
|
|
73
150
|
fulfilledValues,
|
|
151
|
+
isPromise,
|
|
152
|
+
isTypedArray,
|
|
153
|
+
isTypedKey,
|
|
154
|
+
isTypedObject,
|
|
155
|
+
isTypedValue,
|
|
156
|
+
isValidTypedFieldPair,
|
|
74
157
|
rejected
|
|
75
158
|
});
|
|
76
159
|
//# sourceMappingURL=index.cjs.map
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.ts"],"sourcesContent":["export { fulfilled } from './fulfilled'\nexport { fulfilledValues } from './fulfilledValues'\nexport * from './PromiseEx'\nexport { rejected } from './rejected'\nexport * from './types'\n","/**\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","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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/isPromise.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/isType.ts","../../src/Typed.ts"],"sourcesContent":["export { fulfilled } from './fulfilled'\nexport { fulfilledValues } from './fulfilledValues'\nexport * from './isPromise'\nexport * from './PromiseEx'\nexport { rejected } from './rejected'\nexport * from './Typed'\nexport * from './types'\n","/**\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 { TypedValue } from './Typed'\n\nexport interface PromiseType {\n // eslint-disable-next-line @typescript-eslint/ban-types\n then: () => unknown\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\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","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","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'\n\nexport type ObjectTypeShape = Record<string | number | symbol, FieldType>\n\nexport const isType = (value: unknown, expectedType: FieldType) => {\n const typeofValue = typeof value\n switch (expectedType) {\n case 'array': {\n return Array.isArray(value)\n }\n case 'null': {\n return value === null\n }\n case 'undefined': {\n return value === undefined\n }\n case 'object': {\n //nulls resolve to objects, so exclude them\n if (value === null) {\n return false\n }\n //arrays resolve to objects, so exclude them\n return typeofValue === 'object' && !Array.isArray(value)\n }\n default: {\n return typeofValue === expectedType\n }\n }\n}\n","import { isType } from './isType'\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined\nexport type TypedKey = string | number | symbol\nexport type TypedObject = { [key: TypedKey]: TypedValue }\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACXO,IAAM,YAAY,CAAC,UAA8C;AACtE,MAAI,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GAAG;AACtD,WAAO,OAAQ,MAAsB,SAAS;AAAA,EAChD;AACA,SAAO;AACT;;;ACXO,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,mCAAU,KAAK,SAAS;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA,EAEA,MAAM,SAAkC;AACtC,QAAI,mCAAU,KAAK,SAAS;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;AC1BO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACDO,IAAM,SAAS,CAAC,OAAgB,iBAA4B;AACjE,QAAM,cAAc,OAAO;AAC3B,UAAQ,cAAc;AAAA,IACpB,KAAK,SAAS;AACZ,aAAO,MAAM,QAAQ,KAAK;AAAA,IAC5B;AAAA,IACA,KAAK,QAAQ;AACX,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,aAAa;AAChB,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,UAAU;AAEb,UAAI,UAAU,MAAM;AAClB,eAAO;AAAA,MACT;AAEA,aAAO,gBAAgB,YAAY,CAAC,MAAM,QAAQ,KAAK;AAAA,IACzD;AAAA,IACA,SAAS;AACP,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF;AACF;;;ACtBO,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,CAAC,SAAS,CAAC,aAAa,IAAI,CAAC;AAC1E;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;AAAA,EAEtB,CAAC,OAAO,QAAQ,KAAe,EAAE,KAAK,CAAC,SAAS,CAAC,sBAAsB,IAAI,CAAC;AAEhF;","names":[]}
|
package/dist/node/index.d.cts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { fulfilled } from './fulfilled';
|
|
2
2
|
export { fulfilledValues } from './fulfilledValues';
|
|
3
|
+
export * from './isPromise';
|
|
3
4
|
export * from './PromiseEx';
|
|
4
5
|
export { rejected } from './rejected';
|
|
6
|
+
export * from './Typed';
|
|
5
7
|
export * from './types';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/node/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { fulfilled } from './fulfilled';
|
|
2
2
|
export { fulfilledValues } from './fulfilledValues';
|
|
3
|
+
export * from './isPromise';
|
|
3
4
|
export * from './PromiseEx';
|
|
4
5
|
export { rejected } from './rejected';
|
|
6
|
+
export * from './Typed';
|
|
5
7
|
export * from './types';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/node/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export { fulfilled } from './fulfilled';
|
|
2
2
|
export { fulfilledValues } from './fulfilledValues';
|
|
3
|
+
export * from './isPromise';
|
|
3
4
|
export * from './PromiseEx';
|
|
4
5
|
export { rejected } from './rejected';
|
|
6
|
+
export * from './Typed';
|
|
5
7
|
export * from './types';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/node/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
|
package/dist/node/index.js
CHANGED
|
@@ -10,6 +10,14 @@ var fulfilledValues = (previousValue, currentValue) => {
|
|
|
10
10
|
return previousValue;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
// src/isPromise.ts
|
|
14
|
+
var isPromise = (value) => {
|
|
15
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
16
|
+
return typeof value.then === "function";
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
};
|
|
20
|
+
|
|
13
21
|
// src/PromiseEx.ts
|
|
14
22
|
var PromiseEx = class extends Promise {
|
|
15
23
|
cancelled;
|
|
@@ -37,10 +45,79 @@ var PromiseEx = class extends Promise {
|
|
|
37
45
|
var rejected = (val) => {
|
|
38
46
|
return val.status === "rejected";
|
|
39
47
|
};
|
|
48
|
+
|
|
49
|
+
// src/isType.ts
|
|
50
|
+
var isType = (value, expectedType) => {
|
|
51
|
+
const typeofValue = typeof value;
|
|
52
|
+
switch (expectedType) {
|
|
53
|
+
case "array": {
|
|
54
|
+
return Array.isArray(value);
|
|
55
|
+
}
|
|
56
|
+
case "null": {
|
|
57
|
+
return value === null;
|
|
58
|
+
}
|
|
59
|
+
case "undefined": {
|
|
60
|
+
return value === void 0;
|
|
61
|
+
}
|
|
62
|
+
case "object": {
|
|
63
|
+
if (value === null) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return typeofValue === "object" && !Array.isArray(value);
|
|
67
|
+
}
|
|
68
|
+
default: {
|
|
69
|
+
return typeofValue === expectedType;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/Typed.ts
|
|
75
|
+
var isTypedKey = (value) => {
|
|
76
|
+
switch (typeof value) {
|
|
77
|
+
case "string":
|
|
78
|
+
case "bigint":
|
|
79
|
+
case "number":
|
|
80
|
+
case "symbol": {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
default: {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var isTypedValue = (value) => {
|
|
89
|
+
switch (typeof value) {
|
|
90
|
+
case "string":
|
|
91
|
+
case "number":
|
|
92
|
+
case "boolean": {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
default: {
|
|
96
|
+
return value === null || isTypedObject(value) || isTypedArray(value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var isTypedArray = (value) => {
|
|
101
|
+
return Array.isArray(value) && !value.some((item) => !isTypedValue(item));
|
|
102
|
+
};
|
|
103
|
+
var isValidTypedFieldPair = (pair) => {
|
|
104
|
+
const [key, value] = pair;
|
|
105
|
+
return isTypedKey(key) && isTypedValue(value);
|
|
106
|
+
};
|
|
107
|
+
var isTypedObject = (value) => {
|
|
108
|
+
return isType(value, "object") && //check if all keys are strings
|
|
109
|
+
!Object.entries(value).some((item) => !isValidTypedFieldPair(item));
|
|
110
|
+
};
|
|
40
111
|
export {
|
|
41
112
|
PromiseEx,
|
|
42
113
|
fulfilled,
|
|
43
114
|
fulfilledValues,
|
|
115
|
+
isPromise,
|
|
116
|
+
isTypedArray,
|
|
117
|
+
isTypedKey,
|
|
118
|
+
isTypedObject,
|
|
119
|
+
isTypedValue,
|
|
120
|
+
isValidTypedFieldPair,
|
|
44
121
|
rejected
|
|
45
122
|
};
|
|
46
123
|
//# sourceMappingURL=index.js.map
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.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","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"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;
|
|
1
|
+
{"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/isPromise.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/isType.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 { TypedValue } from './Typed'\n\nexport interface PromiseType {\n // eslint-disable-next-line @typescript-eslint/ban-types\n then: () => unknown\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\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","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","/* eslint-disable @typescript-eslint/no-explicit-any */\n\nexport type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'\n\nexport type ObjectTypeShape = Record<string | number | symbol, FieldType>\n\nexport const isType = (value: unknown, expectedType: FieldType) => {\n const typeofValue = typeof value\n switch (expectedType) {\n case 'array': {\n return Array.isArray(value)\n }\n case 'null': {\n return value === null\n }\n case 'undefined': {\n return value === undefined\n }\n case 'object': {\n //nulls resolve to objects, so exclude them\n if (value === null) {\n return false\n }\n //arrays resolve to objects, so exclude them\n return typeofValue === 'object' && !Array.isArray(value)\n }\n default: {\n return typeofValue === expectedType\n }\n }\n}\n","import { isType } from './isType'\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined\nexport type TypedKey = string | number | symbol\nexport type TypedObject = { [key: TypedKey]: TypedValue }\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"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW;AAAa,kBAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACXO,IAAM,YAAY,CAAC,UAA8C;AACtE,MAAI,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GAAG;AACtD,WAAO,OAAQ,MAAsB,SAAS;AAAA,EAChD;AACA,SAAO;AACT;;;ACXO,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,mCAAU,KAAK,SAAS;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA,EAEA,MAAM,SAAkC;AACtC,QAAI,mCAAU,KAAK,SAAS;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;AC1BO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACDO,IAAM,SAAS,CAAC,OAAgB,iBAA4B;AACjE,QAAM,cAAc,OAAO;AAC3B,UAAQ,cAAc;AAAA,IACpB,KAAK,SAAS;AACZ,aAAO,MAAM,QAAQ,KAAK;AAAA,IAC5B;AAAA,IACA,KAAK,QAAQ;AACX,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,aAAa;AAChB,aAAO,UAAU;AAAA,IACnB;AAAA,IACA,KAAK,UAAU;AAEb,UAAI,UAAU,MAAM;AAClB,eAAO;AAAA,MACT;AAEA,aAAO,gBAAgB,YAAY,CAAC,MAAM,QAAQ,KAAK;AAAA,IACzD;AAAA,IACA,SAAS;AACP,aAAO,gBAAgB;AAAA,IACzB;AAAA,EACF;AACF;;;ACtBO,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,CAAC,SAAS,CAAC,aAAa,IAAI,CAAC;AAC1E;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;AAAA,EAEtB,CAAC,OAAO,QAAQ,KAAe,EAAE,KAAK,CAAC,SAAS,CAAC,sBAAsB,IAAI,CAAC;AAEhF;","names":[]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedValue } from './Typed';
|
|
2
|
+
export interface PromiseType {
|
|
3
|
+
then: () => unknown;
|
|
4
|
+
}
|
|
5
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>;
|
|
6
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
7
|
+
//# sourceMappingURL=isPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPromise.d.ts","sourceRoot":"","sources":["../../src/isPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAGD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAEjE,eAAO,MAAM,SAAS,UAAW,OAAO,8BAKvC,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedValue } from './Typed';
|
|
2
|
+
export interface PromiseType {
|
|
3
|
+
then: () => unknown;
|
|
4
|
+
}
|
|
5
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>;
|
|
6
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
7
|
+
//# sourceMappingURL=isPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPromise.d.ts","sourceRoot":"","sources":["../../src/isPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAGD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAEjE,eAAO,MAAM,SAAS,UAAW,OAAO,8BAKvC,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { TypedValue } from './Typed';
|
|
2
|
+
export interface PromiseType {
|
|
3
|
+
then: () => unknown;
|
|
4
|
+
}
|
|
5
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>;
|
|
6
|
+
export declare const isPromise: (value: unknown) => value is Promise<unknown>;
|
|
7
|
+
//# sourceMappingURL=isPromise.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPromise.d.ts","sourceRoot":"","sources":["../../src/isPromise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEpC,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAGD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;AAEjE,eAAO,MAAM,SAAS,UAAW,OAAO,8BAKvC,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAEA,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"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAEA,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"}
|
|
@@ -0,0 +1,4 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isType.d.ts","sourceRoot":"","sources":["../../src/isType.ts"],"names":[],"mappings":"AAEA,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/package.json
CHANGED
package/src/Typed.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { isType } from './isType'
|
|
2
|
+
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
4
|
+
export type TypedValue = bigint | string | number | boolean | null | TypedObject | TypedArray | Function | symbol | undefined
|
|
5
|
+
export type TypedKey = string | number | symbol
|
|
6
|
+
export type TypedObject = { [key: TypedKey]: TypedValue }
|
|
7
|
+
export type TypedArray = TypedValue[]
|
|
8
|
+
|
|
9
|
+
export const isTypedKey = (value: unknown): value is TypedKey => {
|
|
10
|
+
switch (typeof value) {
|
|
11
|
+
case 'string':
|
|
12
|
+
case 'bigint':
|
|
13
|
+
case 'number':
|
|
14
|
+
case 'symbol': {
|
|
15
|
+
return true
|
|
16
|
+
}
|
|
17
|
+
default: {
|
|
18
|
+
return false
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const isTypedValue = (value: unknown): value is TypedValue => {
|
|
24
|
+
switch (typeof value) {
|
|
25
|
+
case 'string':
|
|
26
|
+
case 'number':
|
|
27
|
+
case 'boolean': {
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
30
|
+
default: {
|
|
31
|
+
return value === null || isTypedObject(value) || isTypedArray(value)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const isTypedArray = (value: unknown): value is TypedArray => {
|
|
37
|
+
return Array.isArray(value) && !value.some((item) => !isTypedValue(item))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const isValidTypedFieldPair = (pair: [key: unknown, value: unknown]): pair is [key: TypedKey, value: TypedValue] => {
|
|
41
|
+
const [key, value] = pair
|
|
42
|
+
return isTypedKey(key) && isTypedValue(value)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const isTypedObject = (value: unknown): value is TypedObject => {
|
|
46
|
+
return (
|
|
47
|
+
isType(value, 'object') &&
|
|
48
|
+
//check if all keys are strings
|
|
49
|
+
!Object.entries(value as object).some((item) => !isValidTypedFieldPair(item))
|
|
50
|
+
)
|
|
51
|
+
}
|
package/src/index.ts
CHANGED
package/src/isPromise.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { TypedValue } from './Typed'
|
|
2
|
+
|
|
3
|
+
export interface PromiseType {
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
5
|
+
then: () => unknown
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
export type AnyNonPromise = Exclude<TypedValue, Promise<unknown>>
|
|
10
|
+
|
|
11
|
+
export const isPromise = (value: unknown): value is Promise<unknown> => {
|
|
12
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
13
|
+
return typeof (value as PromiseType).then === 'function'
|
|
14
|
+
}
|
|
15
|
+
return false
|
|
16
|
+
}
|
package/src/isType.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
export type FieldType = 'string' | 'number' | 'object' | 'symbol' | 'symbol' | 'undefined' | 'null' | 'array' | 'function'
|
|
4
|
+
|
|
5
|
+
export type ObjectTypeShape = Record<string | number | symbol, FieldType>
|
|
6
|
+
|
|
7
|
+
export const isType = (value: unknown, expectedType: FieldType) => {
|
|
8
|
+
const typeofValue = typeof value
|
|
9
|
+
switch (expectedType) {
|
|
10
|
+
case 'array': {
|
|
11
|
+
return Array.isArray(value)
|
|
12
|
+
}
|
|
13
|
+
case 'null': {
|
|
14
|
+
return value === null
|
|
15
|
+
}
|
|
16
|
+
case 'undefined': {
|
|
17
|
+
return value === undefined
|
|
18
|
+
}
|
|
19
|
+
case 'object': {
|
|
20
|
+
//nulls resolve to objects, so exclude them
|
|
21
|
+
if (value === null) {
|
|
22
|
+
return false
|
|
23
|
+
}
|
|
24
|
+
//arrays resolve to objects, so exclude them
|
|
25
|
+
return typeofValue === 'object' && !Array.isArray(value)
|
|
26
|
+
}
|
|
27
|
+
default: {
|
|
28
|
+
return typeofValue === expectedType
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|