complete-common 1.0.0 → 1.0.1

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 (151) 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 +22 -0
  12. package/dist/functions/assert.d.mts +22 -0
  13. package/dist/functions/assert.d.ts +22 -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 +52 -0
  20. package/dist/functions/map.d.mts +52 -0
  21. package/dist/functions/map.d.ts +52 -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 +14 -0
  28. package/dist/functions/object.d.mts +14 -0
  29. package/dist/functions/object.d.ts +14 -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/tuple.d.cts +27 -0
  52. package/dist/functions/tuple.d.mts +27 -0
  53. package/dist/functions/tuple.d.ts +27 -0
  54. package/dist/functions/tuple.d.ts.map +1 -0
  55. package/dist/functions/types.d.cts +13 -0
  56. package/dist/functions/types.d.mts +13 -0
  57. package/dist/functions/types.d.ts +13 -0
  58. package/dist/functions/types.d.ts.map +1 -0
  59. package/dist/functions/utils.d.cts +131 -0
  60. package/dist/functions/utils.d.mts +131 -0
  61. package/dist/functions/utils.d.ts +131 -0
  62. package/dist/functions/utils.d.ts.map +1 -0
  63. package/dist/functions/utils.test.d.cts +2 -0
  64. package/dist/functions/utils.test.d.mts +2 -0
  65. package/dist/functions/utils.test.d.ts +2 -0
  66. package/dist/functions/utils.test.d.ts.map +1 -0
  67. package/dist/index.cjs +66 -65
  68. package/dist/index.d.cts +29 -705
  69. package/dist/index.d.mts +29 -705
  70. package/dist/index.d.ts +29 -705
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.mjs +66 -65
  73. package/dist/types/AddSubtract.d.cts +23 -0
  74. package/dist/types/AddSubtract.d.mts +23 -0
  75. package/dist/types/AddSubtract.d.ts +23 -0
  76. package/dist/types/AddSubtract.d.ts.map +1 -0
  77. package/dist/types/CompositionTypeSatisfiesEnum.d.cts +35 -0
  78. package/dist/types/CompositionTypeSatisfiesEnum.d.mts +35 -0
  79. package/dist/types/CompositionTypeSatisfiesEnum.d.ts +35 -0
  80. package/dist/types/CompositionTypeSatisfiesEnum.d.ts.map +1 -0
  81. package/dist/types/ERange.d.cts +12 -0
  82. package/dist/types/ERange.d.mts +12 -0
  83. package/dist/types/ERange.d.ts +12 -0
  84. package/dist/types/ERange.d.ts.map +1 -0
  85. package/dist/types/IRange.d.cts +13 -0
  86. package/dist/types/IRange.d.mts +13 -0
  87. package/dist/types/IRange.d.ts +13 -0
  88. package/dist/types/IRange.d.ts.map +1 -0
  89. package/dist/types/Immutable.d.cts +17 -0
  90. package/dist/types/Immutable.d.mts +17 -0
  91. package/dist/types/Immutable.d.ts +17 -0
  92. package/dist/types/Immutable.d.ts.map +1 -0
  93. package/dist/types/NaturalNumbersLessThan.d.cts +8 -0
  94. package/dist/types/NaturalNumbersLessThan.d.mts +8 -0
  95. package/dist/types/NaturalNumbersLessThan.d.ts +8 -0
  96. package/dist/types/NaturalNumbersLessThan.d.ts.map +1 -0
  97. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.cts +8 -0
  98. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.mts +8 -0
  99. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts +8 -0
  100. package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts.map +1 -0
  101. package/dist/types/ObjectValues.d.cts +3 -0
  102. package/dist/types/ObjectValues.d.mts +3 -0
  103. package/dist/types/ObjectValues.d.ts +3 -0
  104. package/dist/types/ObjectValues.d.ts.map +1 -0
  105. package/dist/types/ReadonlyMap.d.cts +9 -0
  106. package/dist/types/ReadonlyMap.d.mts +9 -0
  107. package/dist/types/ReadonlyMap.d.ts +9 -0
  108. package/dist/types/ReadonlyMap.d.ts.map +1 -0
  109. package/dist/types/ReadonlyRecord.d.cts +7 -0
  110. package/dist/types/ReadonlyRecord.d.mts +7 -0
  111. package/dist/types/ReadonlyRecord.d.ts +7 -0
  112. package/dist/types/ReadonlyRecord.d.ts.map +1 -0
  113. package/dist/types/ReadonlySet.d.cts +8 -0
  114. package/dist/types/ReadonlySet.d.mts +8 -0
  115. package/dist/types/ReadonlySet.d.ts +8 -0
  116. package/dist/types/ReadonlySet.d.ts.map +1 -0
  117. package/dist/types/Tuple.d.cts +10 -0
  118. package/dist/types/Tuple.d.mts +10 -0
  119. package/dist/types/Tuple.d.ts +10 -0
  120. package/dist/types/Tuple.d.ts.map +1 -0
  121. package/dist/types/WidenLiteral.d.cts +3 -0
  122. package/dist/types/WidenLiteral.d.mts +3 -0
  123. package/dist/types/WidenLiteral.d.ts +3 -0
  124. package/dist/types/WidenLiteral.d.ts.map +1 -0
  125. package/dist/types/Writeable.d.cts +9 -0
  126. package/dist/types/Writeable.d.mts +9 -0
  127. package/dist/types/Writeable.d.ts +9 -0
  128. package/dist/types/Writeable.d.ts.map +1 -0
  129. package/package.json +12 -2
  130. package/src/constants.ts +5 -0
  131. package/src/functions/array.ts +8 -2
  132. package/src/functions/assert.ts +41 -0
  133. package/src/functions/enums.ts +6 -0
  134. package/src/functions/map.ts +7 -0
  135. package/src/functions/math.ts +6 -0
  136. package/src/functions/object.ts +8 -3
  137. package/src/functions/random.ts +6 -0
  138. package/src/functions/set.ts +7 -0
  139. package/src/functions/sort.ts +6 -0
  140. package/src/functions/string.ts +25 -0
  141. package/src/functions/tuple.ts +6 -0
  142. package/src/functions/types.ts +6 -0
  143. package/src/functions/utils.test.ts +2 -8
  144. package/src/functions/utils.ts +7 -37
  145. package/src/index.ts +13 -12
  146. package/src/types/AddSubtract.ts +10 -2
  147. package/src/types/CompositionTypeSatisfiesEnum.ts +5 -3
  148. package/src/types/ObjectValues.ts +1 -0
  149. package/src/types/ReadonlyRecord.ts +5 -0
  150. package/src/types/Tuple.ts +1 -0
  151. 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 @@
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,6 +6,17 @@ const HOUR_IN_MILLISECONDS = 60 * MINUTE_IN_MILLISECONDS;
6
6
 
