complete-common 1.0.0 → 1.1.0

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 (156) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +3 -3
  3. package/dist/constants.d.cts +7 -0
  4. package/dist/constants.d.mts +7 -0
  5. package/dist/constants.d.ts +7 -0
  6. package/dist/constants.d.ts.map +1 -0
  7. package/dist/functions/array.d.cts +83 -0
  8. package/dist/functions/array.d.mts +83 -0
  9. package/dist/functions/array.d.ts +83 -0
  10. package/dist/functions/array.d.ts.map +1 -0
  11. package/dist/functions/assert.d.cts +45 -0
  12. package/dist/functions/assert.d.mts +45 -0
  13. package/dist/functions/assert.d.ts +45 -0
  14. package/dist/functions/assert.d.ts.map +1 -0
  15. package/dist/functions/enums.d.cts +73 -0
  16. package/dist/functions/enums.d.mts +73 -0
  17. package/dist/functions/enums.d.ts +73 -0
  18. package/dist/functions/enums.d.ts.map +1 -0
  19. package/dist/functions/map.d.cts +24 -0
  20. package/dist/functions/map.d.mts +24 -0
  21. package/dist/functions/map.d.ts +24 -0
  22. package/dist/functions/map.d.ts.map +1 -0
  23. package/dist/functions/math.d.cts +13 -0
  24. package/dist/functions/math.d.mts +13 -0
  25. package/dist/functions/math.d.ts +13 -0
  26. package/dist/functions/math.d.ts.map +1 -0
  27. package/dist/functions/object.d.cts +60 -0
  28. package/dist/functions/object.d.mts +60 -0
  29. package/dist/functions/object.d.ts +60 -0
  30. package/dist/functions/object.d.ts.map +1 -0
  31. package/dist/functions/random.d.cts +23 -0
  32. package/dist/functions/random.d.mts +23 -0
  33. package/dist/functions/random.d.ts +23 -0
  34. package/dist/functions/random.d.ts.map +1 -0
  35. package/dist/functions/set.d.cts +61 -0
  36. package/dist/functions/set.d.mts +61 -0
  37. package/dist/functions/set.d.ts +61 -0
  38. package/dist/functions/set.d.ts.map +1 -0
  39. package/dist/functions/sort.d.cts +14 -0
  40. package/dist/functions/sort.d.mts +14 -0
  41. package/dist/functions/sort.d.ts +14 -0
  42. package/dist/functions/sort.d.ts.map +1 -0
  43. package/dist/functions/string.d.cts +122 -0
  44. package/dist/functions/string.d.mts +122 -0
  45. package/dist/functions/string.d.ts +122 -0
  46. package/dist/functions/string.d.ts.map +1 -0
  47. package/dist/functions/string.test.d.cts +2 -0
  48. package/dist/functions/string.test.d.mts +2 -0
  49. package/dist/functions/string.test.d.ts +2 -0
  50. package/dist/functions/string.test.d.ts.map +1 -0
  51. package/dist/functions/time.d.cts +20 -0
  52. package/dist/functions/time.d.mts +20 -0
  53. package/dist/functions/time.d.ts +20 -0
  54. package/dist/functions/time.d.ts.map +1 -0
  55. package/dist/functions/tuple.d.cts +27 -0
  56. package/dist/functions/tuple.d.mts +27 -0
  57. package/dist/functions/tuple.d.ts +27 -0
  58. package/dist/functions/tuple.d.ts.map +1 -0
  59. package/dist/functions/types.d.cts +13 -0
  60. package/dist/functions/types.d.mts +13 -0
  61. package/dist/functions/types.d.ts +13 -0
  62. package/dist/functions/types.d.ts.map +1 -0
  63. package/dist/functions/utils.d.cts +131 -0
  64. package/dist/functions/utils.d.mts +131 -0
  65. package/dist/functions/utils.d.ts +131 -0
  66. package/dist/functions/utils.d.ts.map +1 -0
  67. package/dist/functions/utils.test.d.cts +2 -0
  68. package/dist/functions/utils.test.d.mts +2 -0
  69. package/dist/functions/utils.test.d.ts +2 -0
  70. package/dist/functions/utils.test.d.ts.map +1 -0
  71. package/dist/index.cjs +122 -77
  72. package/dist/index.d.cts +30 -705
  73. package/dist/index.d.mts +30 -705
  74. package/dist/index.d.ts +30 -705
  75. package/dist/index.d.ts.map +1 -0
  76. package/dist/index.mjs +118 -78
  77. package/dist/types/AddSubtract.d.cts +23 -0
  78. package/dist/types/AddSubtract.d.mts +23 -0
  79. package/dist/types/AddSubtract.d.ts +23 -0
  80. package/dist/types/AddSubtract.d.ts.map +1 -0
  81. package/dist/types/CompositionTypeSatisfiesEnum.d.cts +35 -0
  82. package/dist/types/CompositionTypeSatisfiesEnum.d.mts +35 -0
  83. package/dist/types/CompositionTypeSatisfiesEnum.d.ts +35 -0
  84. package/dist/types/CompositionTypeSatisfiesEnum.d.ts.map +1 -0
  85. package/dist/types/ERange.d.cts +12 -0
  86. package/dist/types/ERange.d.mts +12 -0
  87. package/dist/types/ERange.d.ts +12 -0
  88. package/dist/types/ERange.d.ts.map +1 -0
  89. package/dist/types/IRange.d.cts +13 -0
  90. package/dist/types/IRange.d.mts +13 -0
  91. package/dist/types/IRange.d.ts +13 -0
  92. package/dist/types/IRange.d.ts.map +1 -0
  93. package/dist/types/Immutable.d.cts +17 -0
  94. package/dist/types/Immutable.d.mts +17 -0
  95. package/dist/types/Immutable.d.ts +17 -0
  96. package/dist/types/Immutable.d.ts.map +1 -0
  97. package/dist/types/NaturalNumbersLessThan.d.cts +8 -0
  98. package/dist/types/NaturalNumbersLessThan.d.mts +8 -0
  99. package/dist/types/NaturalNumbersLessThan.d.ts +8 -0
  100. package/dist/types/NaturalNumbersLessThan.d.ts.map +1 -0
  101. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.cts +8 -0
  102. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.mts +8 -0
  103. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts +8 -0
  104. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts.map +1 -0
  105. package/dist/types/ObjectValues.d.cts +3 -0
  106. package/dist/types/ObjectValues.d.mts +3 -0
  107. package/dist/types/ObjectValues.d.ts +3 -0
  108. package/dist/types/ObjectValues.d.ts.map +1 -0
  109. package/dist/types/ReadonlyMap.d.cts +9 -0
  110. package/dist/types/ReadonlyMap.d.mts +9 -0
  111. package/dist/types/ReadonlyMap.d.ts +9 -0
  112. package/dist/types/ReadonlyMap.d.ts.map +1 -0
  113. package/dist/types/ReadonlyRecord.d.cts +7 -0
  114. package/dist/types/ReadonlyRecord.d.mts +7 -0
  115. package/dist/types/ReadonlyRecord.d.ts +7 -0
  116. package/dist/types/ReadonlyRecord.d.ts.map +1 -0
  117. package/dist/types/ReadonlySet.d.cts +8 -0
  118. package/dist/types/ReadonlySet.d.mts +8 -0
  119. package/dist/types/ReadonlySet.d.ts +8 -0
  120. package/dist/types/ReadonlySet.d.ts.map +1 -0
  121. package/dist/types/Tuple.d.cts +10 -0
  122. package/dist/types/Tuple.d.mts +10 -0
  123. package/dist/types/Tuple.d.ts +10 -0
  124. package/dist/types/Tuple.d.ts.map +1 -0
  125. package/dist/types/WidenLiteral.d.cts +3 -0
  126. package/dist/types/WidenLiteral.d.mts +3 -0
  127. package/dist/types/WidenLiteral.d.ts +3 -0
  128. package/dist/types/WidenLiteral.d.ts.map +1 -0
  129. package/dist/types/Writeable.d.cts +9 -0
  130. package/dist/types/Writeable.d.mts +9 -0
  131. package/dist/types/Writeable.d.ts +9 -0
  132. package/dist/types/Writeable.d.ts.map +1 -0
  133. package/package.json +13 -2
  134. package/src/constants.ts +5 -0
  135. package/src/functions/array.ts +8 -2
  136. package/src/functions/assert.ts +86 -0
  137. package/src/functions/enums.ts +6 -0
  138. package/src/functions/map.ts +7 -45
  139. package/src/functions/math.ts +6 -0
  140. package/src/functions/object.ts +90 -3
  141. package/src/functions/random.ts +6 -0
  142. package/src/functions/set.ts +7 -0
  143. package/src/functions/sort.ts +6 -0
  144. package/src/functions/string.ts +25 -0
  145. package/src/functions/time.ts +26 -0
  146. package/src/functions/tuple.ts +6 -0
  147. package/src/functions/types.ts +6 -0
  148. package/src/functions/utils.test.ts +2 -8
  149. package/src/functions/utils.ts +7 -37
  150. package/src/index.ts +14 -12
  151. package/src/types/AddSubtract.ts +10 -2
  152. package/src/types/CompositionTypeSatisfiesEnum.ts +5 -3
  153. package/src/types/ObjectValues.ts +1 -0
  154. package/src/types/ReadonlyRecord.ts +5 -0
  155. package/src/types/Tuple.ts +1 -0
  156. package/src/types/WidenLiteral.ts +1 -0
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Helper functions that do not belong to any category in particular.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Helper function to get an iterator of integers with the specified range, inclusive on the lower
8
+ * end and exclusive on the high end. (The "e" in the function name stands for exclusive.) Thus,
9
+ * this function works in the same way as the built-in `range` function from Python.
10
+ *
11
+ * If the end is lower than the start, then an empty range will be returned.
12
+ *
13
+ * For example:
14
+ *
15
+ * - `eRange(2)` returns `[0, 1]`.
16
+ * - `eRange(3)` returns `[0, 1, 2]`.
17
+ * - `eRange(-3)` returns `[0, -1, -2]`.
18
+ * - `eRange(1, 3)` returns `[1, 2]`.
19
+ * - `eRange(2, 5)` returns `[2, 3, 4]`.
20
+ * - `eRange(5, 2)` returns `[]`.
21
+ * - `eRange(3, 3)` returns `[]`.
22
+ *
23
+ * If you want an array instead of an iterator, use the spread operator like this:
24
+ *
25
+ * ```ts
26
+ * const myArray = [...eRange(1, 3)];
27
+ * ```
28
+ *
29
+ * @param start The integer to start at.
30
+ * @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
31
+ * first argument will be the end.
32
+ * @param increment Optional. The increment to use. Default is 1.
33
+ */
34
+ export declare function eRange(start: number, end?: number, increment?: number): Generator<number>;
35
+ /**
36
+ * Helper function to get an array of integers with the specified range, inclusive on both ends.
37
+ * (The "i" in the function name stands for inclusive.)
38
+ *
39
+ * If the end is lower than the start, then an empty range will be returned.
40
+ *
41
+ * For example:
42
+ *
43
+ * - `iRange(2)` returns `[0, 1, 2]`.
44
+ * - `iRange(3)` returns `[0, 1, 2, 3]`.
45
+ * - `iRange(-3)` returns `[0, -1, -2, -3]`.
46
+ * - `iRange(1, 3)` returns `[1, 2, 3]`.
47
+ * - `iRange(2, 5)` returns `[2, 3, 4, 5]`.
48
+ * - `iRange(5, 2)` returns `[]`.
49
+ * - `iRange(3, 3)` returns `[3]`.
50
+ *
51
+ * If you want an array instead of an iterator, use the spread operator like this:
52
+ *
53
+ * ```ts
54
+ * const myArray = [...iRange(1, 3)];
55
+ * ```
56
+ *
57
+ * @param start The integer to start at.
58
+ * @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
59
+ * first argument will be the end.
60
+ * @param increment Optional. The increment to use. Default is 1.
61
+ */
62
+ export declare function iRange(start: number, end?: number, increment?: number): Generator<number>;
63
+ /** From: https://stackoverflow.com/questions/61526746 */
64
+ export declare function isKeyOf<T extends object>(key: PropertyKey, target: T): key is keyof T;
65
+ /**
66
+ * Helper function to perform a no-op. This can be useful in order to make a trailing return valid
67
+ * in functions that use the early return pattern.
68
+ */
69
+ export declare function noop(): void;
70
+ /**
71
+ * This is a more reliable version of `Number.parseFloat`:
72
+ *
73
+ * - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
74
+ * TypeScript type narrowing patterns.
75
+ * - Strings that are a mixture of numbers and letters will result in undefined instead of the part
76
+ * of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
77
+ * - Non-strings will result in undefined instead of being coerced to a number.
78
+ *
79
+ * @param string A string to convert to an integer.
80
+ */
81
+ export declare function parseFloatSafe(string: string): number | undefined;
82
+ /**
83
+ * This is a more reliable version of `Number.parseInt`:
84
+ *
85
+ * - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
86
+ * TypeScript type narrowing patterns.
87
+ * - Strings that are a mixture of numbers and letters will result in undefined instead of the part
88
+ * of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
89
+ * - Non-strings will result in undefined instead of being coerced to a number.
90
+ *
91
+ * If you have to use a radix other than 10, use the vanilla `Number.parseInt` function instead,
92
+ * because this function ensures that the string contains no letters.
93
+ */
94
+ export declare function parseIntSafe(string: string): number | undefined;
95
+ /**
96
+ * Helper function to repeat code N times. This is faster to type and cleaner than using a for loop.
97
+ *
98
+ * For example:
99
+ *
100
+ * ```ts
101
+ * repeat(10, () => {
102
+ * foo();
103
+ * });
104
+ * ```
105
+ *
106
+ * The repeated function is passed the index of the iteration, if needed:
107
+ *
108
+ * ```ts
109
+ * repeat(3, (i) => {
110
+ * console.log(i); // Prints "0", "1", "2"
111
+ * });
112
+ * ```
113
+ */
114
+ export declare function repeat(num: number, func: (i: number) => void): void;
115
+ /**
116
+ * Helper function to signify that the enclosing code block is not yet complete. Using this function
117
+ * is similar to writing a "TODO" comment, but it has the benefit of preventing ESLint errors due to
118
+ * unused variables or early returns.
119
+ *
120
+ * When you see this function, it simply means that the programmer intends to add in more code to
121
+ * this spot later.
122
+ *
123
+ * This function is variadic, meaning that you can pass as many arguments as you want. (This is
124
+ * useful as a means to prevent unused variables.)
125
+ *
126
+ * This function does not actually do anything. (It is an "empty" function.)
127
+ *
128
+ * @allowEmptyVariadic
129
+ */
130
+ export declare function todo(...args: readonly unknown[]): void;
131
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Helper functions that do not belong to any category in particular.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Helper function to get an iterator of integers with the specified range, inclusive on the lower
8
+ * end and exclusive on the high end. (The "e" in the function name stands for exclusive.) Thus,
9
+ * this function works in the same way as the built-in `range` function from Python.
10
+ *
11
+ * If the end is lower than the start, then an empty range will be returned.
12
+ *
13
+ * For example:
14
+ *
15
+ * - `eRange(2)` returns `[0, 1]`.
16
+ * - `eRange(3)` returns `[0, 1, 2]`.
17
+ * - `eRange(-3)` returns `[0, -1, -2]`.
18
+ * - `eRange(1, 3)` returns `[1, 2]`.
19
+ * - `eRange(2, 5)` returns `[2, 3, 4]`.
20
+ * - `eRange(5, 2)` returns `[]`.
21
+ * - `eRange(3, 3)` returns `[]`.
22
+ *
23
+ * If you want an array instead of an iterator, use the spread operator like this:
24
+ *
25
+ * ```ts
26
+ * const myArray = [...eRange(1, 3)];
27
+ * ```
28
+ *
29
+ * @param start The integer to start at.
30
+ * @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
31
+ * first argument will be the end.
32
+ * @param increment Optional. The increment to use. Default is 1.
33
+ */
34
+ export declare function eRange(start: number, end?: number, increment?: number): Generator<number>;
35
+ /**
36
+ * Helper function to get an array of integers with the specified range, inclusive on both ends.
37
+ * (The "i" in the function name stands for inclusive.)
38
+ *
39
+ * If the end is lower than the start, then an empty range will be returned.
40
+ *
41
+ * For example:
42
+ *
43
+ * - `iRange(2)` returns `[0, 1, 2]`.
44
+ * - `iRange(3)` returns `[0, 1, 2, 3]`.
45
+ * - `iRange(-3)` returns `[0, -1, -2, -3]`.
46
+ * - `iRange(1, 3)` returns `[1, 2, 3]`.
47
+ * - `iRange(2, 5)` returns `[2, 3, 4, 5]`.
48
+ * - `iRange(5, 2)` returns `[]`.
49
+ * - `iRange(3, 3)` returns `[3]`.
50
+ *
51
+ * If you want an array instead of an iterator, use the spread operator like this:
52
+ *
53
+ * ```ts
54
+ * const myArray = [...iRange(1, 3)];
55
+ * ```
56
+ *
57
+ * @param start The integer to start at.
58
+ * @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
59
+ * first argument will be the end.
60
+ * @param increment Optional. The increment to use. Default is 1.
61
+ */
62
+ export declare function iRange(start: number, end?: number, increment?: number): Generator<number>;
63
+ /** From: https://stackoverflow.com/questions/61526746 */
64
+ export declare function isKeyOf<T extends object>(key: PropertyKey, target: T): key is keyof T;
65
+ /**
66
+ * Helper function to perform a no-op. This can be useful in order to make a trailing return valid
67
+ * in functions that use the early return pattern.
68
+ */
69
+ export declare function noop(): void;
70
+ /**
71
+ * This is a more reliable version of `Number.parseFloat`:
72
+ *
73
+ * - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
74
+ * TypeScript type narrowing patterns.
75
+ * - Strings that are a mixture of numbers and letters will result in undefined instead of the part
76
+ * of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
77
+ * - Non-strings will result in undefined instead of being coerced to a number.
78
+ *
79
+ * @param string A string to convert to an integer.
80
+ */
81
+ export declare function parseFloatSafe(string: string): number | undefined;
82
+ /**
83
+ * This is a more reliable version of `Number.parseInt`:
84
+ *
85
+ * - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
86
+ * TypeScript type narrowing patterns.
87
+ * - Strings that are a mixture of numbers and letters will result in undefined instead of the part
88
+ * of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
89
+ * - Non-strings will result in undefined instead of being coerced to a number.
90
+ *
91
+ * If you have to use a radix other than 10, use the vanilla `Number.parseInt` function instead,
92
+ * because this function ensures that the string contains no letters.
93
+ */
94
+ export declare function parseIntSafe(string: string): number | undefined;
95
+ /**
96
+ * Helper function to repeat code N times. This is faster to type and cleaner than using a for loop.
97
+ *
98
+ * For example:
99
+ *
100
+ * ```ts
101
+ * repeat(10, () => {
102
+ * foo();
103
+ * });
104
+ * ```
105
+ *
106
+ * The repeated function is passed the index of the iteration, if needed:
107
+ *
108
+ * ```ts
109
+ * repeat(3, (i) => {
110
+ * console.log(i); // Prints "0", "1", "2"
111
+ * });
112
+ * ```
113
+ */
114
+ export declare function repeat(num: number, func: (i: number) => void): void;
115
+ /**
116
+ * Helper function to signify that the enclosing code block is not yet complete. Using this function
117
+ * is similar to writing a "TODO" comment, but it has the benefit of preventing ESLint errors due to
118
+ * unused variables or early returns.
119
+ *
120
+ * When you see this function, it simply means that the programmer intends to add in more code to
121
+ * this spot later.
122
+ *
123
+ * This function is variadic, meaning that you can pass as many arguments as you want. (This is
124
+ * useful as a means to prevent unused variables.)
125
+ *
126
+ * This function does not actually do anything. (It is an "empty" function.)
127
+ *
128
+ * @allowEmptyVariadic
129
+ */
130
+ export declare function todo(...args: readonly unknown[]): void;
131
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Helper functions that do not belong to any category in particular.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Helper function to get an iterator of integers with the specified range, inclusive on the lower
8
+ * end and exclusive on the high end. (The "e" in the function name stands for exclusive.) Thus,
9
+ * this function works in the same way as the built-in `range` function from Python.
10
+ *
11
+ * If the end is lower than the start, then an empty range will be returned.
12
+ *
13
+ * For example:
14
+ *
15
+ * - `eRange(2)` returns `[0, 1]`.
16
+ * - `eRange(3)` returns `[0, 1, 2]`.
17
+ * - `eRange(-3)` returns `[0, -1, -2]`.
18
+ * - `eRange(1, 3)` returns `[1, 2]`.
19
+ * - `eRange(2, 5)` returns `[2, 3, 4]`.
20
+ * - `eRange(5, 2)` returns `[]`.
21
+ * - `eRange(3, 3)` returns `[]`.
22
+ *
23
+ * If you want an array instead of an iterator, use the spread operator like this:
24
+ *
25
+ * ```ts
26
+ * const myArray = [...eRange(1, 3)];
27
+ * ```
28
+ *
29
+ * @param start The integer to start at.
30
+ * @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
31
+ * first argument will be the end.
32
+ * @param increment Optional. The increment to use. Default is 1.
33
+ */
34
+ export declare function eRange(start: number, end?: number, increment?: number): Generator<number>;
35
+ /**
36
+ * Helper function to get an array of integers with the specified range, inclusive on both ends.
37
+ * (The "i" in the function name stands for inclusive.)
38
+ *
39
+ * If the end is lower than the start, then an empty range will be returned.
40
+ *
41
+ * For example:
42
+ *
43
+ * - `iRange(2)` returns `[0, 1, 2]`.
44
+ * - `iRange(3)` returns `[0, 1, 2, 3]`.
45
+ * - `iRange(-3)` returns `[0, -1, -2, -3]`.
46
+ * - `iRange(1, 3)` returns `[1, 2, 3]`.
47
+ * - `iRange(2, 5)` returns `[2, 3, 4, 5]`.
48
+ * - `iRange(5, 2)` returns `[]`.
49
+ * - `iRange(3, 3)` returns `[3]`.
50
+ *
51
+ * If you want an array instead of an iterator, use the spread operator like this:
52
+ *
53
+ * ```ts
54
+ * const myArray = [...iRange(1, 3)];
55
+ * ```
56
+ *
57
+ * @param start The integer to start at.
58
+ * @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
59
+ * first argument will be the end.
60
+ * @param increment Optional. The increment to use. Default is 1.
61
+ */
62
+ export declare function iRange(start: number, end?: number, increment?: number): Generator<number>;
63
+ /** From: https://stackoverflow.com/questions/61526746 */
64
+ export declare function isKeyOf<T extends object>(key: PropertyKey, target: T): key is keyof T;
65
+ /**
66
+ * Helper function to perform a no-op. This can be useful in order to make a trailing return valid
67
+ * in functions that use the early return pattern.
68
+ */
69
+ export declare function noop(): void;
70
+ /**
71
+ * This is a more reliable version of `Number.parseFloat`:
72
+ *
73
+ * - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
74
+ * TypeScript type narrowing patterns.
75
+ * - Strings that are a mixture of numbers and letters will result in undefined instead of the part
76
+ * of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
77
+ * - Non-strings will result in undefined instead of being coerced to a number.
78
+ *
79
+ * @param string A string to convert to an integer.
80
+ */
81
+ export declare function parseFloatSafe(string: string): number | undefined;
82
+ /**
83
+ * This is a more reliable version of `Number.parseInt`:
84
+ *
85
+ * - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
86
+ * TypeScript type narrowing patterns.
87
+ * - Strings that are a mixture of numbers and letters will result in undefined instead of the part
88
+ * of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
89
+ * - Non-strings will result in undefined instead of being coerced to a number.
90
+ *
91
+ * If you have to use a radix other than 10, use the vanilla `Number.parseInt` function instead,
92
+ * because this function ensures that the string contains no letters.
93
+ */
94
+ export declare function parseIntSafe(string: string): number | undefined;
95
+ /**
96
+ * Helper function to repeat code N times. This is faster to type and cleaner than using a for loop.
97
+ *
98
+ * For example:
99
+ *
100
+ * ```ts
101
+ * repeat(10, () => {
102
+ * foo();
103
+ * });
104
+ * ```
105
+ *
106
+ * The repeated function is passed the index of the iteration, if needed:
107
+ *
108
+ * ```ts
109
+ * repeat(3, (i) => {
110
+ * console.log(i); // Prints "0", "1", "2"
111
+ * });
112
+ * ```
113
+ */
114
+ export declare function repeat(num: number, func: (i: number) => void): void;
115
+ /**
116
+ * Helper function to signify that the enclosing code block is not yet complete. Using this function
117
+ * is similar to writing a "TODO" comment, but it has the benefit of preventing ESLint errors due to
118
+ * unused variables or early returns.
119
+ *
120
+ * When you see this function, it simply means that the programmer intends to add in more code to
121
+ * this spot later.
122
+ *
123
+ * This function is variadic, meaning that you can pass as many arguments as you want. (This is
124
+ * useful as a means to prevent unused variables.)
125
+ *
126
+ * This function does not actually do anything. (It is an "empty" function.)
127
+ *
128
+ * @allowEmptyVariadic
129
+ */
130
+ export declare function todo(...args: readonly unknown[]): void;
131
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/functions/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAiB,MAAM,CACrB,KAAK,EAAE,MAAM,EACb,GAAG,CAAC,EAAE,MAAM,EACZ,SAAS,SAAI,GACZ,SAAS,CAAC,MAAM,CAAC,CASnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAiB,MAAM,CACrB,KAAK,EAAE,MAAM,EACb,GAAG,CAAC,EAAE,MAAM,EACZ,SAAS,SAAI,GACZ,SAAS,CAAC,MAAM,CAAC,CAQnB;AAED,yDAAyD;AACzD,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,EACtC,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,CAAC,GACR,GAAG,IAAI,MAAM,CAAC,CAEhB;AAED;;;GAGG;AAEH,wBAAgB,IAAI,IAAI,IAAI,CAAG;AAE/B;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAcjE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAc/D;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAInE;AAED;;;;;;;;;;;;;;GAcG;AAEH,wBAAgB,IAAI,CAAC,GAAG,IAAI,EAAE,SAAS,OAAO,EAAE,GAAG,IAAI,CAAG"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=utils.test.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=utils.test.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.test.d.ts","sourceRoot":"","sources":["../../src/functions/utils.test.ts"],"names":[],"mappings":""}
package/dist/index.cjs CHANGED
@@ -6,25 +6,21 @@ const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
6
6
 
