min-heap-typed 1.52.3 → 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 (74) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +1 -37
  2. package/dist/data-structures/base/iterable-element-base.js +1 -37
  3. package/dist/data-structures/base/iterable-entry-base.d.ts +2 -54
  4. package/dist/data-structures/base/iterable-entry-base.js +1 -49
  5. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -32
  6. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -41
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -46
  8. package/dist/data-structures/binary-tree/avl-tree.js +0 -46
  9. package/dist/data-structures/binary-tree/binary-tree.d.ts +82 -147
  10. package/dist/data-structures/binary-tree/binary-tree.js +299 -331
  11. package/dist/data-structures/binary-tree/bst.d.ts +1 -40
  12. package/dist/data-structures/binary-tree/bst.js +12 -44
  13. package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -48
  14. package/dist/data-structures/binary-tree/rb-tree.js +2 -50
  15. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -32
  16. package/dist/data-structures/binary-tree/tree-multi-map.js +9 -41
  17. package/dist/data-structures/graph/abstract-graph.d.ts +0 -75
  18. package/dist/data-structures/graph/abstract-graph.js +0 -75
  19. package/dist/data-structures/graph/directed-graph.d.ts +0 -98
  20. package/dist/data-structures/graph/directed-graph.js +0 -98
  21. package/dist/data-structures/graph/undirected-graph.d.ts +0 -50
  22. package/dist/data-structures/graph/undirected-graph.js +0 -50
  23. package/dist/data-structures/hash/hash-map.d.ts +5 -92
  24. package/dist/data-structures/hash/hash-map.js +29 -115
  25. package/dist/data-structures/heap/heap.d.ts +0 -32
  26. package/dist/data-structures/heap/heap.js +0 -32
  27. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +5 -88
  28. package/dist/data-structures/linked-list/doubly-linked-list.js +5 -88
  29. package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -83
  30. package/dist/data-structures/linked-list/singly-linked-list.js +2 -84
  31. package/dist/data-structures/linked-list/skip-linked-list.d.ts +1 -35
  32. package/dist/data-structures/linked-list/skip-linked-list.js +1 -35
  33. package/dist/data-structures/queue/deque.d.ts +1 -98
  34. package/dist/data-structures/queue/deque.js +3 -99
  35. package/dist/data-structures/queue/queue.d.ts +5 -58
  36. package/dist/data-structures/queue/queue.js +4 -57
  37. package/dist/data-structures/stack/stack.d.ts +1 -34
  38. package/dist/data-structures/stack/stack.js +1 -34
  39. package/dist/data-structures/tree/tree.js +2 -1
  40. package/dist/data-structures/trie/trie.d.ts +0 -64
  41. package/dist/data-structures/trie/trie.js +0 -64
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +8 -0
  43. package/dist/types/data-structures/binary-tree/binary-tree.js +6 -0
  44. package/dist/types/utils/utils.d.ts +13 -12
  45. package/dist/utils/number.d.ts +13 -0
  46. package/dist/utils/number.js +13 -0
  47. package/dist/utils/utils.d.ts +125 -3
  48. package/dist/utils/utils.js +177 -21
  49. package/package.json +2 -2
  50. package/src/data-structures/base/iterable-element-base.ts +2 -42
  51. package/src/data-structures/base/iterable-entry-base.ts +3 -62
  52. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +8 -48
  53. package/src/data-structures/binary-tree/avl-tree.ts +0 -57
  54. package/src/data-structures/binary-tree/binary-tree.ts +329 -358
  55. package/src/data-structures/binary-tree/bst.ts +11 -54
  56. package/src/data-structures/binary-tree/rb-tree.ts +2 -62
  57. package/src/data-structures/binary-tree/tree-multi-map.ts +8 -48
  58. package/src/data-structures/graph/abstract-graph.ts +0 -92
  59. package/src/data-structures/graph/directed-graph.ts +0 -122
  60. package/src/data-structures/graph/undirected-graph.ts +0 -62
  61. package/src/data-structures/hash/hash-map.ts +31 -139
  62. package/src/data-structures/heap/heap.ts +0 -40
  63. package/src/data-structures/linked-list/doubly-linked-list.ts +5 -112
  64. package/src/data-structures/linked-list/singly-linked-list.ts +2 -104
  65. package/src/data-structures/linked-list/skip-linked-list.ts +1 -44
  66. package/src/data-structures/queue/deque.ts +2 -125
  67. package/src/data-structures/queue/queue.ts +5 -72
  68. package/src/data-structures/stack/stack.ts +1 -43
  69. package/src/data-structures/tree/tree.ts +1 -1
  70. package/src/data-structures/trie/trie.ts +0 -80
  71. package/src/types/data-structures/binary-tree/binary-tree.ts +8 -1
  72. package/src/types/utils/utils.ts +17 -15
  73. package/src/utils/number.ts +13 -0
  74. package/src/utils/utils.ts +174 -18
