data-structure-typed 1.52.4 → 1.52.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/README_zh-CN.md +216 -26
  4. package/benchmark/report.html +13 -13
  5. package/benchmark/report.json +158 -158
  6. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +1 -37
  7. package/dist/cjs/data-structures/base/iterable-element-base.js +1 -37
  8. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  9. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +2 -54
  10. package/dist/cjs/data-structures/base/iterable-entry-base.js +1 -49
  11. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -32
  13. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +9 -41
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -46
  16. package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -46
  17. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +82 -147
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +299 -331
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -40
  22. package/dist/cjs/data-structures/binary-tree/bst.js +12 -44
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -48
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +2 -50
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +0 -32
  28. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +9 -41
  29. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  30. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -75
  31. package/dist/cjs/data-structures/graph/abstract-graph.js +0 -75
  32. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/directed-graph.d.ts +0 -98
  34. package/dist/cjs/data-structures/graph/directed-graph.js +0 -98
  35. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +0 -50
  37. package/dist/cjs/data-structures/graph/undirected-graph.js +0 -50
  38. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/hash/hash-map.d.ts +5 -92
  40. package/dist/cjs/data-structures/hash/hash-map.js +27 -111
  41. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  42. package/dist/cjs/data-structures/heap/heap.d.ts +0 -32
  43. package/dist/cjs/data-structures/heap/heap.js +0 -32
  44. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  45. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +5 -88
  46. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +5 -88
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  48. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +1 -83
  49. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +2 -84
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +1 -35
  52. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +1 -35
  53. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/deque.d.ts +1 -98
  55. package/dist/cjs/data-structures/queue/deque.js +3 -99
  56. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  57. package/dist/cjs/data-structures/queue/queue.d.ts +1 -54
  58. package/dist/cjs/data-structures/queue/queue.js +0 -53
  59. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  60. package/dist/cjs/data-structures/stack/stack.d.ts +1 -34
  61. package/dist/cjs/data-structures/stack/stack.js +1 -34
  62. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  63. package/dist/cjs/data-structures/tree/tree.js +0 -1
  64. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  65. package/dist/cjs/data-structures/trie/trie.d.ts +0 -64
  66. package/dist/cjs/data-structures/trie/trie.js +0 -64
  67. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  68. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +8 -0
  69. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +6 -0
  70. package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
  71. package/dist/cjs/types/utils/utils.d.ts +13 -12
  72. package/dist/cjs/utils/number.d.ts +13 -0
  73. package/dist/cjs/utils/number.js +13 -0
  74. package/dist/cjs/utils/number.js.map +1 -1
  75. package/dist/cjs/utils/utils.d.ts +125 -3
  76. package/dist/cjs/utils/utils.js +177 -21
  77. package/dist/cjs/utils/utils.js.map +1 -1
  78. package/dist/mjs/data-structures/base/iterable-element-base.d.ts +1 -37
  79. package/dist/mjs/data-structures/base/iterable-element-base.js +1 -37
  80. package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +2 -54
  81. package/dist/mjs/data-structures/base/iterable-entry-base.js +1 -49
  82. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -32
  83. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +9 -41
  84. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -46
  85. package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -46
  86. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +82 -147
  87. package/dist/mjs/data-structures/binary-tree/binary-tree.js +298 -332
  88. package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -40
  89. package/dist/mjs/data-structures/binary-tree/bst.js +12 -44
  90. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -48
  91. package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -50
  92. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +0 -32
  93. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +9 -41
  94. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -75
  95. package/dist/mjs/data-structures/graph/abstract-graph.js +0 -75
  96. package/dist/mjs/data-structures/graph/directed-graph.d.ts +0 -98
  97. package/dist/mjs/data-structures/graph/directed-graph.js +0 -98
  98. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +0 -50
  99. package/dist/mjs/data-structures/graph/undirected-graph.js +0 -50
  100. package/dist/mjs/data-structures/hash/hash-map.d.ts +5 -92
  101. package/dist/mjs/data-structures/hash/hash-map.js +27 -111
  102. package/dist/mjs/data-structures/heap/heap.d.ts +0 -32
  103. package/dist/mjs/data-structures/heap/heap.js +0 -32
  104. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +5 -88
  105. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +5 -88
  106. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +1 -83
  107. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +2 -84
  108. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +1 -35
  109. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +1 -35
  110. package/dist/mjs/data-structures/queue/deque.d.ts +1 -98
  111. package/dist/mjs/data-structures/queue/deque.js +3 -99
  112. package/dist/mjs/data-structures/queue/queue.d.ts +1 -54
  113. package/dist/mjs/data-structures/queue/queue.js +0 -53
  114. package/dist/mjs/data-structures/stack/stack.d.ts +1 -34
  115. package/dist/mjs/data-structures/stack/stack.js +1 -34
  116. package/dist/mjs/data-structures/tree/tree.js +0 -1
  117. package/dist/mjs/data-structures/trie/trie.d.ts +0 -64
  118. package/dist/mjs/data-structures/trie/trie.js +0 -64
  119. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +8 -0
  120. package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +5 -1
  121. package/dist/mjs/types/utils/utils.d.ts +13 -12
  122. package/dist/mjs/utils/number.d.ts +13 -0
  123. package/dist/mjs/utils/number.js +13 -0
  124. package/dist/mjs/utils/utils.d.ts +125 -3
  125. package/dist/mjs/utils/utils.js +177 -21
  126. package/dist/umd/data-structure-typed.js +408 -1474
  127. package/dist/umd/data-structure-typed.min.js +5 -4
  128. package/dist/umd/data-structure-typed.min.js.map +1 -1
  129. package/package.json +6 -6
  130. package/src/data-structures/base/iterable-element-base.ts +2 -42
  131. package/src/data-structures/base/iterable-entry-base.ts +3 -62
  132. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +8 -48
  133. package/src/data-structures/binary-tree/avl-tree.ts +0 -57
  134. package/src/data-structures/binary-tree/binary-tree.ts +329 -358
  135. package/src/data-structures/binary-tree/bst.ts +11 -54
  136. package/src/data-structures/binary-tree/rb-tree.ts +2 -62
  137. package/src/data-structures/binary-tree/tree-multi-map.ts +8 -48
  138. package/src/data-structures/graph/abstract-graph.ts +0 -92
  139. package/src/data-structures/graph/directed-graph.ts +0 -122
  140. package/src/data-structures/graph/undirected-graph.ts +0 -62
  141. package/src/data-structures/hash/hash-map.ts +29 -133
  142. package/src/data-structures/heap/heap.ts +0 -40
  143. package/src/data-structures/linked-list/doubly-linked-list.ts +5 -112
  144. package/src/data-structures/linked-list/singly-linked-list.ts +2 -104
  145. package/src/data-structures/linked-list/skip-linked-list.ts +1 -44
  146. package/src/data-structures/queue/deque.ts +2 -125
  147. package/src/data-structures/queue/queue.ts +1 -68
  148. package/src/data-structures/stack/stack.ts +1 -43
  149. package/src/data-structures/tree/tree.ts +1 -1
  150. package/src/data-structures/trie/trie.ts +0 -80
  151. package/src/types/data-structures/binary-tree/binary-tree.ts +8 -1
  152. package/src/types/utils/utils.ts +17 -15
  153. package/src/utils/number.ts +13 -0
  154. package/src/utils/utils.ts +174 -18
  155. package/test/config.ts +8 -0
  156. package/test/integration/all-in-one.test.ts +1 -1
  157. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +13 -13
  158. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +18 -13
  159. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +471 -64
  160. package/test/unit/data-structures/binary-tree/bst.test.ts +167 -23
  161. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  162. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +136 -13
  163. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +18 -13
  164. package/test/unit/data-structures/graph/directed-graph.test.ts +46 -32
  165. package/test/unit/data-structures/graph/map-graph.test.ts +24 -2
  166. package/test/unit/data-structures/graph/undirected-graph.test.ts +24 -24
  167. package/test/unit/data-structures/hash/hash-map.test.ts +225 -35
  168. package/test/unit/data-structures/heap/heap.test.ts +47 -39
  169. package/test/unit/data-structures/heap/min-heap.test.ts +5 -5
  170. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +34 -4
  171. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +32 -0
  172. package/test/unit/data-structures/matrix/matrix.test.ts +35 -5
  173. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +31 -0
  174. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +18 -0
  175. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +17 -0
  176. package/test/unit/data-structures/queue/deque.test.ts +288 -47
  177. package/test/unit/data-structures/queue/queue.test.ts +62 -37
  178. package/test/unit/data-structures/stack/stack.test.ts +30 -5
  179. package/test/unit/data-structures/tree/tree.test.ts +58 -0
  180. package/test/unit/data-structures/trie/trie.test.ts +46 -5
  181. package/test/unit/utils/utils.test.ts +169 -0
