@wener/utils 1.1.54 → 1.1.56
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/lib/arrays/arrayFromAsync.js +11 -3
- package/lib/asyncs/AsyncInterval.js +11 -3
- package/lib/asyncs/Promises.js +6 -5
- package/lib/asyncs/createAsyncIterator.js +11 -3
- package/lib/asyncs/createLazyPromise.test.js +11 -3
- package/lib/asyncs/generatorOfStream.js +11 -3
- package/lib/browsers/download.js +11 -3
- package/lib/browsers/loaders.js +11 -3
- package/lib/crypto/hashing.js +11 -3
- package/lib/crypto/hashing.test.js +11 -3
- package/lib/crypto/pem/pem.js +1 -1
- package/lib/fetch/createFetchWith.js +11 -3
- package/lib/fetch/dumpRequest.js +12 -4
- package/lib/fetch/dumpRequest.test.js +11 -3
- package/lib/fetch/dumpResponse.js +11 -3
- package/lib/fetch/dumpResponse.test.js +11 -3
- package/lib/io/ArrayBuffers.js +2 -2
- package/lib/io/ByteBuffer.test.js +11 -3
- package/lib/io/parseDataUri.js +11 -3
- package/lib/io/parseDataUri.test.js +31 -11
- package/lib/langs/AsyncCloser.js +11 -3
- package/lib/langs/deepFreeze.js +5 -5
- package/lib/langs/mixin.js +6 -12
- package/lib/langs/mixin.test.js +53 -5
- package/lib/langs/mixin2.js +26 -0
- package/lib/langs/parseBoolean.js +3 -2
- package/lib/langs/shallowEqual.js +5 -5
- package/lib/libs/ms.js +1 -1
- package/lib/maths/random.js +12 -4
- package/lib/objects/merge/isMergeableObject.js +1 -1
- package/lib/objects/merge/merge.js +1 -1
- package/lib/objects/merge/merge.test.js +11 -3
- package/lib/objects/set.js +10 -2
- package/lib/objects/set.test.js +2 -2
- package/lib/scripts/getGenerateContext.js +13 -5
- package/lib/server/fetch/createFetchWithProxyByNodeFetch.js +11 -3
- package/lib/server/fetch/createFetchWithProxyByUndici.js +11 -3
- package/lib/server/polyfill/polyfillBrowser.js +11 -3
- package/lib/server/polyfill/polyfillBrowser.test.js +11 -3
- package/lib/server/polyfill/polyfillCrypto.js +11 -3
- package/lib/server/polyfill/polyfillJsDom.js +31 -11
- package/lib/strings/renderTemplate.test.js +2 -2
- package/lib/web/getRandomValues.js +1 -1
- package/lib/web/structuredClone.js +2 -2
- package/package.json +8 -4
- package/src/asyncs/Promises.ts +2 -1
- package/src/asyncs/timeout.ts +1 -1
- package/src/crypto/hashing.ts +7 -6
- package/src/crypto/pem/pem.ts +3 -2
- package/src/fetch/dumpRequest.ts +1 -1
- package/src/langs/mixin.test.ts +35 -5
- package/src/langs/mixin.ts +46 -65
- package/src/langs/mixin2.ts +80 -0
- package/src/langs/parseBoolean.ts +9 -1
- package/src/objects/set.ts +10 -1
- package/src/types.d.ts +1 -1
- package/tsconfig.json +5 -2
package/src/langs/mixin.ts
CHANGED
|
@@ -1,79 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
* Helper type to convert a union to an intersection.
|
|
3
|
-
*
|
|
4
|
-
* @internal
|
|
5
|
-
*/
|
|
6
|
-
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
|
|
1
|
+
import type { Constructor } from '../types';
|
|
7
2
|
|
|
8
3
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @typeParam T The type of the instance to create.
|
|
12
|
-
* @example ```ts
|
|
13
|
-
* function Walkable<TBase extends MixinConstructor<Positionable>>(Base: TBase) {
|
|
14
|
-
* ··return class Walkable extends Base {
|
|
15
|
-
* ····public forward() { this.x++; }
|
|
16
|
-
* ····public backward() { this.x--; }
|
|
17
|
-
* ··};
|
|
18
|
-
* }
|
|
19
|
-
* ```
|
|
20
|
-
* @example ```ts
|
|
21
|
-
* function Loggable(Base: MixinConstructor) {
|
|
22
|
-
* ··return class Loggable extends Base {
|
|
23
|
-
* ····public log(message: string) { throw new Error(404); }
|
|
24
|
-
* ··};
|
|
25
|
-
* }
|
|
26
|
-
* ```
|
|
4
|
+
* Mixin function type - takes a base class and returns an extended class
|
|
27
5
|
*/
|
|
28
|
-
export type
|
|
6
|
+
export type MixinFn<TBase extends Constructor = Constructor, TResult extends TBase = TBase> = (Base: TBase) => TResult;
|
|
29
7
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* @typeParam T The type of the base class.
|
|
34
|
-
* @typeParam R The type of the returned class.
|
|
35
|
-
*/
|
|
36
|
-
export type MixinFunction<T extends MixinConstructor = MixinConstructor, R extends T = T & MixinConstructor> = (
|
|
8
|
+
export function mixin<T extends Constructor>(Base: T): T;
|
|
9
|
+
export function mixin<T extends Constructor, M1 extends MixinFn<T>>(Base: T, m1: M1): ReturnType<M1>;
|
|
10
|
+
export function mixin<T extends Constructor, M1 extends MixinFn<T>, M2 extends MixinFn<ReturnType<M1>>>(
|
|
37
11
|
Base: T,
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
12
|
+
m1: M1,
|
|
13
|
+
m2: M2,
|
|
14
|
+
): ReturnType<M2>;
|
|
15
|
+
export function mixin<
|
|
16
|
+
T extends Constructor,
|
|
17
|
+
M1 extends MixinFn<T>,
|
|
18
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
19
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
20
|
+
>(Base: T, m1: M1, m2: M2, m3: M3): ReturnType<M3>;
|
|
21
|
+
export function mixin<
|
|
22
|
+
T extends Constructor,
|
|
23
|
+
M1 extends MixinFn<T>,
|
|
24
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
25
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
26
|
+
M4 extends MixinFn<ReturnType<M3>>,
|
|
27
|
+
>(Base: T, m1: M1, m2: M2, m3: M3, m4: M4): ReturnType<M4>;
|
|
28
|
+
export function mixin<
|
|
29
|
+
T extends Constructor,
|
|
30
|
+
M1 extends MixinFn<T>,
|
|
31
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
32
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
33
|
+
M4 extends MixinFn<ReturnType<M3>>,
|
|
34
|
+
M5 extends MixinFn<ReturnType<M4>>,
|
|
35
|
+
>(Base: T, m1: M1, m2: M2, m3: M3, m4: M4, m5: M5): ReturnType<M5>;
|
|
36
|
+
export function mixin<
|
|
37
|
+
T extends Constructor,
|
|
38
|
+
M1 extends MixinFn<T>,
|
|
39
|
+
M2 extends MixinFn<ReturnType<M1>>,
|
|
40
|
+
M3 extends MixinFn<ReturnType<M2>>,
|
|
41
|
+
M4 extends MixinFn<ReturnType<M3>>,
|
|
42
|
+
M5 extends MixinFn<ReturnType<M4>>,
|
|
43
|
+
M6 extends MixinFn<ReturnType<M5>>,
|
|
44
|
+
>(Base: T, m1: M1, m2: M2, m3: M3, m4: M4, m5: M5, m6: M6): ReturnType<M6>;
|
|
57
45
|
|
|
58
46
|
/**
|
|
59
|
-
* Applies the given mixins to
|
|
47
|
+
* Applies the given mixins to a common base class.
|
|
60
48
|
*
|
|
61
49
|
* @param Base The base class to apply the mixins to.
|
|
62
|
-
* @param mixins The mixins to apply
|
|
50
|
+
* @param mixins The mixins to apply sequentially.
|
|
63
51
|
* @returns A class constructor with all mixins applied.
|
|
64
52
|
*
|
|
65
|
-
* @
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @example ```ts
|
|
69
|
-
* class Dog extends mixin(Animal, FourLegged, Carnivore, PackHunting, Barking, Domesticated) {}
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* class Dog extends mixin(Animal, FourLegged, Carnivore) {}
|
|
70
56
|
* ```
|
|
71
57
|
*/
|
|
72
|
-
export function mixin<T extends
|
|
73
|
-
Base
|
|
74
|
-
...mixins: M
|
|
75
|
-
): MixinReturnValue<T, M> {
|
|
76
|
-
return mixins.reduce((mix, applyMixin) => applyMixin(mix), Base) as MixinReturnValue<T, M>;
|
|
58
|
+
export function mixin<T extends Constructor>(Base: T, ...mixins: MixinFn<any>[]): Constructor {
|
|
59
|
+
return mixins.reduce((mix, applyMixin) => applyMixin(mix), Base as Constructor);
|
|
77
60
|
}
|
|
78
|
-
|
|
79
|
-
// https://github.com/1nVitr0/lib-ts-mixin-extended
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to convert a union to an intersection.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Constructor function, that creates a new instance of the given type.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T The type of the instance to create.
|
|
12
|
+
* @example ```ts
|
|
13
|
+
* function Walkable<TBase extends MixinConstructor<Positionable>>(Base: TBase) {
|
|
14
|
+
* ··return class Walkable extends Base {
|
|
15
|
+
* ····public forward() { this.x++; }
|
|
16
|
+
* ····public backward() { this.x--; }
|
|
17
|
+
* ··};
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
* @example ```ts
|
|
21
|
+
* function Loggable(Base: MixinConstructor) {
|
|
22
|
+
* ··return class Loggable extends Base {
|
|
23
|
+
* ····public log(message: string) { throw new Error(404); }
|
|
24
|
+
* ··};
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export type MixinConstructor<T = {}> = new (...args: any[]) => T;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Function that applies a mixin to a given base class.
|
|
32
|
+
*
|
|
33
|
+
* @typeParam T The type of the base class.
|
|
34
|
+
* @typeParam R The type of the returned class.
|
|
35
|
+
*/
|
|
36
|
+
export type MixinFunction<T extends MixinConstructor = MixinConstructor, R extends T = T & MixinConstructor> = (
|
|
37
|
+
Base: T,
|
|
38
|
+
) => R;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The return type of the mixin function.
|
|
42
|
+
*
|
|
43
|
+
* @typeParam T The type of the base class.
|
|
44
|
+
* @typeParam M The type of the mixin functions.
|
|
45
|
+
*/
|
|
46
|
+
export type MixinReturnValue<T extends MixinConstructor, M extends MixinFunction<T, any>[]> = UnionToIntersection<
|
|
47
|
+
T | { [K in keyof M]: M[K] extends MixinFunction<any, infer U> ? U : never }[number]
|
|
48
|
+
>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The instance created by a mixin function.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam F The type of the mixin function.
|
|
54
|
+
*/
|
|
55
|
+
export type MixinInstance<F extends MixinFunction<any>> =
|
|
56
|
+
F extends MixinFunction<MixinConstructor<any>, infer R> ? InstanceType<R> : never;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Applies the given mixins to the a common base class.
|
|
60
|
+
*
|
|
61
|
+
* @param Base The base class to apply the mixins to.
|
|
62
|
+
* @param mixins The mixins to apply. All mixins must extend a common base class or an empty class.
|
|
63
|
+
* @returns A class constructor with all mixins applied.
|
|
64
|
+
*
|
|
65
|
+
* @typeParam T The type of the base class.
|
|
66
|
+
* @typeParam M The type of the mixin functions.
|
|
67
|
+
*
|
|
68
|
+
* @example ```ts
|
|
69
|
+
* class Dog extends mixin(Animal, FourLegged, Carnivore, PackHunting, Barking, Domesticated) {}
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export function mixin<T extends MixinConstructor, M extends MixinFunction<T, any>[]>(
|
|
73
|
+
Base: T,
|
|
74
|
+
...mixins: M
|
|
75
|
+
): MixinReturnValue<T, M> {
|
|
76
|
+
return mixins.reduce((mix, applyMixin) => applyMixin(mix), Base) as MixinReturnValue<T, M>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// this is complex, may cause ts unable to resolve prototype typing
|
|
80
|
+
// https://github.com/1nVitr0/lib-ts-mixin-extended
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
export interface ParseBooleanOptions {
|
|
2
|
+
strict?: boolean;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function parseBoolean(s: string | boolean | number | null | undefined, options: { strict: true }): boolean | undefined;
|
|
6
|
+
export function parseBoolean(s: string | boolean | number | null | undefined | any, options?: ParseBooleanOptions): boolean;
|
|
7
|
+
/** @deprecated Use `parseBoolean(s, { strict: true })` instead */
|
|
1
8
|
export function parseBoolean(s: string | boolean | number | null | undefined, strict: true): boolean | undefined;
|
|
2
9
|
export function parseBoolean(s: string | boolean | number | null | undefined | any): boolean;
|
|
3
|
-
export function parseBoolean(s?: string | boolean | number | null,
|
|
10
|
+
export function parseBoolean(s?: string | boolean | number | null, options?: boolean | ParseBooleanOptions): boolean | undefined {
|
|
11
|
+
const strict = typeof options === 'boolean' ? options : options?.strict ?? false;
|
|
4
12
|
if (typeof s === 'boolean') {
|
|
5
13
|
return s;
|
|
6
14
|
}
|
package/src/objects/set.ts
CHANGED
|
@@ -13,7 +13,9 @@ export function set<T extends object, V>(obj: T, key: ObjectKey | ObjectPath, va
|
|
|
13
13
|
let x, k;
|
|
14
14
|
while (i < len) {
|
|
15
15
|
k = path[i++];
|
|
16
|
+
// Security: Prevent prototype pollution
|
|
16
17
|
if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
|
|
18
|
+
|
|
17
19
|
// noinspection PointlessArithmeticExpressionJS
|
|
18
20
|
current = current[k] =
|
|
19
21
|
i === len
|
|
@@ -22,7 +24,14 @@ export function set<T extends object, V>(obj: T, key: ObjectKey | ObjectPath, va
|
|
|
22
24
|
: val
|
|
23
25
|
: typeof (x = current[k]) === typeof path
|
|
24
26
|
? x
|
|
25
|
-
: //
|
|
27
|
+
: // Determine if we should create an Object or an Array for the next level
|
|
28
|
+
// If the next key is NOT an integer-like index, or contains a dot, create an Object.
|
|
29
|
+
// Otherwise, create an Array.
|
|
30
|
+
//
|
|
31
|
+
// path[i] * 0 !== 0 checks if it is NOT a number (NaN * 0 is NaN).
|
|
32
|
+
// !!~('' + path[i]).indexOf('.') checks if it contains a dot.
|
|
33
|
+
//
|
|
34
|
+
// @ts-expect-error hacky type check from dset
|
|
26
35
|
path[i] * 0 !== 0 || !!~('' + path[i]).indexOf('.') // eslint-disable-line
|
|
27
36
|
? {}
|
|
28
37
|
: [];
|
package/src/types.d.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -21,8 +21,11 @@
|
|
|
21
21
|
"isolatedModules": true,
|
|
22
22
|
"outDir": "lib",
|
|
23
23
|
"baseUrl": ".",
|
|
24
|
-
"rootDir": "./src"
|
|
24
|
+
"rootDir": "./src",
|
|
25
|
+
"paths": {
|
|
26
|
+
"#/*": ["./src/*"]
|
|
27
|
+
}
|
|
25
28
|
},
|
|
26
|
-
"exclude": ["node_modules"],
|
|
29
|
+
"exclude": ["node_modules", "**/*.test.ts", "**/*.spec.ts"],
|
|
27
30
|
"include": ["src/types.d.ts", "src"]
|
|
28
31
|
}
|