@@ -147,11 +147,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
147
147
  return this._root;
148
148
  }
149
149
 
150
- /**
151
- * Time Complexity: O(l), where l is the length of the word being added.
152
- * Space Complexity: O(l) - Each character in the word adds a TrieNode.
153
- */
154
-
155
150
  /**
156
151
  * Time Complexity: O(l), where l is the length of the word being added.
157
152
  * Space Complexity: O(l) - Each character in the word adds a TrieNode.
@@ -180,11 +175,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
180
175
  return isNewWord;
181
176
  }
182
177
 
183
- /**
184
- * Time Complexity: O(l), where l is the length of the input word.
185
- * Space Complexity: O(1) - Constant space.
186
- */
187
-
188
178
  /**
189
179
  * Time Complexity: O(l), where l is the length of the input word.
190
180
  * Space Complexity: O(1) - Constant space.
@@ -204,11 +194,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
204
194
  return cur.isEnd;
205
195
  }
206
196
 
207
- /**
208
- * Time Complexity: O(1)
209
- * Space Complexity: O(1)
210
- */
211
-
212
197
  /**
213
198
  * Time Complexity: O(1)
214
199
  * Space Complexity: O(1)
@@ -220,11 +205,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
220
205
  return this.size === 0;
221
206
  }
222
207
 
223
- /**
224
- * Time Complexity: O(1)
225
- * Space Complexity: O(1)
226
- */
227
-
228
208
  /**
229
209
  * Time Complexity: O(1)
230
210
  * Space Complexity: O(1)
@@ -236,11 +216,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
236
216
  this._root = new TrieNode('');
237
217
  }
238
218
 
239
- /**
240
- * Time Complexity: O(l), where l is the length of the word being deleted.
241
- * Space Complexity: O(n) - Due to the recursive DFS approach.
242
- */
243
-
244
219
  /**
245
220
  * Time Complexity: O(l), where l is the length of the word being deleted.
246
221
  * Space Complexity: O(n) - Due to the recursive DFS approach.
@@ -285,11 +260,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
285
260
  return isDeleted;
286
261
  }
287
262
 
288
- /**
289
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
290
- * Space Complexity: O(1) - Constant space.
291
- */
292
-
293
263
  /**
294
264
  * Time Complexity: O(n), where n is the total number of nodes in the trie.
295
265
  * Space Complexity: O(1) - Constant space.
@@ -315,11 +285,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
315
285
  return maxDepth;
316
286
  }
317
287
 
318
- /**
319
- * Time Complexity: O(l), where l is the length of the input prefix.
320
- * Space Complexity: O(1) - Constant space.
321
- */
322
-
323
288
  /**
324
289
  * Time Complexity: O(l), where l is the length of the input prefix.
325
290
  * Space Complexity: O(1) - Constant space.
@@ -339,11 +304,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
339
304
  return !cur.isEnd;
340
305
  }
341
306
 
342
- /**
343
- * Time Complexity: O(l), where l is the length of the input prefix.
344
- * Space Complexity: O(1) - Constant space.
345
- */
346
-
347
307
  /**
348
308
  * Time Complexity: O(l), where l is the length of the input prefix.
349
309
  * Space Complexity: O(1) - Constant space.
@@ -363,11 +323,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
363
323
  return true;
364
324
  }
365
325
 
366
- /**
367
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
368
- * Space Complexity: O(l), where l is the length of the input prefix.
369
- */
370
-
371
326
  /**
372
327
  * Time Complexity: O(n), where n is the total number of nodes in the trie.
373
328
  * Space Complexity: O(l), where l is the length of the input prefix.
@@ -390,11 +345,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
390
345
  return commonPre === input;
391
346
  }
392
347
 
393
- /**
394
- * Time Complexity: O(n), where n is the total number of nodes in the trie.
395
- * Space Complexity: O(l), where l is the length of the longest common prefix.
396
- */
397
-
398
348
  /**
399
349
  * Time Complexity: O(n), where n is the total number of nodes in the trie.
400
350
  * Space Complexity: O(l), where l is the length of the longest common prefix.
@@ -414,11 +364,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
414
364
  return commonPre;
415
365
  }
416
366
 
417
- /**
418
- * Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
419
- * Space Complexity: O(w * l) - The space required for the output array.
420
- */
421
-
422
367
  /**
423
368
  * Time Complexity: O(w * l), where w is the number of words retrieved, and l is the average length of the words.
424
369
  * Space Complexity: O(w * l) - The space required for the output array.
@@ -468,11 +413,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
468
413
  return words;
469
414
  }
470
415
 
471
- /**
472
- * Time Complexity: O(n)
473
- * Space Complexity: O(n)
474
- */
475
-
476
416
  /**
477
417
  * Time Complexity: O(n)
478
418
  * Space Complexity: O(n)
@@ -485,11 +425,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
485
425
  return new Trie<R>(this, { caseSensitive: this.caseSensitive, toElementFn: this.toElementFn });
486
426
  }
487
427
 
488
- /**
489
- * Time Complexity: O(n)
490
- * Space Complexity: O(n)
491
- */
492
-
493
428
  /**
494
429
  * Time Complexity: O(n)
495
430
  * Space Complexity: O(n)
@@ -516,11 +451,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
516
451
  return results;
517
452
  }
518
453
 
519
- /**
520
- * Time Complexity: O(n)
521
- * Space Complexity: O(n)
522
- */
523
-
524
454
  /**
525
455
  * Time Complexity: O(n)
526
456
  * Space Complexity: O(n)
@@ -553,11 +483,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
553
483
  return newTrie;
554
484
  }
555
485
 
556
- /**
557
- * Time Complexity: O(n)
558
- * Space Complexity: O(n)
559
- */
560
-
561
486
  /**
562
487
  * Time Complexity: O(n)
563
488
  * Space Complexity: O(n)
@@ -578,11 +503,6 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
578
503
  yield* _dfs(this.root, '');
579
504
  }
580
505
 
581
- /**
582
- * Time Complexity: O(l), where l is the length of the input string.
583
- * Space Complexity: O(1) - Constant space.
584
- */
585
-
586
506
  /**
587
507
  * Time Complexity: O(l), where l is the length of the input string.
588
508
  * Space Complexity: O(1) - Constant space.
@@ -28,4 +28,11 @@ export type BTNPureKeyOrNodeOrEntry<K, V, NODE> = [K, OptValue<V>] | BTNPureKeyO
28
28
 
29
29
  export type BinaryTreeDeleteResult<NODE> = { deleted: OptBTNOrNull<NODE>; needBalanced: OptBTNOrNull<NODE> };
30
30
 
31
- export type BTNCallback<NODE, D = any> = (node: NODE) => D;
31
+ export type BTNCallback<NODE, D = any> = (node: NODE) => D;
32
+
33
+ export enum DFSOperation {
34
+ VISIT = 0,
35
+ PROCESS = 1,
36
+ }
37
+
38
+ export type DFSStackItem<NODE> = { opt: DFSOperation; node: OptBTNOrNull<NODE> }
@@ -1,21 +1,23 @@
1
- export type ToThunkFn = () => ReturnType<TrlFn>;
2
- export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
3
- export type TrlFn = (...args: any[]) => any;
1
+ export type ToThunkFn<R = any> = () => R;
2
+ export type Thunk<R = any> = ToThunkFn<R> & { __THUNK__?: symbol };
3
+ export type TrlFn<A extends any[] = any[], R = any> = (...args: A) => R;
4
4
  export type TrlAsyncFn = (...args: any[]) => any;
5
5
 
6
6
  export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
7
7
 
8
8
  export type Any = string | number | bigint | boolean | symbol | undefined | object;
9
9
 
10
- export type Comparable =
11
- | number
12
- | string
13
- | bigint
14
- | boolean
15
- | ({ [key in string]: any } & {
16
- valueOf(): Comparable;
17
- })
18
- | ({ [key in string]: any } & {
19
- toString(): Comparable;
20
- })
21
- | (() => Comparable);
10
+ export type ComparablePrimitive = number | bigint | string | boolean;
11
+
12
+ // TODO type safety looks not strict
13
+ export type ComparableObject = { [key in string]: any } & (
14
+ | {
15
+ valueOf: () => ComparablePrimitive | ComparableObject;
16
+ toString?: () => string;
17
+ }
18
+ | {
19
+ toString: () => string;
20
+ }
21
+ );
22
+
23
+ export type Comparable = ComparablePrimitive | Date | ComparableObject;
@@ -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: number, 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
@@ -5,8 +5,14 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparable, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
8
+ import type { Comparable, ComparablePrimitive, Thunk, ToThunkFn, TrlAsyncFn, TrlFn } from '../types';
9
9
 
10
+ /**
11
+ * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
12
+ * @returns A randomly generated UUID (Universally Unique Identifier) in the format
13
+ * 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' where each 'x' is replaced with a random hexadecimal
14
+ * character.
15
+ */
10
16
  export const uuidV4 = function () {
11
17
  return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
12
18
  const r = (Math.random() * 16) | 0,
@@ -15,6 +21,15 @@ export const uuidV4 = function () {
15
21
  });