@@ -1,17 +1,18 @@
1
- export type ToThunkFn = () => ReturnType<TrlFn>;
2
- export type Thunk = () => ReturnType<ToThunkFn> & {
3
- __THUNK__: symbol;
1
+ export type ToThunkFn<R = any> = () => R;
2
+ export type Thunk<R = any> = ToThunkFn<R> & {
3
+ __THUNK__?: symbol;
4
4
  };
5
- export type TrlFn = (...args: any[]) => any;
5
+ export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
6
6
  export type TrlAsyncFn = (...args: any[]) => any;
7
7
  export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
8
8
  export type Any = string | number | bigint | boolean | symbol | undefined | object;
9
- export type Comparable = number | string | bigint | boolean | ({
9
+ export type ComparablePrimitive = number | bigint | string | boolean;
10
+ export type ComparableObject = {
10
11
  [key in string]: any;
11
- } & {
12
- valueOf(): Comparable;
13
- }) | ({
14
- [key in string]: any;
15
- } & {
16
- toString(): Comparable;
17
- }) | (() => Comparable);
12
+ } & ({
13
+ valueOf: () => ComparablePrimitive | ComparableObject;
14
+ toString?: () => string;
15
+ } | {
16
+ toString: () => string;
17
+ });
18
+ export type Comparable = ComparablePrimitive | Date | ComparableObject;
@@ -1 +1,14 @@
1
+ /**
2
+ * The function `toBinaryString` converts a number to a binary string representation with a specified
3
+ * number of digits.
4
+ * @param {number} num - The `num` parameter in the `toBinaryString` function represents the number
5
+ * that you want to convert to a binary string.
6
+ * @param [digit=32] - The `digit` parameter in the `toBinaryString` function represents the number of
7
+ * digits the binary string should have. By default, it is set to 32, meaning that the binary string
8
+ * will be padded with zeros at the beginning to ensure it is 32 bits long. You can provide a
9
+ * @returns The function `toBinaryString` takes a number as input and converts it to a binary string
10
+ * representation with a specified number of digits (default is 32). The binary string is padded with
11
+ * zeros at the beginning to ensure it has the specified number of digits. The function returns the
12
+ * binary string representation of the input number.
13
+ */
1
14
  export declare function toBinaryString(num: number, digit?: number): string;
@@ -1,3 +1,16 @@
1
+ /**
2
+ * The function `toBinaryString` converts a number to a binary string representation with a specified
3
+ * number of digits.
4
+ * @param {number} num - The `num` parameter in the `toBinaryString` function represents the number
5
+ * that you want to convert to a binary string.
6
+ * @param [digit=32] - The `digit` parameter in the `toBinaryString` function represents the number of
7
+ * digits the binary string should have. By default, it is set to 32, meaning that the binary string
8
+ * will be padded with zeros at the beginning to ensure it is 32 bits long. You can provide a
9
+ * @returns The function `toBinaryString` takes a number as input and converts it to a binary string
10
+ * representation with a specified number of digits (default is 32). The binary string is padded with
11
+ * zeros at the beginning to ensure it has the specified number of digits. The function returns the
12
+ * binary string representation of the input number.
13
+ */
1
14
  export function toBinaryString(num, digit = 32) {
2
15
  // Convert number to binary string
3
16
  let binaryString = (num >>> 0).toString(2); // Use the unsigned right shift operator to ensure you get a binary representation of a 32-bit unsigned integer
@@ -6,21 +6,143 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { Comparable, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
9
+ /**
10
+ * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
11
+ * @returns A randomly generated UUID (Universally Unique Identifier) in the format
12
+ * 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' where each 'x' is replaced with a random hexadecimal
13
+ * character.
14
+ */
9
15
  export declare const uuidV4: () => string;
16
+ /**
17
+ * The `arrayRemove` function removes elements from an array based on a specified predicate function
18
+ * and returns the removed elements.
19
+ * @param {T[]} array - An array of elements that you want to filter based on the provided predicate
20
+ * function.
21
+ * @param predicate - The `predicate` parameter is a function that takes three arguments:
22
+ * @returns The `arrayRemove` function returns an array containing the elements that satisfy the given
23
+ * `predicate` function.
24
+ */
10
25
  export declare const arrayRemove: <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean) => T[];
11
26
  export declare const THUNK_SYMBOL: unique symbol;
27
+ /**
28
+ * The function `isThunk` checks if a given value is a function with a specific symbol property.
29
+ * @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
30
+ * function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
31
+ * around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
32
+ * @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
33
+ * property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
34
+ * met, otherwise it will be `false`.
35
+ */
12
36
  export declare const isThunk: (fnOrValue: any) => boolean;
37
+ /**
38
+ * The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
39
+ * @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
40
+ * @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
41
+ * of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
42
+ * function takes a function `fn` as an argument and returns a thunk function that, when called, will
43
+ * execute the `fn` function provided as an argument.
44
+ */
13
45
  export declare const toThunk: (fn: ToThunkFn) => Thunk;
46
+ /**
47
+ * The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
48
+ * stack overflow.
49
+ * @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
50
+ * number of arguments and returns a value.
51
+ * @returns The `trampoline` function returns an object with two properties:
52
+ * 1. A function that executes the provided function `fn` and continues to execute any thunks returned
53
+ * by `fn` until a non-thunk value is returned.
54
+ * 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
55
+ */
14
56
  export declare const trampoline: (fn: TrlFn) => ((...args: [...Parameters<TrlFn>]) => any) & {
15
- cont: (...args: [...Parameters<TrlFn>]) => Thunk;
57
+ cont: (...args: [...Parameters<TrlFn>]) => ReturnType<TrlFn>;
16
58
  };
59
+ /**
60
+ * The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
61
+ * function.
62
+ * @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
63
+ * function that returns a Promise. This function will be called recursively until a non-thunk value is
64
+ * returned.
65
+ * @returns The `trampolineAsync` function returns an object with two properties:
66
+ * 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
67
+ * thunks returned by the function until a non-thunk value is returned.
68
+ * 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
69
+ * and returns it.
70
+ */
17
71
  export declare const trampolineAsync: (fn: TrlAsyncFn) => ((...args: [...Parameters<TrlAsyncFn>]) => Promise<any>) & {
18
- cont: (...args: [...Parameters<TrlAsyncFn>]) => Thunk;
72
+ cont: (...args: [...Parameters<TrlAsyncFn>]) => ReturnType<TrlAsyncFn>;
19
73
  };
74
+ /**
75
+ * The function `getMSB` returns the most significant bit of a given number.
76
+ * @param {number} value - The `value` parameter is a number for which we want to find the position of
77
+ * the Most Significant Bit (MSB). The function `getMSB` takes this number as input and calculates the
78
+ * position of the MSB in its binary representation.
79
+ * @returns The function `getMSB` returns the most significant bit (MSB) of the input `value`. If the
80
+ * input value is less than or equal to 0, it returns 0. Otherwise, it calculates the position of the
81
+ * MSB using the `Math.clz32` function and bitwise left shifts 1 to that position.
82
+ */
20
83
  export declare const getMSB: (value: number) => number;
84
+ /**
85
+ * The `rangeCheck` function in TypeScript is used to validate if an index is within a specified range
86
+ * and throws a `RangeError` with a custom message if it is out of bounds.
87
+ * @param {number} index - The `index` parameter represents the value that you want to check if it
88
+ * falls within a specified range.
89
+ * @param {number} min - The `min` parameter represents the minimum value that the `index` should be
90
+ * compared against in the `rangeCheck` function.
91
+ * @param {number} max - The `max` parameter in the `rangeCheck` function represents the maximum value
92
+ * that the `index` parameter is allowed to have. If the `index` is greater than this `max` value, a
93
+ * `RangeError` will be thrown.
94
+ * @param [message=Index out of bounds.] - The `message` parameter is a string that represents the
95
+ * error message to be thrown if the index is out of bounds. By default, if no message is provided when
96
+ * calling the `rangeCheck` function, the message "Index out of bounds." will be used.
97
+ */
21
98
  export declare const rangeCheck: (index: number, min: number, max: number, message?: string) => void;
99
+ /**
100
+ * The function `throwRangeError` throws a RangeError with a custom message if called.
101
+ * @param [message=The value is off-limits.] - The `message` parameter is a string that represents the
102
+ * error message to be displayed when a `RangeError` is thrown. If no message is provided, the default
103
+ * message is 'The value is off-limits.'.
104
+ */
22
105
  export declare const throwRangeError: (message?: string) => void;
106
+ /**
107
+ * The function `isWeakKey` checks if the input is an object or a function in TypeScript.
108
+ * @param {unknown} input - The `input` parameter in the `isWeakKey` function is of type `unknown`,
109
+ * which means it can be any type. The function checks if the `input` is an object (excluding `null`)
110
+ * or a function, and returns a boolean indicating whether the `input` is a weak
111
+ * @returns The function `isWeakKey` returns a boolean value indicating whether the input is an object
112
+ * or a function.
113
+ */
23
114
  export declare const isWeakKey: (input: unknown) => input is object;
115
+ /**
116
+ * The function `calcMinUnitsRequired` calculates the minimum number of units required to accommodate a
117
+ * given total quantity based on a specified unit size.
118
+ * @param {number} totalQuantity - The `totalQuantity` parameter represents the total quantity of items
119
+ * that need to be processed or handled.
120
+ * @param {number} unitSize - The `unitSize` parameter represents the size of each unit or package. It
121
+ * is used in the `calcMinUnitsRequired` function to calculate the minimum number of units required to
122
+ * accommodate a total quantity of items.
123
+ */
24
124
  export declare const calcMinUnitsRequired: (totalQuantity: number, unitSize: number) => number;
125
+ /**
126
+ * The `roundFixed` function in TypeScript rounds a number to a specified number of decimal places.
127
+ * @param {number} num - The `num` parameter is a number that you want to round to a certain number of
128
+ * decimal places.
129
+ * @param {number} [digit=10] - The `digit` parameter in the `roundFixed` function specifies the number
130
+ * of decimal places to round the number to. By default, it is set to 10 if not provided explicitly.
131
+ * @returns The function `roundFixed` returns a number that is rounded to the specified number of
132
+ * decimal places (default is 10 decimal places).
133
+ */
25
134
  export declare const roundFixed: (num: number, digit?: number) => number;
26
- export declare function isComparable(key: any): key is Comparable;
135
+ /**
136
+ * The function `isComparable` in TypeScript checks if a value is comparable, handling primitive values
137
+ * and objects with optional force comparison.
138
+ * @param {unknown} value - The `value` parameter in the `isComparable` function represents the value
139
+ * that you want to check if it is comparable. It can be of any type (`unknown`), and the function will
140
+ * determine if it is comparable based on certain conditions.
141
+ * @param [isForceObjectComparable=false] - The `isForceObjectComparable` parameter in the
142
+ * `isComparable` function is a boolean flag that determines whether to treat non-primitive values as
143
+ * comparable objects. When set to `true`, it forces the function to consider non-primitive values as
144
+ * comparable objects, regardless of their type.
145
+ * @returns The function `isComparable` returns a boolean value indicating whether the `value` is
146
+ * considered comparable or not.
147
+ */
148
+ export declare function isComparable(value: unknown, isForceObjectComparable?: boolean): value is Comparable;
@@ -1,9 +1,24 @@
1
+ /**
2
+ * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
3
+ * @returns A randomly generated UUID (Universally Unique Identifier) in the format
4
+ * 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' where each 'x' is replaced with a random hexadecimal
5
+ * character.
6
+ */
1
7
  export const uuidV4 = function () {
2
8
  return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
3
9
  const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
4
10
  return v.toString(16);
5
11
  });
6
12
  };