7
7
  const ReadonlySet = Set;
8
8
 
9
+ function assertDefined(value, ...[msg]) {
10
+ if (value === void 0) {
11
+ throw new TypeError(msg);
12
+ }
13
+ }
14
+ function assertNotNull(value, ...[msg]) {
15
+ if (value === null) {
16
+ throw new TypeError(msg);
17
+ }
18
+ }
19
+
9
20
  function getRandomInt(min, max, exceptions = []) {
10
21
  min = Math.ceil(min);
11
22
  max = Math.floor(max);
@@ -23,70 +34,6 @@ function getRandomInt(min, max, exceptions = []) {
23
34
  return randomInt;
24
35
  }
25
36
 
26
- const FLOAT_REGEX = /^-?\d*\.?\d+$/;
27
- const INTEGER_REGEX = /^-?\d+$/;
28
- function assertDefined(value, ...[msg]) {
29
- if (value === void 0) {
30
- throw new TypeError(msg);
31
- }
32
- }
33
- function assertNotNull(value, ...[msg]) {
34
- if (value === null) {
35
- throw new TypeError(msg);
36
- }
37
- }
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;
67
- }
68
- const number = Number.parseFloat(trimmedString);
69
- return Number.isNaN(number) ? void 0 : number;
70
- }
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;
78
- }
79
- const number = Number.parseInt(trimmedString, 10);
80
- return Number.isNaN(number) ? void 0 : number;
81
- }
82
- function repeat(num, func) {
83
- for (let i = 0; i < num; i++) {
84
- func(i);
85
- }
86
- }
87
- function todo(...args) {
88
- }
89
-
90
37
  function arrayCopyTwoDimensional(array) {
91
38
  const copiedArray = [];
92
39
  for (const subArray of array) {
@@ -117,7 +64,7 @@ function arrayRemoveInPlace(array, ...elementsToRemove) {
117
64
  const removedElements = [];
118
65
  for (const element of elementsToRemove) {
119
66
  const index = array.indexOf(element);
120
- if (index > -1) {
67
+ if (index !== -1) {
121
68
  const removedElement = array.splice(index, 1);
122
69
  removedElements.push(...removedElement);
123
70
  }
@@ -308,6 +255,60 @@ function sortCaseInsensitive(array) {
308
255
  return newArray;
309
256
  }
310
257
 
258
+ const FLOAT_REGEX = /^-?\d*\.?\d+$/;
259
+ const INTEGER_REGEX = /^-?\d+$/;
260
+ function* eRange(start, end, increment = 1) {
261
+ if (end === void 0) {
262
+ yield* eRange(0, start, increment);
263
+ return;
264
+ }
265
+ for (let i = start; i < end; i += increment) {
266
+ yield i;
267
+ }
268
+ }
269
+ function* iRange(start, end, increment = 1) {
270
+ if (end === void 0) {
271
+ yield* iRange(0, start, increment);
272
+ return;
273
+ }
274
+ const exclusiveEnd = end + 1;
275
+ yield* eRange(start, exclusiveEnd, increment);
276
+ }
277
+ function isKeyOf(key, target) {
278
+ return key in target;
279
+ }
280
+ function noop() {
281
+ }
282
+ function parseFloatSafe(string) {
283
+ if (typeof string !== "string") {
284
+ return void 0;
285
+ }
286
+ const trimmedString = string.trim();
287
+ if (FLOAT_REGEX.exec(trimmedString) === null) {
288
+ return void 0;
289
+ }
290
+ const number = Number.parseFloat(trimmedString);
291
+ return Number.isNaN(number) ? void 0 : number;
292
+ }
293
+ function parseIntSafe(string) {
294
+ if (typeof string !== "string") {
295
+ return void 0;
296
+ }
297
+ const trimmedString = string.trim();
298
+ if (INTEGER_REGEX.exec(trimmedString) === null) {
299
+ return void 0;
300
+ }
301
+ const number = Number.parseInt(trimmedString, 10);
302
+ return Number.isNaN(number) ? void 0 : number;
303
+ }
304
+ function repeat(num, func) {
305
+ for (let i = 0; i < num; i++) {
306
+ func(i);
307
+ }
308
+ }
309
+ function todo(...args) {
310
+ }
311
+
311
312
  const DIACRITIC_REGEX = /\p{Diacritic}/u;
312
313
  const EMOJI_REGEX = /(\p{Extended_Pictographic}|\p{Emoji_Component})/u;
313
314
  const FIRST_LETTER_CAPITALIZED_REGEX = /^\p{Lu}/u;