@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.
Files changed (57) hide show
  1. package/lib/arrays/arrayFromAsync.js +11 -3
  2. package/lib/asyncs/AsyncInterval.js +11 -3
  3. package/lib/asyncs/Promises.js +6 -5
  4. package/lib/asyncs/createAsyncIterator.js +11 -3
  5. package/lib/asyncs/createLazyPromise.test.js +11 -3
  6. package/lib/asyncs/generatorOfStream.js +11 -3
  7. package/lib/browsers/download.js +11 -3
  8. package/lib/browsers/loaders.js +11 -3
  9. package/lib/crypto/hashing.js +11 -3
  10. package/lib/crypto/hashing.test.js +11 -3
  11. package/lib/crypto/pem/pem.js +1 -1
  12. package/lib/fetch/createFetchWith.js +11 -3
  13. package/lib/fetch/dumpRequest.js +12 -4
  14. package/lib/fetch/dumpRequest.test.js +11 -3
  15. package/lib/fetch/dumpResponse.js +11 -3
  16. package/lib/fetch/dumpResponse.test.js +11 -3
  17. package/lib/io/ArrayBuffers.js +2 -2
  18. package/lib/io/ByteBuffer.test.js +11 -3
  19. package/lib/io/parseDataUri.js +11 -3
  20. package/lib/io/parseDataUri.test.js +31 -11
  21. package/lib/langs/AsyncCloser.js +11 -3
  22. package/lib/langs/deepFreeze.js +5 -5
  23. package/lib/langs/mixin.js +6 -12
  24. package/lib/langs/mixin.test.js +53 -5
  25. package/lib/langs/mixin2.js +26 -0
  26. package/lib/langs/parseBoolean.js +3 -2
  27. package/lib/langs/shallowEqual.js +5 -5
  28. package/lib/libs/ms.js +1 -1
  29. package/lib/maths/random.js +12 -4
  30. package/lib/objects/merge/isMergeableObject.js +1 -1
  31. package/lib/objects/merge/merge.js +1 -1
  32. package/lib/objects/merge/merge.test.js +11 -3
  33. package/lib/objects/set.js +10 -2
  34. package/lib/objects/set.test.js +2 -2
  35. package/lib/scripts/getGenerateContext.js +13 -5
  36. package/lib/server/fetch/createFetchWithProxyByNodeFetch.js +11 -3
  37. package/lib/server/fetch/createFetchWithProxyByUndici.js +11 -3
  38. package/lib/server/polyfill/polyfillBrowser.js +11 -3
  39. package/lib/server/polyfill/polyfillBrowser.test.js +11 -3
  40. package/lib/server/polyfill/polyfillCrypto.js +11 -3
  41. package/lib/server/polyfill/polyfillJsDom.js +31 -11
  42. package/lib/strings/renderTemplate.test.js +2 -2
  43. package/lib/web/getRandomValues.js +1 -1
  44. package/lib/web/structuredClone.js +2 -2
  45. package/package.json +8 -4
  46. package/src/asyncs/Promises.ts +2 -1
  47. package/src/asyncs/timeout.ts +1 -1
  48. package/src/crypto/hashing.ts +7 -6
  49. package/src/crypto/pem/pem.ts +3 -2
  50. package/src/fetch/dumpRequest.ts +1 -1
  51. package/src/langs/mixin.test.ts +35 -5
  52. package/src/langs/mixin.ts +46 -65
  53. package/src/langs/mixin2.ts +80 -0
  54. package/src/langs/parseBoolean.ts +9 -1
  55. package/src/objects/set.ts +10 -1
  56. package/src/types.d.ts +1 -1
  57. package/tsconfig.json +5 -2
@@ -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
- * 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
- * ```
4
+ * Mixin function type - takes a base class and returns an extended class
27
5
  */
28
- export type MixinConstructor<T = {}> = new (...args: any[]) => T;
6
+ export type MixinFn<TBase extends Constructor = Constructor, TResult extends TBase = TBase> = (Base: TBase) => TResult;
29
7
 
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> = (
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
- ) => 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;
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 the a common base class.
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. All mixins must extend a common base class or an empty class.
50
+ * @param mixins The mixins to apply sequentially.
63
51
  * @returns A class constructor with all mixins applied.
64
52
  *
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) {}
53
+ * @example
54
+ * ```ts
55
+ * class Dog extends mixin(Animal, FourLegged, Carnivore) {}
70
56
  * ```
71
57
  */
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>;
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, strict = false): boolean | undefined {
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
  }
@@ -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
- : // @ts-expect-error hacky type check
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
@@ -1,6 +1,6 @@
1
1
  declare var __DEV__: boolean;
2
2
 
3
- namespace NodeJS {
3
+ declare namespace NodeJS {
4
4
  interface Process {
5
5
  // webpack check
6
6
  readonly browser?: boolean;
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
  }