13
+ /**
14
+ * The `arrayRemove` function removes elements from an array based on a specified predicate function
15
+ * and returns the removed elements.
16
+ * @param {T[]} array - An array of elements that you want to filter based on the provided predicate
17
+ * function.
18
+ * @param predicate - The `predicate` parameter is a function that takes three arguments:
19
+ * @returns The `arrayRemove` function returns an array containing the elements that satisfy the given
20
+ * `predicate` function.
21
+ */
7
22
  export const arrayRemove = function (array, predicate) {
8
23
  let i = -1, len = array ? array.length : 0;
9
24
  const result = [];
@@ -18,14 +33,41 @@ export const arrayRemove = function (array, predicate) {
18
33
  return result;
19
34
  };
20
35
  export const THUNK_SYMBOL = Symbol('thunk');
36
+ /**
37
+ * The function `isThunk` checks if a given value is a function with a specific symbol property.
38
+ * @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
39
+ * function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
40
+ * around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
41
+ * @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
42
+ * property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
43
+ * met, otherwise it will be `false`.
44
+ */
21
45
  export const isThunk = (fnOrValue) => {
22
46
  return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL;
23
47
  };
48
+ /**
49
+ * The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
50
+ * @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
51
+ * @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
52
+ * of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
53
+ * function takes a function `fn` as an argument and returns a thunk function that, when called, will
54
+ * execute the `fn` function provided as an argument.
55
+ */
24
56
  export const toThunk = (fn) => {
25
57
  const thunk = () => fn();
26
58
  thunk.__THUNK__ = THUNK_SYMBOL;
27
59
  return thunk;
28
60
  };
61
+ /**
62
+ * The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
63
+ * stack overflow.
64
+ * @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
65
+ * number of arguments and returns a value.
66
+ * @returns The `trampoline` function returns an object with two properties:
67
+ * 1. A function that executes the provided function `fn` and continues to execute any thunks returned
68
+ * by `fn` until a non-thunk value is returned.
69
+ * 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
70
+ */
29
71
  export const trampoline = (fn) => {
30
72
  const cont = (...args) => toThunk(() => fn(...args));
31
73
  return Object.assign((...args) => {
@@ -36,6 +78,18 @@ export const trampoline = (fn) => {
36
78
  return result;
37
79
  }, { cont });
38
80
  };
81
+ /**
82
+ * The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
83
+ * function.
84
+ * @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
85
+ * function that returns a Promise. This function will be called recursively until a non-thunk value is
86
+ * returned.
87
+ * @returns The `trampolineAsync` function returns an object with two properties:
88
+ * 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
89
+ * thunks returned by the function until a non-thunk value is returned.
90
+ * 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
91
+ * and returns it.
92
+ */
39
93
  export const trampolineAsync = (fn) => {
40
94
  const cont = (...args) => toThunk(() => fn(...args));
41
95
  return Object.assign(async (...args) => {
@@ -46,50 +100,152 @@ export const trampolineAsync = (fn) => {
46
100
  return result;
47
101
  }, { cont });
48
102
  };
103
+ /**
104
+ * The function `getMSB` returns the most significant bit of a given number.
105
+ * @param {number} value - The `value` parameter is a number for which we want to find the position of
106
+ * the Most Significant Bit (MSB). The function `getMSB` takes this number as input and calculates the
107
+ * position of the MSB in its binary representation.
108
+ * @returns The function `getMSB` returns the most significant bit (MSB) of the input `value`. If the
109
+ * input value is less than or equal to 0, it returns 0. Otherwise, it calculates the position of the
110
+ * MSB using the `Math.clz32` function and bitwise left shifts 1 to that position.
111
+ */
49
112
  export const getMSB = (value) => {
50
113
  if (value <= 0) {
51
114
  return 0;
52
115
  }
53
116
  return 1 << (31 - Math.clz32(value));
54
117
  };
118
+ /**
119
+ * The `rangeCheck` function in TypeScript is used to validate if an index is within a specified range
120
+ * and throws a `RangeError` with a custom message if it is out of bounds.
121
+ * @param {number} index - The `index` parameter represents the value that you want to check if it
122
+ * falls within a specified range.
123
+ * @param {number} min - The `min` parameter represents the minimum value that the `index` should be
124
+ * compared against in the `rangeCheck` function.
125
+ * @param {number} max - The `max` parameter in the `rangeCheck` function represents the maximum value
126
+ * that the `index` parameter is allowed to have. If the `index` is greater than this `max` value, a
127
+ * `RangeError` will be thrown.
128
+ * @param [message=Index out of bounds.] - The `message` parameter is a string that represents the
129
+ * error message to be thrown if the index is out of bounds. By default, if no message is provided when
130
+ * calling the `rangeCheck` function, the message "Index out of bounds." will be used.
131
+ */
55
132
  export const rangeCheck = (index, min, max, message = 'Index out of bounds.') => {
56
133
  if (index < min || index > max)
57
134
  throw new RangeError(message);
58
135
  };
136
+ /**
137
+ * The function `throwRangeError` throws a RangeError with a custom message if called.
138
+ * @param [message=The value is off-limits.] - The `message` parameter is a string that represents the
139
+ * error message to be displayed when a `RangeError` is thrown. If no message is provided, the default
140
+ * message is 'The value is off-limits.'.
141
+ */
59
142
  export const throwRangeError = (message = 'The value is off-limits.') => {
60
143
  throw new RangeError(message);
61
144
  };
145
+ /**
146
+ * The function `isWeakKey` checks if the input is an object or a function in TypeScript.
147
+ * @param {unknown} input - The `input` parameter in the `isWeakKey` function is of type `unknown`,
148
+ * which means it can be any type. The function checks if the `input` is an object (excluding `null`)
149
+ * or a function, and returns a boolean indicating whether the `input` is a weak
150
+ * @returns The function `isWeakKey` returns a boolean value indicating whether the input is an object
151
+ * or a function.
152
+ */
62
153
  export const isWeakKey = (input) => {
63
154
  const inputType = typeof input;
64
155
  return (inputType === 'object' && input !== null) || inputType === 'function';
65
156
  };
157
+ /**
158
+ * The function `calcMinUnitsRequired` calculates the minimum number of units required to accommodate a
159
+ * given total quantity based on a specified unit size.
160
+ * @param {number} totalQuantity - The `totalQuantity` parameter represents the total quantity of items
161
+ * that need to be processed or handled.
162
+ * @param {number} unitSize - The `unitSize` parameter represents the size of each unit or package. It
163
+ * is used in the `calcMinUnitsRequired` function to calculate the minimum number of units required to
164
+ * accommodate a total quantity of items.
165
+ */
66
166
  export const calcMinUnitsRequired = (totalQuantity, unitSize) => Math.floor((totalQuantity + unitSize - 1) / unitSize);
167
+ /**
168
+ * The `roundFixed` function in TypeScript rounds a number to a specified number of decimal places.
169
+ * @param {number} num - The `num` parameter is a number that you want to round to a certain number of
170
+ * decimal places.
171
+ * @param {number} [digit=10] - The `digit` parameter in the `roundFixed` function specifies the number
172
+ * of decimal places to round the number to. By default, it is set to 10 if not provided explicitly.
173
+ * @returns The function `roundFixed` returns a number that is rounded to the specified number of
174
+ * decimal places (default is 10 decimal places).
175
+ */
67
176
  export const roundFixed = (num, digit = 10) => {
68
177
  const multiplier = Math.pow(10, digit);
69
178
  return Math.round(num * multiplier) / multiplier;
70
179
  };
71
- export function isComparable(key) {
72
- const keyType = typeof key;
73
- if (keyType === 'number')
74
- return !isNaN(key);
75
- if (keyType === 'string')
76
- return true;
77
- if (keyType === 'bigint')
78
- return true;
79
- if (keyType === 'boolean')
80
- return true;
81
- if (keyType === 'symbol')
180
+ /**
181
+ * The function `isPrimitiveComparable` checks if a value is a primitive type that can be compared.
182
+ * @param {unknown} value - The `value` parameter in the `isPrimitiveComparable` function is of type
183
+ * `unknown`, which means it can be any type. The function checks if the `value` is a primitive type
184
+ * that can be compared, such as number, bigint, string, or boolean.
185
+ * @returns The function `isPrimitiveComparable` returns a boolean value indicating whether the input
186
+ * `value` is a primitive value that can be compared using standard comparison operators (<, >, <=,
187
+ * >=).
188
+ */
189
+ function isPrimitiveComparable(value) {
190
+ const valueType = typeof value;
191
+ if (valueType === 'number')
192
+ return !Number.isNaN(value);
193
+ return valueType === 'bigint' || valueType === 'string' || valueType === 'boolean';
194
+ }
195
+ /**
196
+ * The function `tryObjectToPrimitive` attempts to convert an object to a comparable primitive value by
197
+ * first checking the `valueOf` method and then the `toString` method.
198
+ * @param {object} obj - The `obj` parameter in the `tryObjectToPrimitive` function is an object that
199
+ * you want to convert to a primitive value. The function attempts to convert the object to a primitive
200
+ * value by first checking if the object has a `valueOf` method. If the `valueOf` method exists, it
201
+ * @returns The function `tryObjectToPrimitive` returns a value of type `ComparablePrimitive` if a
202
+ * primitive comparable value is found within the object, or a string value if the object has a custom
203
+ * `toString` method that does not return `'[object Object]'`. If neither condition is met, the
204
+ * function returns `null`.
205
+ */
206
+ function tryObjectToPrimitive(obj) {
207
+ if (typeof obj.valueOf === 'function') {
208
+ const valueOfResult = obj.valueOf();
209
+ if (valueOfResult !== obj) {
210
+ if (isPrimitiveComparable(valueOfResult))
211
+ return valueOfResult;
212
+ if (typeof valueOfResult === 'object' && valueOfResult !== null)
213
+ return tryObjectToPrimitive(valueOfResult);
214
+ }
215
+ }
216
+ if (typeof obj.toString === 'function') {
217
+ const stringResult = obj.toString();
218
+ if (stringResult !== '[object Object]')
219
+ return stringResult;
220
+ }
221
+ return null;
222
+ }
223
+ /**
224
+ * The function `isComparable` in TypeScript checks if a value is comparable, handling primitive values
225
+ * and objects with optional force comparison.
226
+ * @param {unknown} value - The `value` parameter in the `isComparable` function represents the value
227
+ * that you want to check if it is comparable. It can be of any type (`unknown`), and the function will
228
+ * determine if it is comparable based on certain conditions.
229
+ * @param [isForceObjectComparable=false] - The `isForceObjectComparable` parameter in the
230
+ * `isComparable` function is a boolean flag that determines whether to treat non-primitive values as
231
+ * comparable objects. When set to `true`, it forces the function to consider non-primitive values as
232
+ * comparable objects, regardless of their type.
233
+ * @returns The function `isComparable` returns a boolean value indicating whether the `value` is
234
+ * considered comparable or not.
235
+ */
236
+ export function isComparable(value, isForceObjectComparable = false) {
237
+ if (value === null || value === undefined)
82
238
  return false;
83
- if (keyType === 'undefined')
239
+ if (isPrimitiveComparable(value))
240
+ return true;
241
+ if (typeof value !== 'object')
84
242
  return false;
85
- if (keyType === 'function')
86
- return isComparable(key());
87
- if (keyType === 'object') {
88
- if (key === null)
89
- return true;
90
- // if (typeof key.valueOf === 'function') return isComparable(key.valueOf()); // This will keep recursing because every object has a valueOf method.
91
- // if (typeof key.toString === 'function') return isComparable(key.toString()); // This will also keep recursing because every string type has a toString method.
243
+ if (value instanceof Date)
244
+ return !Number.isNaN(value.getTime());
245
+ if (isForceObjectComparable)
246
+ return true;
247
+ const comparableValue = tryObjectToPrimitive(value);
248
+ if (comparableValue === null || comparableValue === undefined)
92
249
  return false;
93
- }
94
- return false;
250
+ return isPrimitiveComparable(comparableValue);
95
251
  }