7
7
  const ReadonlySet = Set;
8
8
 
9
- function getRandomInt(min, max, exceptions = []) {
10
- min = Math.ceil(min);
11
- max = Math.floor(max);
12
- if (min > max) {
13
- const oldMin = min;
14
- const oldMax = max;
15
- min = oldMax;
16
- max = oldMin;
17
- }
18
- const exceptionsSet = new ReadonlySet(exceptions);
19
- let randomInt;
20
- do {
21
- randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
22
- } while (exceptionsSet.has(randomInt));
23
- return randomInt;
9
+ function isObject(variable) {
10
+ return typeof variable === "object" && variable !== null && !Array.isArray(variable);
24
11
  }
25
12
 
26
- const FLOAT_REGEX = /^-?\d*\.?\d+$/;
27
- const INTEGER_REGEX = /^-?\d+$/;
13
+ const TYPES_TO_VALIDATION_FUNCTIONS = {
14
+ string: (val) => typeof val === "string",
15
+ number: (val) => typeof val === "number",
16
+ boolean: (val) => typeof val === "boolean",
17
+ object: (val) => val !== null && typeof val === "object",
18
+ array: (val) => Array.isArray(val),
19
+ null: (val) => val === null,
20
+ undefined: (val) => val === void 0,
21
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
22
+ function: (val) => typeof val === "function"
23
+ };
28
24
  function assertDefined(value, ...[msg]) {
29
25
  if (value === void 0) {
30
26
  throw new TypeError(msg);
@@ -35,56 +31,33 @@ function assertNotNull(value, ...[msg]) {
35
31
  throw new TypeError(msg);
36
32
  }
37
33
  }
38
- function* eRange(start, end, increment = 1) {
39
- if (end === void 0) {
40
- yield* eRange(0, start, increment);
41
- return;
42
- }
43
- for (let i = start; i < end; i += increment) {
44
- yield i;
45
- }
46
- }
47
- function* iRange(start, end, increment = 1) {
48
- if (end === void 0) {
49
- yield* iRange(0, start, increment);
50
- return;
51
- }
52
- const exclusiveEnd = end + 1;
53
- yield* eRange(start, exclusiveEnd, increment);
54
- }
55
- function isKeyOf(key, target) {
56
- return key in target;
57
- }
58
- function noop() {
59
- }
60
- function parseFloatSafe(string) {
61
- if (typeof string !== "string") {
62
- return void 0;
63
- }
64
- const trimmedString = string.trim();
65
- if (FLOAT_REGEX.exec(trimmedString) === null) {
66
- return void 0;
34
+ function assertObject(value, msg) {
35
+ if (!isObject(value)) {
36
+ throw new TypeError(msg);
67
37
  }
68
- const number = Number.parseFloat(trimmedString);
69
- return Number.isNaN(number) ? void 0 : number;
70
38
  }
71
- function parseIntSafe(string) {
72
- if (typeof string !== "string") {
73
- return void 0;
74
- }
75
- const trimmedString = string.trim();
76
- if (INTEGER_REGEX.exec(trimmedString) === null) {
77
- return void 0;
39
+ function assertType(value, type, msg) {
40
+ const validationFunction = TYPES_TO_VALIDATION_FUNCTIONS[type];
41
+ if (!validationFunction(value)) {
42
+ throw new TypeError(msg);
78
43
  }
79
- const number = Number.parseInt(trimmedString, 10);
80
- return Number.isNaN(number) ? void 0 : number;
81
44
  }
82
- function repeat(num, func) {
83
- for (let i = 0; i < num; i++) {
84
- func(i);
45
+
46
+ function getRandomInt(min, max, exceptions = []) {
47
+ min = Math.ceil(min);
48
+ max = Math.floor(max);
49
+ if (min > max) {
50
+ const oldMin = min;
51
+ const oldMax = max;
52
+ min = oldMax;
53
+ max = oldMin;
85
54
  }
86
- }
87
- function todo(...args) {
55
+ const exceptionsSet = new ReadonlySet(exceptions);
56
+ let randomInt;
57
+ do {
58
+ randomInt = Math.floor(Math.random() * (max - min + 1)) + min;
59
+ } while (exceptionsSet.has(randomInt));
60
+ return randomInt;
88
61
  }
89
62
 
90
63
  function arrayCopyTwoDimensional(array) {
@@ -117,7 +90,7 @@ function arrayRemoveInPlace(array, ...elementsToRemove) {
117
90
  const removedElements = [];
118
91
  for (const element of elementsToRemove) {
119
92
  const index = array.indexOf(element);
120
- if (index > -1) {
93
+ if (index !== -1) {
121
94
  const removedElement = array.splice(index, 1);
122
95
  removedElements.push(...removedElement);
123
96
  }
@@ -219,16 +192,6 @@ function mapFind(map, predicate) {
219
192
  }
220
193
  return void 0;
221
194
  }
222
- function objectToMap(object) {
223
- const map = /* @__PURE__ */ new Map();
224
- for (const [key, value] of Object.entries(object)) {
225
- map.set(key, value);
226
- }
227
- return map;
228
- }
229
- function objectToReadonlyMap(object) {
230
- return objectToMap(object);
231
- }
232
195
 
233
196
  function clamp(num, min, max) {
234
197
  return Math.max(min, Math.min(num, max));
@@ -245,6 +208,26 @@ function objectFilter(object, predicate) {
245
208
  }
246
209
  return array;
247
210
  }
211
+ function objectToMap(object) {
212
+ const map = /* @__PURE__ */ new Map();
213
+ for (const [key, value] of Object.entries(object)) {
214
+ map.set(key, value);
215
+ }
216
+ return map;
217
+ }
218
+ function objectToReadonlyMap(object) {
219
+ return objectToMap(object);
220
+ }
221
+ function objectToReverseMap(object) {
222
+ const map = /* @__PURE__ */ new Map();
223
+ for (const [key, value] of Object.entries(object)) {
224
+ map.set(value, key);
225
+ }
226
+ return map;
227
+ }
228
+ function objectToReverseReadonlyMap(object) {
229
+ return objectToReverseMap(object);
230
+ }
248
231
 
249
232
  function addSetsToSet(mainSet, ...setsToAdd) {
250
233
  for (const set of setsToAdd) {
@@ -308,6 +291,60 @@ function sortCaseInsensitive(array) {
308
291
  return newArray;
309
292
  }
310
293
 
294
+ const FLOAT_REGEX = /^-?\d*\.?\d+$/;
295
+ const INTEGER_REGEX = /^-?\d+$/;
296
+ function* eRange(start, end, increment = 1) {
297
+ if (end === void 0) {
298
+ yield* eRange(0, start, increment);
299
+ return;
300
+ }
301
+ for (let i = start; i < end; i += increment) {
302
+ yield i;
303
+ }
304
+ }
305
+ function* iRange(start, end, increment = 1) {
306
+ if (end === void 0) {
307
+ yield* iRange(0, start, increment);
308
+ return;
309
+ }
310
+ const exclusiveEnd = end + 1;
311
+ yield* eRange(start, exclusiveEnd, increment);
312
+ }
313
+ function isKeyOf(key, target) {
314
+ return key in target;
315
+ }
316
+ function noop() {
317
+ }
318
+ function parseFloatSafe(string) {
319
+ if (typeof string !== "string") {
320
+ return void 0;
321
+ }
322
+ const trimmedString = string.trim();
323
+ if (FLOAT_REGEX.exec(trimmedString) === null) {
324
+ return void 0;
325
+ }
326
+ const number = Number.parseFloat(trimmedString);
327
+ return Number.isNaN(number) ? void 0 : number;
328
+ }
329
+ function parseIntSafe(string) {
330
+ if (typeof string !== "string") {
331
+ return void 0;
332
+ }
333
+ const trimmedString = string.trim();
334
+ if (INTEGER_REGEX.exec(trimmedString) === null) {
335
+ return void 0;
336
+ }
337
+ const number = Number.parseInt(trimmedString, 10);
338
+ return Number.isNaN(number) ? void 0 : number;
339
+ }
340
+ function repeat(num, func) {
341
+ for (let i = 0; i < num; i++) {
342
+ func(i);
343
+ }
344
+ }
345
+ function todo(...args) {
346
+ }
347
+
311
348
  const DIACRITIC_REGEX = /\p{Diacritic}/u;
312
349
  const EMOJI_REGEX = /(\p{Extended_Pictographic}|\p{Emoji_Component})/u;
313
350
  const FIRST_LETTER_CAPITALIZED_REGEX = /^\p{Lu}/u;
@@ -449,6 +486,13 @@ function truncateString(string, maxLength) {
449
486
  return string.slice(0, maxLength);
450
487
  }
451
488
 
489
+ function getElapsedSeconds(startTime) {
490
+ const endTime = Date.now();
491
+ const elapsedMilliseconds = endTime - startTime;
492
+ const elapsedSeconds = elapsedMilliseconds / 1e3;
493
+ return Math.floor(elapsedSeconds);
494
+ }
495
+
452
496
  function* tupleEntries(tuple) {
453
497
  yield* tuple.entries();
454
498
  }
@@ -456,10 +500,6 @@ function* tupleKeys(tuple) {
456
500
  yield* tuple.keys();
457
501
  }
458
502
 
459
- function isObject(variable) {
460
- return typeof variable === "object" && variable !== null && !Array.isArray(variable);
461
- }
462
-
463
503
  const ReadonlyMap = Map;
464
504
 
465
505
  exports.HOUR_IN_MILLISECONDS = HOUR_IN_MILLISECONDS;
@@ -474,6 +514,8 @@ exports.arrayRemove = arrayRemove;
474
514
  exports.arrayRemoveInPlace = arrayRemoveInPlace;
475
515
  exports.assertDefined = assertDefined;
476
516
  exports.assertNotNull = assertNotNull;
517
+ exports.assertObject = assertObject;
518
+ exports.assertType = assertType;
477
519
  exports.capitalizeFirstLetter = capitalizeFirstLetter;
478
520
  exports.clamp = clamp;
479
521
  exports.combineSets = combineSets;
@@ -482,6 +524,7 @@ exports.eRange = eRange;
482
524
  exports.emptyArray = emptyArray;
483
525
  exports.escapeHTMLCharacters = escapeHTMLCharacters;
484
526
  exports.filterMap = filterMap;
527
+ exports.getElapsedSeconds = getElapsedSeconds;
485
528
  exports.getEnumEntries = getEnumEntries;
486
529
  exports.getEnumKeys = getEnumKeys;
487
530
  exports.getEnumValues = getEnumValues;
@@ -513,6 +556,8 @@ exports.objectKeysToReadonlySet = objectKeysToReadonlySet;
513
556
  exports.objectKeysToSet = objectKeysToSet;
514
557
  exports.objectToMap = objectToMap;
515
558
  exports.objectToReadonlyMap = objectToReadonlyMap;
559
+ exports.objectToReverseMap = objectToReverseMap;
560
+ exports.objectToReverseReadonlyMap = objectToReverseReadonlyMap;
516
561
  exports.objectValuesToReadonlySet = objectValuesToReadonlySet;
517
562
  exports.objectValuesToSet = objectValuesToSet;
518
563
  exports.parseFloatSafe = parseFloatSafe;