16
22
  };
17
23
 
24
+ /**
25
+ * The `arrayRemove` function removes elements from an array based on a specified predicate function
26
+ * and returns the removed elements.
27
+ * @param {T[]} array - An array of elements that you want to filter based on the provided predicate
28
+ * function.
29
+ * @param predicate - The `predicate` parameter is a function that takes three arguments:
30
+ * @returns The `arrayRemove` function returns an array containing the elements that satisfy the given
31
+ * `predicate` function.
32
+ */
18
33
  export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
19
34
  let i = -1,
20
35
  len = array ? array.length : 0;
@@ -34,18 +49,45 @@ export const arrayRemove = function <T>(array: T[], predicate: (item: T, index:
34
49
 
35
50
  export const THUNK_SYMBOL = Symbol('thunk');
36
51
 
52
+ /**
53
+ * The function `isThunk` checks if a given value is a function with a specific symbol property.
54
+ * @param {any} fnOrValue - The `fnOrValue` parameter in the `isThunk` function can be either a
55
+ * function or a value that you want to check if it is a thunk. Thunks are functions that are wrapped
56
+ * around a value or computation for lazy evaluation. The function checks if the `fnOrValue` is
57
+ * @returns The function `isThunk` is checking if the input `fnOrValue` is a function and if it has a
58
+ * property `__THUNK__` equal to `THUNK_SYMBOL`. The return value will be `true` if both conditions are
59
+ * met, otherwise it will be `false`.
60
+ */
37
61
  export const isThunk = (fnOrValue: any) => {
38
62
  return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL;
39
63
  };
40
64
 
65
+ /**
66
+ * The `toThunk` function in TypeScript converts a function into a thunk by wrapping it in a closure.
67
+ * @param {ToThunkFn} fn - `fn` is a function that will be converted into a thunk.
68
+ * @returns A thunk function is being returned. Thunk functions are functions that delay the evaluation
69
+ * of an expression or operation until it is explicitly called or invoked. In this case, the `toThunk`
70
+ * function takes a function `fn` as an argument and returns a thunk function that, when called, will
71
+ * execute the `fn` function provided as an argument.
72
+ */
41
73
  export const toThunk = (fn: ToThunkFn): Thunk => {
42
74
  const thunk = () => fn();
43
75
  thunk.__THUNK__ = THUNK_SYMBOL;
44
76
  return thunk;
45
77
  };
46
78
 
79
+ /**
80
+ * The `trampoline` function in TypeScript enables tail call optimization by using thunks to avoid
81
+ * stack overflow.
82
+ * @param {TrlFn} fn - The `fn` parameter in the `trampoline` function is a function that takes any
83
+ * number of arguments and returns a value.
84
+ * @returns The `trampoline` function returns an object with two properties:
85
+ * 1. A function that executes the provided function `fn` and continues to execute any thunks returned
86
+ * by `fn` until a non-thunk value is returned.
87
+ * 2. A `cont` property that is a function which creates a thunk for the provided function `fn`.
88
+ */
47
89
  export const trampoline = (fn: TrlFn) => {
48
- const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args));
90
+ const cont = (...args: [...Parameters<TrlFn>]): ReturnType<TrlFn> => toThunk(() => fn(...args));
49
91
 
50
92
  return Object.assign(
51
93
  (...args: [...Parameters<TrlFn>]) => {
@@ -61,8 +103,20 @@ export const trampoline = (fn: TrlFn) => {
61
103
  );
62
104
  };
63
105
 
106
+ /**
107
+ * The `trampolineAsync` function in TypeScript allows for asynchronous trampolining of a given
108
+ * function.
109
+ * @param {TrlAsyncFn} fn - The `fn` parameter in the `trampolineAsync` function is expected to be a
110
+ * function that returns a Promise. This function will be called recursively until a non-thunk value is
111
+ * returned.
112
+ * @returns The `trampolineAsync` function returns an object with two properties:
113
+ * 1. An async function that executes the provided `TrlAsyncFn` function and continues to execute any
114
+ * thunks returned by the function until a non-thunk value is returned.
115
+ * 2. A `cont` property that is a function which wraps the provided `TrlAsyncFn` function in a thunk
116
+ * and returns it.
117
+ */
64
118
  export const trampolineAsync = (fn: TrlAsyncFn) => {
65
- const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args));
119
+ const cont = (...args: [...Parameters<TrlAsyncFn>]): ReturnType<TrlAsyncFn> => toThunk(() => fn(...args));
66
120
 
67
121
  return Object.assign(
68
122
  async (...args: [...Parameters<TrlAsyncFn>]) => {
@@ -78,6 +132,15 @@ export const trampolineAsync = (fn: TrlAsyncFn) => {
78
132
  );
79
133
  };
80
134
 
135
+ /**
136
+ * The function `getMSB` returns the most significant bit of a given number.
137
+ * @param {number} value - The `value` parameter is a number for which we want to find the position of
138
+ * the Most Significant Bit (MSB). The function `getMSB` takes this number as input and calculates the
139
+ * position of the MSB in its binary representation.
140
+ * @returns The function `getMSB` returns the most significant bit (MSB) of the input `value`. If the
141
+ * input value is less than or equal to 0, it returns 0. Otherwise, it calculates the position of the
142
+ * MSB using the `Math.clz32` function and bitwise left shifts 1 to that position.
143
+ */
81
144
  export const getMSB = (value: number): number => {
82
145
  if (value <= 0) {
83
146
  return 0;
@@ -85,42 +148,135 @@ export const getMSB = (value: number): number => {
85
148
  return 1 << (31 - Math.clz32(value));
86
149
  };
87
150
 
151
+ /**
152
+ * The `rangeCheck` function in TypeScript is used to validate if an index is within a specified range
153
+ * and throws a `RangeError` with a custom message if it is out of bounds.
154
+ * @param {number} index - The `index` parameter represents the value that you want to check if it
155
+ * falls within a specified range.
156
+ * @param {number} min - The `min` parameter represents the minimum value that the `index` should be
157
+ * compared against in the `rangeCheck` function.
158
+ * @param {number} max - The `max` parameter in the `rangeCheck` function represents the maximum value
159
+ * that the `index` parameter is allowed to have. If the `index` is greater than this `max` value, a
160
+ * `RangeError` will be thrown.
161
+ * @param [message=Index out of bounds.] - The `message` parameter is a string that represents the
162
+ * error message to be thrown if the index is out of bounds. By default, if no message is provided when
163
+ * calling the `rangeCheck` function, the message "Index out of bounds." will be used.
164
+ */
88
165
  export const rangeCheck = (index: number, min: number, max: number, message = 'Index out of bounds.'): void => {
89
166
  if (index < min || index > max) throw new RangeError(message);
90
167
  };
91
168
 
169
+ /**
170
+ * The function `throwRangeError` throws a RangeError with a custom message if called.
171
+ * @param [message=The value is off-limits.] - The `message` parameter is a string that represents the
172
+ * error message to be displayed when a `RangeError` is thrown. If no message is provided, the default
173
+ * message is 'The value is off-limits.'.
174
+ */
92
175
  export const throwRangeError = (message = 'The value is off-limits.'): void => {
93
176
  throw new RangeError(message);
94
177
  };
95
178
 
179
+ /**
180
+ * The function `isWeakKey` checks if the input is an object or a function in TypeScript.
181
+ * @param {unknown} input - The `input` parameter in the `isWeakKey` function is of type `unknown`,
182
+ * which means it can be any type. The function checks if the `input` is an object (excluding `null`)
183
+ * or a function, and returns a boolean indicating whether the `input` is a weak
184
+ * @returns The function `isWeakKey` returns a boolean value indicating whether the input is an object
185
+ * or a function.
186
+ */
96
187
  export const isWeakKey = (input: unknown): input is object => {
97
188
  const inputType = typeof input;
98
189
  return (inputType === 'object' && input !== null) || inputType === 'function';
99
190
  };
100
191
 
192
+ /**
193
+ * The function `calcMinUnitsRequired` calculates the minimum number of units required to accommodate a
194
+ * given total quantity based on a specified unit size.
195
+ * @param {number} totalQuantity - The `totalQuantity` parameter represents the total quantity of items
196
+ * that need to be processed or handled.
197
+ * @param {number} unitSize - The `unitSize` parameter represents the size of each unit or package. It
198
+ * is used in the `calcMinUnitsRequired` function to calculate the minimum number of units required to
199
+ * accommodate a total quantity of items.
200
+ */
101
201
  export const calcMinUnitsRequired = (totalQuantity: number, unitSize: number) =>
102
202
  Math.floor((totalQuantity + unitSize - 1) / unitSize);
103
203
 
204
+ /**
205
+ * The `roundFixed` function in TypeScript rounds a number to a specified number of decimal places.
206
+ * @param {number} num - The `num` parameter is a number that you want to round to a certain number of
207
+ * decimal places.
208
+ * @param {number} [digit=10] - The `digit` parameter in the `roundFixed` function specifies the number
209
+ * of decimal places to round the number to. By default, it is set to 10 if not provided explicitly.
210
+ * @returns The function `roundFixed` returns a number that is rounded to the specified number of
211
+ * decimal places (default is 10 decimal places).
212
+ */
104
213
  export const roundFixed = (num: number, digit: number = 10) => {
105
214
  const multiplier = Math.pow(10, digit);
106
215
  return Math.round(num * multiplier) / multiplier;
107
216
  };
108
217
 
109
- export function isComparable(key: any): key is Comparable {
110
- const keyType = typeof key;
111
- if (keyType === 'number') return !isNaN(key);
112
- if (keyType === 'string') return true;
113
- if (keyType === 'bigint') return true;
114
- if (keyType === 'boolean') return true;
115
- if (keyType === 'symbol') return false;
116
- if (keyType === 'undefined') return false;
117
- if (keyType === 'function') return isComparable(key());
118
- if (keyType === 'object') {
119
- if (key === null) return true;
120
- // if (typeof key.valueOf === 'function') return isComparable(key.valueOf()); // This will keep recursing because every object has a valueOf method.
121
- // if (typeof key.toString === 'function') return isComparable(key.toString()); // This will also keep recursing because every string type has a toString method.
122
- return false;
218
+ /**
219
+ * The function `isPrimitiveComparable` checks if a value is a primitive type that can be compared.
220
+ * @param {unknown} value - The `value` parameter in the `isPrimitiveComparable` function is of type
221
+ * `unknown`, which means it can be any type. The function checks if the `value` is a primitive type
222
+ * that can be compared, such as number, bigint, string, or boolean.
223
+ * @returns The function `isPrimitiveComparable` returns a boolean value indicating whether the input
224
+ * `value` is a primitive value that can be compared using standard comparison operators (<, >, <=,
225
+ * >=).
226
+ */
227
+ function isPrimitiveComparable(value: unknown): value is ComparablePrimitive {
228
+ const valueType = typeof value;
229
+ if (valueType === 'number') return !Number.isNaN(value);
230
+ return valueType === 'bigint' || valueType === 'string' || valueType === 'boolean';
231
+ }
232
+
233
+ /**
234
+ * The function `tryObjectToPrimitive` attempts to convert an object to a comparable primitive value by
235
+ * first checking the `valueOf` method and then the `toString` method.
236
+ * @param {object} obj - The `obj` parameter in the `tryObjectToPrimitive` function is an object that
237
+ * you want to convert to a primitive value. The function attempts to convert the object to a primitive
238
+ * value by first checking if the object has a `valueOf` method. If the `valueOf` method exists, it
239
+ * @returns The function `tryObjectToPrimitive` returns a value of type `ComparablePrimitive` if a
240
+ * primitive comparable value is found within the object, or a string value if the object has a custom
241
+ * `toString` method that does not return `'[object Object]'`. If neither condition is met, the
242
+ * function returns `null`.
243
+ */
244
+ function tryObjectToPrimitive(obj: object): ComparablePrimitive | null {
245
+ if (typeof obj.valueOf === 'function') {
246
+ const valueOfResult = obj.valueOf();
247
+ if (valueOfResult !== obj) {
248
+ if (isPrimitiveComparable(valueOfResult)) return valueOfResult;
249
+ if (typeof valueOfResult === 'object' && valueOfResult !== null) return tryObjectToPrimitive(valueOfResult);
250
+ }
251
+ }
252
+ if (typeof obj.toString === 'function') {
253
+ const stringResult = obj.toString();
254
+ if (stringResult !== '[object Object]') return stringResult;
123
255
  }
256
+ return null;
257
+ }
258
+
259
+ /**
260
+ * The function `isComparable` in TypeScript checks if a value is comparable, handling primitive values
261
+ * and objects with optional force comparison.
262
+ * @param {unknown} value - The `value` parameter in the `isComparable` function represents the value
263
+ * that you want to check if it is comparable. It can be of any type (`unknown`), and the function will
264
+ * determine if it is comparable based on certain conditions.
265
+ * @param [isForceObjectComparable=false] - The `isForceObjectComparable` parameter in the
266
+ * `isComparable` function is a boolean flag that determines whether to treat non-primitive values as
267
+ * comparable objects. When set to `true`, it forces the function to consider non-primitive values as
268
+ * comparable objects, regardless of their type.
269
+ * @returns The function `isComparable` returns a boolean value indicating whether the `value` is
270
+ * considered comparable or not.
271
+ */
272
+ export function isComparable(value: unknown, isForceObjectComparable = false): value is Comparable {
273
+ if (value === null || value === undefined) return false;
274
+ if (isPrimitiveComparable(value)) return true;
124
275
 
125
- return false;
276
+ if (typeof value !== 'object') return false;
277
+ if (value instanceof Date) return !Number.isNaN(value.getTime());
278
+ if (isForceObjectComparable) return true;
279
+ const comparableValue = tryObjectToPrimitive(value);
280
+ if (comparableValue === null || comparableValue === undefined) return false;
281
+ return isPrimitiveComparable(comparableValue);
126
282
  }