complete-common 1.0.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.
- package/LICENSE +9 -0
- package/README.md +7 -0
- package/dist/index.cjs +535 -0
- package/dist/index.d.cts +705 -0
- package/dist/index.d.mts +705 -0
- package/dist/index.d.ts +705 -0
- package/dist/index.mjs +463 -0
- package/package.json +36 -0
- package/src/constants.ts +3 -0
- package/src/functions/array.ts +209 -0
- package/src/functions/enums.ts +105 -0
- package/src/functions/map.ts +90 -0
- package/src/functions/math.ts +9 -0
- package/src/functions/object.ts +27 -0
- package/src/functions/random.ts +43 -0
- package/src/functions/set.ts +131 -0
- package/src/functions/sort.ts +18 -0
- package/src/functions/string.test.ts +42 -0
- package/src/functions/string.ts +304 -0
- package/src/functions/tuple.ts +31 -0
- package/src/functions/types.ts +15 -0
- package/src/functions/utils.test.ts +910 -0
- package/src/functions/utils.ts +238 -0
- package/src/index.ts +27 -0
- package/src/types/AddSubtract.ts +19 -0
- package/src/types/CompositionTypeSatisfiesEnum.ts +67 -0
- package/src/types/ERange.ts +15 -0
- package/src/types/IRange.ts +16 -0
- package/src/types/Immutable.ts +28 -0
- package/src/types/NaturalNumbersLessThan.ts +12 -0
- package/src/types/NaturalNumbersLessThanOrEqualTo.ts +14 -0
- package/src/types/ObjectValues.ts +1 -0
- package/src/types/ReadonlyMap.ts +12 -0
- package/src/types/ReadonlyRecord.ts +3 -0
- package/src/types/ReadonlySet.ts +9 -0
- package/src/types/Tuple.ts +14 -0
- package/src/types/WidenLiteral.ts +11 -0
- package/src/types/Writeable.ts +6 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
declare const SECOND_IN_MILLISECONDS = 1000;
|
|
2
|
+
declare const MINUTE_IN_MILLISECONDS: number;
|
|
3
|
+
declare const HOUR_IN_MILLISECONDS: number;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Helper function to copy a two-dimensional array. Note that the sub-arrays will only be shallow
|
|
7
|
+
* copied (using the spread operator).
|
|
8
|
+
*/
|
|
9
|
+
declare function arrayCopyTwoDimensional<T>(array: ReadonlyArray<readonly T[]>): T[][];
|
|
10
|
+
/**
|
|
11
|
+
* Helper function for determining if two arrays contain the exact same elements. Note that this
|
|
12
|
+
* only performs a shallow comparison.
|
|
13
|
+
*/
|
|
14
|
+
declare function arrayEquals<T>(array1: readonly T[], array2: readonly T[]): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Builds a new array based on the original array without the specified element(s). Returns the new
|
|
17
|
+
* array. If the specified element(s) are not found in the array, it will simply return a shallow
|
|
18
|
+
* copy of the array.
|
|
19
|
+
*
|
|
20
|
+
* This function is variadic, meaning that you can specify N arguments to remove N elements.
|
|
21
|
+
*/
|
|
22
|
+
declare function arrayRemove<T>(originalArray: readonly T[], ...elementsToRemove: readonly T[]): T[];
|
|
23
|
+
/**
|
|
24
|
+
* Removes the specified element(s) from the array. If the specified element(s) are not found in the
|
|
25
|
+
* array, this function will do nothing.
|
|
26
|
+
*
|
|
27
|
+
* This function is variadic, meaning that you can specify N arguments to remove N elements.
|
|
28
|
+
*
|
|
29
|
+
* If there is more than one matching element in the array, this function will only remove the first
|
|
30
|
+
* matching element. If you want to remove all of the elements, use the `arrayRemoveAllInPlace`
|
|
31
|
+
* function instead.
|
|
32
|
+
*
|
|
33
|
+
* @returns The removed elements. This will be an empty array if no elements were removed.
|
|
34
|
+
*/
|
|
35
|
+
declare function arrayRemoveInPlace<T>(array: T[], ...elementsToRemove: readonly T[]): T[];
|
|
36
|
+
/** Helper function to remove all of the elements in an array in-place. */
|
|
37
|
+
declare function emptyArray<T>(array: T[]): void;
|
|
38
|
+
/**
|
|
39
|
+
* Helper function to perform a filter and a map at the same time. Similar to `Array.map`, provide a
|
|
40
|
+
* function that transforms a value, but return `undefined` if the value should be skipped. (Thus,
|
|
41
|
+
* this function cannot be used in situations where `undefined` can be a valid array element.)
|
|
42
|
+
*
|
|
43
|
+
* This function is useful because the `Array.map` method will always produce an array with the same
|
|
44
|
+
* amount of elements as the original array.
|
|
45
|
+
*
|
|
46
|
+
* This is named `filterMap` after the Rust function:
|
|
47
|
+
* https://doc.rust-lang.org/std/iter/struct.FilterMap.html
|
|
48
|
+
*/
|
|
49
|
+
declare function filterMap<OldT, NewT>(array: readonly OldT[], func: (element: OldT) => NewT | undefined): NewT[];
|
|
50
|
+
/**
|
|
51
|
+
* Helper function to get a random element from the provided array.
|
|
52
|
+
*
|
|
53
|
+
* Note that this will only work with arrays that do not contain values of `undefined`, since the
|
|
54
|
+
* function uses `undefined` as an indication that the corresponding element does not exist.
|
|
55
|
+
*
|
|
56
|
+
* @param array The array to get an element from.
|
|
57
|
+
* @param exceptions Optional. An array of elements to skip over if selected.
|
|
58
|
+
*/
|
|
59
|
+
declare function getRandomArrayElement<T>(array: readonly T[], exceptions?: readonly T[]): T;
|
|
60
|
+
/**
|
|
61
|
+
* Helper function to get a random index from the provided array.
|
|
62
|
+
*
|
|
63
|
+
* @param array The array to get the index from.
|
|
64
|
+
* @param exceptions Optional. An array of indexes that will be skipped over when getting the random
|
|
65
|
+
* index. Default is an empty array.
|
|
66
|
+
*/
|
|
67
|
+
declare function getRandomArrayIndex<T>(array: readonly T[], exceptions?: readonly number[]): number;
|
|
68
|
+
/**
|
|
69
|
+
* Similar to the `Array.includes` method, but works on a widened version of the array.
|
|
70
|
+
*
|
|
71
|
+
* This is useful when the normal `Array.includes` produces a type error from an array that uses an
|
|
72
|
+
* `as const` assertion.
|
|
73
|
+
*/
|
|
74
|
+
declare function includes<T, TupleElement extends WidenLiteral<T>>(array: readonly TupleElement[], searchElement: WidenLiteral<T>): searchElement is TupleElement;
|
|
75
|
+
/** A wrapper around `Array.isArray` that narrows to `unknown[]` instead of `any[]`. */
|
|
76
|
+
declare function isArray(arg: unknown): arg is unknown[];
|
|
77
|
+
/** Initializes an array with all elements containing the specified default value. */
|
|
78
|
+
declare function newArray<T>(length: number, value: T): T[];
|
|
79
|
+
/** Helper function to sum every value in an array together. */
|
|
80
|
+
declare function sumArray(array: readonly number[]): number;
|
|
81
|
+
|
|
82
|
+
type TranspiledEnum = Record<string, string | number>;
|
|
83
|
+
/**
|
|
84
|
+
* Helper function to get the entries of an enum.
|
|
85
|
+
*
|
|
86
|
+
* (By default, TypeScript will put the keys inside of the values of a number-based enum, so those
|
|
87
|
+
* have to be filtered out.)
|
|
88
|
+
*
|
|
89
|
+
* This function will work properly for both number and string enums.
|
|
90
|
+
*/
|
|
91
|
+
declare function getEnumEntries<T extends TranspiledEnum>(transpiledEnum: T): ReadonlyArray<[key: string, value: T[keyof T]]>;
|
|
92
|
+
/**
|
|
93
|
+
* Helper function to get the keys of an enum.
|
|
94
|
+
*
|
|
95
|
+
* (By default, TypeScript will put the keys inside of the values of a number-based enum, so those
|
|
96
|
+
* have to be filtered out.)
|
|
97
|
+
*
|
|
98
|
+
* This function will work properly for both number and string enums.
|
|
99
|
+
*/
|
|
100
|
+
declare function getEnumKeys(transpiledEnum: TranspiledEnum): readonly string[];
|
|
101
|
+
/**
|
|
102
|
+
* Helper function to get the only the values of an enum.
|
|
103
|
+
*
|
|
104
|
+
* (By default, TypeScript will put the keys inside of the values of a number-based enum, so those
|
|
105
|
+
* have to be filtered out.)
|
|
106
|
+
*
|
|
107
|
+
* This function will work properly for both number and string enums.
|
|
108
|
+
*/
|
|
109
|
+
declare function getEnumValues<T extends TranspiledEnum>(transpiledEnum: T): ReadonlyArray<T[keyof T]>;
|
|
110
|
+
/**
|
|
111
|
+
* Helper function to validate that an interface contains all of the keys of an enum. You must
|
|
112
|
+
* specify both generic parameters in order for this to work properly (i.e. the interface and then
|
|
113
|
+
* the enum).
|
|
114
|
+
*
|
|
115
|
+
* For example:
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* enum MyEnum {
|
|
119
|
+
* Value1,
|
|
120
|
+
* Value2,
|
|
121
|
+
* Value3,
|
|
122
|
+
* }
|
|
123
|
+
*
|
|
124
|
+
* interface MyEnumToType {
|
|
125
|
+
* [MyEnum.Value1]: boolean;
|
|
126
|
+
* [MyEnum.Value2]: number;
|
|
127
|
+
* [MyEnum.Value3]: string;
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* interfaceSatisfiesEnum<MyEnumToType, MyEnum>();
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* This function is only meant to be used with interfaces (i.e. types that will not exist at
|
|
134
|
+
* run-time). If you are generating an object that will contain all of the keys of an enum, use the
|
|
135
|
+
* `satisfies` operator with the `Record` type instead.
|
|
136
|
+
*/
|
|
137
|
+
declare function interfaceSatisfiesEnum<T extends Record<Enum, unknown>, Enum extends string | number>(): void;
|
|
138
|
+
/**
|
|
139
|
+
* Helper function to validate that a particular value exists inside of an enum.
|
|
140
|
+
*
|
|
141
|
+
* @param value The value to check.
|
|
142
|
+
* @param transpiledEnum The enum to check against.
|
|
143
|
+
* @param set Optional. A set that contains all of the values of an enum. If provided, this function
|
|
144
|
+
* will check for existence using the set (instead of the enum itself). Using a set
|
|
145
|
+
* should be more performant for enums with around 52 or more elements.
|
|
146
|
+
*/
|
|
147
|
+
declare function isEnumValue<T extends TranspiledEnum>(value: number | string, transpiledEnum: T, set?: ReadonlySet<string | number>): value is T[keyof T];
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Helper function to get the values in a `Map` that match an arbitrary condition. Similar to the
|
|
151
|
+
* `Array.map` method, but works for maps.
|
|
152
|
+
*
|
|
153
|
+
* This is efficient such that it avoids converting the map values into an array.
|
|
154
|
+
*
|
|
155
|
+
* If you want to perform a filter and a map at the same time on an array, use the `filterMap`
|
|
156
|
+
* helper function instead.
|
|
157
|
+
*/
|
|
158
|
+
declare function mapFilter<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V) => boolean): V[];
|
|
159
|
+
/**
|
|
160
|
+
* Helper function to find a value in a `Map`. Similar to the `Array.find` method, but works for
|
|
161
|
+
* maps.
|
|
162
|
+
*
|
|
163
|
+
* This is efficient such that it avoids converting the map values into an array.
|
|
164
|
+
*/
|
|
165
|
+
declare function mapFind<K, V>(map: ReadonlyMap<K, V>, predicate: (value: V) => boolean): V | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Helper function to convert an object to a map.
|
|
168
|
+
*
|
|
169
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
170
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
171
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
172
|
+
* assertions.
|
|
173
|
+
*
|
|
174
|
+
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
175
|
+
* hood.
|
|
176
|
+
*
|
|
177
|
+
* Also see the `objectToReadonlyMap` function.
|
|
178
|
+
*/
|
|
179
|
+
declare function objectToMap<K extends string | number | symbol, V>(object: Record<K, V>): Map<K, V>;
|
|
180
|
+
/**
|
|
181
|
+
* Helper function to convert an object to a read-only map.
|
|
182
|
+
*
|
|
183
|
+
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
184
|
+
* then later on you need to query it in a way where you expect the return value to be T or
|
|
185
|
+
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
186
|
+
* assertions.
|
|
187
|
+
*
|
|
188
|
+
* Note that the map values will be inserted in a random order, due to how `pairs` works under the
|
|
189
|
+
* hood.
|
|
190
|
+
*
|
|
191
|
+
* Also see the `objectToMap` function.
|
|
192
|
+
*/
|
|
193
|
+
declare function objectToReadonlyMap<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlyMap<K, V>;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Helper function to normalize a number, ensuring that it is within a certain range.
|
|
197
|
+
*
|
|
198
|
+
* - If `num` is less than `min`, then it will be clamped to `min`.
|
|
199
|
+
* - If `num` is greater than `max`, then it will be clamped to `max`.
|
|
200
|
+
*/
|
|
201
|
+
declare function clamp(num: number, min: number, max: number): number;
|
|
202
|
+
|
|
203
|
+
type ReadonlyRecord<K extends string | number | symbol, V> = Readonly<Record<K, V>>;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Helper function to get the values in an object that match an arbitrary condition. Similar to the
|
|
207
|
+
* `Array.map` method, but works for objects.
|
|
208
|
+
*
|
|
209
|
+
* This is efficient such that it avoids converting the object values into an array.
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
declare function objectFilter<K extends string | number | symbol, V>(object: ReadonlyRecord<K, V>, predicate: (value: V) => boolean): V[];
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* This returns a random integer between min and max. It is inclusive on both ends.
|
|
216
|
+
*
|
|
217
|
+
* For example:
|
|
218
|
+
*
|
|
219
|
+
* ```ts
|
|
220
|
+
* const oneTwoOrThree = getRandomInt(1, 3);
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* @param min The lower bound for the random number (inclusive).
|
|
224
|
+
* @param max The upper bound for the random number (inclusive).
|
|
225
|
+
* @param exceptions Optional. An array of elements that will be skipped over when getting the
|
|
226
|
+
* random integer. For example, a min of 1, a max of 4, and an exceptions array of
|
|
227
|
+
* `[2]` would cause the function to return either 1, 3, or 4. Default is an empty
|
|
228
|
+
* array.
|
|
229
|
+
*/
|
|
230
|
+
declare function getRandomInt(min: number, max: number, exceptions?: readonly number[]): number;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Helper function to add all of the values in one set to another set. The first set passed will be
|
|
234
|
+
* modified in place.
|
|
235
|
+
*
|
|
236
|
+
* This function is variadic, meaning that you can specify N sets to add to the first set.
|
|
237
|
+
*/
|
|
238
|
+
declare function addSetsToSet<T>(mainSet: Set<T>, ...setsToAdd: ReadonlyArray<ReadonlySet<T>>): void;
|
|
239
|
+
/**
|
|
240
|
+
* Helper function to create a new set that is the composition of two or more sets.
|
|
241
|
+
*
|
|
242
|
+
* This function is variadic, meaning that you can specify N sets.
|
|
243
|
+
*/
|
|
244
|
+
declare function combineSets<T>(...sets: ReadonlyArray<ReadonlySet<T>>): Set<T>;
|
|
245
|
+
/** Helper function to copy a set. (You can also use a Set constructor to accomplish this task.) */
|
|
246
|
+
declare function copySet<T>(oldSet: ReadonlySet<T>): Set<T>;
|
|
247
|
+
/**
|
|
248
|
+
* Helper function to convert the keys of an object to a read-only set.
|
|
249
|
+
*
|
|
250
|
+
* Also see the `objectKeysToSet` function.
|
|
251
|
+
*/
|
|
252
|
+
declare function objectKeysToReadonlySet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<K>;
|
|
253
|
+
/**
|
|
254
|
+
* Helper function to convert the keys of an object to a set.
|
|
255
|
+
*
|
|
256
|
+
* Also see the `objectKeysToReadonlySet` function.
|
|
257
|
+
*/
|
|
258
|
+
declare function objectKeysToSet<K extends string | number | symbol, V>(object: Record<K, V>): Set<K>;
|
|
259
|
+
/**
|
|
260
|
+
* Helper function to convert the values of an object to a read-only set.
|
|
261
|
+
*
|
|
262
|
+
* Also see the `objectValuesToSet` function.
|
|
263
|
+
*/
|
|
264
|
+
declare function objectValuesToReadonlySet<K extends string | number | symbol, V>(object: Record<K, V>): ReadonlySet<V>;
|
|
265
|
+
/**
|
|
266
|
+
* Helper function to convert the values of an object to a set.
|
|
267
|
+
*
|
|
268
|
+
* Also see the `objectValuesToReadonlySet` function.
|
|
269
|
+
*/
|
|
270
|
+
declare function objectValuesToSet<K extends string | number | symbol, V>(object: Record<K, V>): Set<V>;
|
|
271
|
+
/**
|
|
272
|
+
* Helper function to add one or more elements to a set at once without having to repeatedly call
|
|
273
|
+
* the `Set.add` method.
|
|
274
|
+
*
|
|
275
|
+
* This function is variadic, meaning that you can pass as many things as you want to add.
|
|
276
|
+
*/
|
|
277
|
+
declare function setAdd<T>(set: Set<T>, ...elements: readonly T[]): void;
|
|
278
|
+
/**
|
|
279
|
+
* Helper function to check for one or more elements in a set at once without having to repeatedly
|
|
280
|
+
* call the `Set.has` method.
|
|
281
|
+
*
|
|
282
|
+
* This function is variadic, meaning that you can pass as many things as you want to check for. It
|
|
283
|
+
* will return true if one or more elements are found.
|
|
284
|
+
*/
|
|
285
|
+
declare function setHas<T>(set: ReadonlySet<T>, ...elements: readonly T[]): boolean;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Helper function to perform a case insensitive sort. This will copy the provided array and return
|
|
289
|
+
* the sorted copy.
|
|
290
|
+
*
|
|
291
|
+
* From:
|
|
292
|
+
* https://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-array-of-string-in-javascript
|
|
293
|
+
*/
|
|
294
|
+
declare function sortCaseInsensitive(array: readonly string[]): string[];
|
|
295
|
+
|
|
296
|
+
declare function capitalizeFirstLetter(string: string): string;
|
|
297
|
+
/**
|
|
298
|
+
* Helper function to replace all of the ampersands, less than signs, greater than signs, double
|
|
299
|
+
* quotes, and single quotes in a string with the escaped counterparts. For example, "<" will be
|
|
300
|
+
* replaced with "<".
|
|
301
|
+
*/
|
|
302
|
+
declare function escapeHTMLCharacters(string: string): string;
|
|
303
|
+
declare function getNumConsecutiveDiacritics(string: string): number;
|
|
304
|
+
declare function hasDiacritic(string: string): boolean;
|
|
305
|
+
declare function hasEmoji(string: string): boolean;
|
|
306
|
+
/** From: https://stackoverflow.com/questions/1731190/check-if-a-string-has-white-space */
|
|
307
|
+
declare function hasWhitespace(string: string): boolean;
|
|
308
|
+
/**
|
|
309
|
+
* From:
|
|
310
|
+
* https://stackoverflow.com/questions/8334606/check-if-first-letter-of-word-is-a-capital-letter
|
|
311
|
+
*/
|
|
312
|
+
declare function isFirstLetterCapitalized(string: string): boolean;
|
|
313
|
+
/** Kebab case is the naming style of using all lowercase and hyphens, like "foo-bar". */
|
|
314
|
+
declare function isKebabCase(string: string): boolean;
|
|
315
|
+
/**
|
|
316
|
+
* Helper function to check if a given string is a valid Semantic Version.
|
|
317
|
+
*
|
|
318
|
+
* @see https://semver.org/
|
|
319
|
+
*/
|
|
320
|
+
declare function isSemanticVersion(versionString: string): boolean;
|
|
321
|
+
declare function kebabCaseToCamelCase(string: string): string;
|
|
322
|
+
/**
|
|
323
|
+
* Helper function to normalize a string. Specifically, this performs the following steps:
|
|
324
|
+
*
|
|
325
|
+
* - Removes any non-printable characters, if any.
|
|
326
|
+
* - Normalizes all newlines to "\n".
|
|
327
|
+
* - Normalizes all spaces to " ".
|
|
328
|
+
* - Removes leading/trailing whitespace.
|
|
329
|
+
*
|
|
330
|
+
* @see
|
|
331
|
+
* https://stackoverflow.com/questions/11598786/how-to-replace-non-printable-unicode-characters-javascript
|
|
332
|
+
*/
|
|
333
|
+
declare function normalizeString(string: string): string;
|
|
334
|
+
/**
|
|
335
|
+
* Helper function to parse a Semantic Versioning string into its individual constituents. Returns
|
|
336
|
+
* undefined if the submitted string was not a proper Semantic Version.
|
|
337
|
+
*
|
|
338
|
+
* @see https://semver.org/
|
|
339
|
+
*/
|
|
340
|
+
declare function parseSemanticVersion(versionString: string): {
|
|
341
|
+
majorVersion: number;
|
|
342
|
+
minorVersion: number;
|
|
343
|
+
patchVersion: number;
|
|
344
|
+
} | undefined;
|
|
345
|
+
/**
|
|
346
|
+
* Helper function to remove lines from a multi-line string. This function looks for a "-start" and
|
|
347
|
+
* a "-end" suffix after the marker. Lines with markets will be completely removed from the output.
|
|
348
|
+
*
|
|
349
|
+
* For example, by using a marker of "@foo":
|
|
350
|
+
*
|
|
351
|
+
* ```text
|
|
352
|
+
* line1
|
|
353
|
+
* # @foo-start
|
|
354
|
+
* line2
|
|
355
|
+
* line3
|
|
356
|
+
* # @foo-end
|
|
357
|
+
* line4
|
|
358
|
+
* ```
|
|
359
|
+
*
|
|
360
|
+
* Would return:
|
|
361
|
+
*
|
|
362
|
+
* ```text
|
|
363
|
+
* line1
|
|
364
|
+
* line4
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
declare function removeLinesBetweenMarkers(string: string, marker: string): string;
|
|
368
|
+
/** Helper function to remove lines from a multi-line string matching a certain other string. */
|
|
369
|
+
declare function removeLinesMatching(string: string, match: string): string;
|
|
370
|
+
/**
|
|
371
|
+
* Helper function to remove all non-printable characters from a string.
|
|
372
|
+
*
|
|
373
|
+
* @see
|
|
374
|
+
* https://stackoverflow.com/questions/11598786/how-to-replace-non-printable-unicode-characters-javascript
|
|
375
|
+
*/
|
|
376
|
+
declare function removeNonPrintableCharacters(string: string): string;
|
|
377
|
+
/** Helper function to remove all whitespace characters from a string. */
|
|
378
|
+
declare function removeWhitespace(string: string): string;
|
|
379
|
+
/**
|
|
380
|
+
* Helper function to trim a prefix from a string, if it exists. Returns the trimmed string.
|
|
381
|
+
*
|
|
382
|
+
* @param string The string to trim.
|
|
383
|
+
* @param prefix The prefix to trim.
|
|
384
|
+
* @param trimAll Whether to remove multiple instances of the prefix, if they exist. If this is set
|
|
385
|
+
* to true, the prefix must only be a single character.
|
|
386
|
+
*/
|
|
387
|
+
declare function trimPrefix(string: string, prefix: string, trimAll?: boolean): string;
|
|
388
|
+
/** Helper function to trim a suffix from a string, if it exists. Returns the trimmed string. */
|
|
389
|
+
declare function trimSuffix(string: string, prefix: string): string;
|
|
390
|
+
/**
|
|
391
|
+
* Helper function to truncate a string to a maximum length. If the length of the string is less
|
|
392
|
+
* than or equal to the provided maximum length, the string will be returned unmodified.
|
|
393
|
+
*/
|
|
394
|
+
declare function truncateString(string: string, maxLength: number): string;
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Helper type to represent a tuple of length N.
|
|
398
|
+
*
|
|
399
|
+
* From:
|
|
400
|
+
* https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
|
|
401
|
+
*/
|
|
402
|
+
type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
|
|
403
|
+
type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
|
|
404
|
+
|
|
405
|
+
type TupleKey<T extends readonly unknown[]> = {
|
|
406
|
+
[L in T["length"]]: Exclude<Partial<Tuple<unknown, L>>["length"], L>;
|
|
407
|
+
}[T["length"]];
|
|
408
|
+
type TupleValue<T extends readonly unknown[]> = T[0];
|
|
409
|
+
type TupleEntry<T extends readonly unknown[]> = [TupleKey<T>, TupleValue<T>];
|
|
410
|
+
/**
|
|
411
|
+
* Helper function to get the entries (i.e. indexes and values) of a tuple in a type-safe way.
|
|
412
|
+
*
|
|
413
|
+
* This is useful because the vanilla `Array.entries` method will always have the keys be of type
|
|
414
|
+
* `number`.
|
|
415
|
+
*/
|
|
416
|
+
declare function tupleEntries<T extends readonly unknown[]>(tuple: T): Generator<TupleEntry<T>>;
|
|
417
|
+
/**
|
|
418
|
+
* Helper function to get the keys (i.e. indexes) of a tuple in a type-safe way.
|
|
419
|
+
*
|
|
420
|
+
* This is useful because the vanilla `Array.keys` method will always have the keys be of type
|
|
421
|
+
* `number`.
|
|
422
|
+
*/
|
|
423
|
+
declare function tupleKeys<T extends readonly unknown[]>(tuple: T): Generator<TupleKey<T>>;
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Helper function to narrow an unknown value to an object (i.e. a TypeScript record).
|
|
427
|
+
*
|
|
428
|
+
* Under the hood, this checks for `typeof variable === "object"`, `variable !== null`, and
|
|
429
|
+
* `!Array.isArray(variable)`.
|
|
430
|
+
*/
|
|
431
|
+
declare function isObject(variable: unknown): variable is Record<string, unknown>;
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
435
|
+
*
|
|
436
|
+
* This is useful to have TypeScript narrow a `T | undefined` value to `T` in a concise way.
|
|
437
|
+
*/
|
|
438
|
+
declare function assertDefined<T>(value: T, ...[msg]: [undefined] extends [T] ? [string] : [
|
|
439
|
+
"The assertion is useless because the provided value does not contain undefined."
|
|
440
|
+
]): asserts value is Exclude<T, undefined>;
|
|
441
|
+
/**
|
|
442
|
+
* Helper function to throw an error if the provided value is equal to `null`.
|
|
443
|
+
*
|
|
444
|
+
* This is useful to have TypeScript narrow a `T | null` value to `T` in a concise way.
|
|
445
|
+
*/
|
|
446
|
+
declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T] ? [string] : [
|
|
447
|
+
"The assertion is useless because the provided value does not contain null."
|
|
448
|
+
]): asserts value is Exclude<T, null>;
|
|
449
|
+
/**
|
|
450
|
+
* Helper function to get an iterator of integers with the specified range, inclusive on the lower
|
|
451
|
+
* end and exclusive on the high end. (The "e" in the function name stands for exclusive.) Thus,
|
|
452
|
+
* this function works in the same way as the built-in `range` function from Python.
|
|
453
|
+
*
|
|
454
|
+
* If the end is lower than the start, then an empty range will be returned.
|
|
455
|
+
*
|
|
456
|
+
* For example:
|
|
457
|
+
*
|
|
458
|
+
* - `eRange(2)` returns `[0, 1]`.
|
|
459
|
+
* - `eRange(3)` returns `[0, 1, 2]`.
|
|
460
|
+
* - `eRange(-3)` returns `[0, -1, -2]`.
|
|
461
|
+
* - `eRange(1, 3)` returns `[1, 2]`.
|
|
462
|
+
* - `eRange(2, 5)` returns `[2, 3, 4]`.
|
|
463
|
+
* - `eRange(5, 2)` returns `[]`.
|
|
464
|
+
* - `eRange(3, 3)` returns `[]`.
|
|
465
|
+
*
|
|
466
|
+
* If you want an array instead of an iterator, use the spread operator like this:
|
|
467
|
+
*
|
|
468
|
+
* ```ts
|
|
469
|
+
* const myArray = [...eRange(1, 3)];
|
|
470
|
+
* ```
|
|
471
|
+
*
|
|
472
|
+
* @param start The integer to start at.
|
|
473
|
+
* @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
|
|
474
|
+
* first argument will be the end.
|
|
475
|
+
* @param increment Optional. The increment to use. Default is 1.
|
|
476
|
+
*/
|
|
477
|
+
declare function eRange(start: number, end?: number, increment?: number): Generator<number>;
|
|
478
|
+
/**
|
|
479
|
+
* Helper function to get an array of integers with the specified range, inclusive on both ends.
|
|
480
|
+
* (The "i" in the function name stands for inclusive.)
|
|
481
|
+
*
|
|
482
|
+
* If the end is lower than the start, then an empty range will be returned.
|
|
483
|
+
*
|
|
484
|
+
* For example:
|
|
485
|
+
*
|
|
486
|
+
* - `iRange(2)` returns `[0, 1, 2]`.
|
|
487
|
+
* - `iRange(3)` returns `[0, 1, 2, 3]`.
|
|
488
|
+
* - `iRange(-3)` returns `[0, -1, -2, -3]`.
|
|
489
|
+
* - `iRange(1, 3)` returns `[1, 2, 3]`.
|
|
490
|
+
* - `iRange(2, 5)` returns `[2, 3, 4, 5]`.
|
|
491
|
+
* - `iRange(5, 2)` returns `[]`.
|
|
492
|
+
* - `iRange(3, 3)` returns `[3]`.
|
|
493
|
+
*
|
|
494
|
+
* If you want an array instead of an iterator, use the spread operator like this:
|
|
495
|
+
*
|
|
496
|
+
* ```ts
|
|
497
|
+
* const myArray = [...eRange(1, 3)];
|
|
498
|
+
* ```
|
|
499
|
+
*
|
|
500
|
+
* @param start The integer to start at.
|
|
501
|
+
* @param end Optional. The integer to end at. If not specified, then the start will be 0 and the
|
|
502
|
+
* first argument will be the end.
|
|
503
|
+
* @param increment Optional. The increment to use. Default is 1.
|
|
504
|
+
*/
|
|
505
|
+
declare function iRange(start: number, end?: number, increment?: number): Generator<number>;
|
|
506
|
+
/** From: https://stackoverflow.com/questions/61526746 */
|
|
507
|
+
declare function isKeyOf<T extends object>(key: PropertyKey, target: T): key is keyof T;
|
|
508
|
+
/**
|
|
509
|
+
* Helper function to perform a no-op. This can be useful in order to make a trailing return valid
|
|
510
|
+
* in functions that use the early return pattern.
|
|
511
|
+
*/
|
|
512
|
+
declare function noop(): void;
|
|
513
|
+
/**
|
|
514
|
+
* This is a more reliable version of `Number.parseFloat`:
|
|
515
|
+
*
|
|
516
|
+
* - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
|
|
517
|
+
* TypeScript type narrowing patterns.
|
|
518
|
+
* - Strings that are a mixture of numbers and letters will result in undefined instead of the part
|
|
519
|
+
* of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
|
|
520
|
+
* - Non-strings will result in undefined instead of being coerced to a number.
|
|
521
|
+
*
|
|
522
|
+
* @param string A string to convert to an integer.
|
|
523
|
+
*/
|
|
524
|
+
declare function parseFloatSafe(string: string): number | undefined;
|
|
525
|
+
/**
|
|
526
|
+
* This is a more reliable version of `Number.parseInt`:
|
|
527
|
+
*
|
|
528
|
+
* - `undefined` is returned instead of `Number.NaN`, which is helpful in conjunction with
|
|
529
|
+
* TypeScript type narrowing patterns.
|
|
530
|
+
* - Strings that are a mixture of numbers and letters will result in undefined instead of the part
|
|
531
|
+
* of the string that is the number. (e.g. "1a" --> undefined instead of "1a" --> 1)
|
|
532
|
+
* - Non-strings will result in undefined instead of being coerced to a number.
|
|
533
|
+
*
|
|
534
|
+
* If you have to use a radix other than 10, use the vanilla `Number.parseInt` function instead,
|
|
535
|
+
* because this function ensures that the string contains no letters.
|
|
536
|
+
*/
|
|
537
|
+
declare function parseIntSafe(string: string): number | undefined;
|
|
538
|
+
/**
|
|
539
|
+
* Helper function to repeat code N times. This is faster to type and cleaner than using a for loop.
|
|
540
|
+
*
|
|
541
|
+
* For example:
|
|
542
|
+
*
|
|
543
|
+
* ```ts
|
|
544
|
+
* repeat(10, () => {
|
|
545
|
+
* foo();
|
|
546
|
+
* });
|
|
547
|
+
* ```
|
|
548
|
+
*
|
|
549
|
+
* The repeated function is passed the index of the iteration, if needed:
|
|
550
|
+
*
|
|
551
|
+
* ```ts
|
|
552
|
+
* repeat(3, (i) => {
|
|
553
|
+
* console.log(i); // Prints "0", "1", "2"
|
|
554
|
+
* });
|
|
555
|
+
* ```
|
|
556
|
+
*/
|
|
557
|
+
declare function repeat(num: number, func: (i: number) => void): void;
|
|
558
|
+
/**
|
|
559
|
+
* Helper function to signify that the enclosing code block is not yet complete. Using this function
|
|
560
|
+
* is similar to writing a "TODO" comment, but it has the benefit of preventing ESLint errors due to
|
|
561
|
+
* unused variables or early returns.
|
|
562
|
+
*
|
|
563
|
+
* When you see this function, it simply means that the programmer intends to add in more code to
|
|
564
|
+
* this spot later.
|
|
565
|
+
*
|
|
566
|
+
* This function is variadic, meaning that you can pass as many arguments as you want. (This is
|
|
567
|
+
* useful as a means to prevent unused variables.)
|
|
568
|
+
*
|
|
569
|
+
* This function does not actually do anything. (It is an "empty" function.)
|
|
570
|
+
*
|
|
571
|
+
* @allowEmptyVariadic
|
|
572
|
+
*/
|
|
573
|
+
declare function todo(...args: readonly unknown[]): void;
|
|
574
|
+
|
|
575
|
+
/** From: https://gist.github.com/ryandabler/8b4ff4f36aed47bc09acc03174638468 */
|
|
576
|
+
type Add<A extends number, B extends number> = Length<[
|
|
577
|
+
...BuildTuple<A>,
|
|
578
|
+
...BuildTuple<B>
|
|
579
|
+
]>;
|
|
580
|
+
/** From: https://gist.github.com/ryandabler/8b4ff4f36aed47bc09acc03174638468 */
|
|
581
|
+
type Subtract<A extends number, B extends number> = A extends A ? BuildTuple<A> extends [...infer U, ...BuildTuple<B>] ? Length<U> : never : never;
|
|
582
|
+
type BuildTuple<L extends number, T extends unknown[] = []> = T extends {
|
|
583
|
+
length: L;
|
|
584
|
+
} ? T : BuildTuple<L, [...T, unknown]>;
|
|
585
|
+
type Length<T extends unknown[]> = T extends {
|
|
586
|
+
length: infer L;
|
|
587
|
+
} ? L : never;
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Helper type to validate that a union of interfaces with a field of `type` that is based on an
|
|
591
|
+
* enum is complete.
|
|
592
|
+
*
|
|
593
|
+
* For example:
|
|
594
|
+
*
|
|
595
|
+
* ```ts
|
|
596
|
+
* enum ObjectiveType {
|
|
597
|
+
* Foo,
|
|
598
|
+
* Bar,
|
|
599
|
+
* Baz,
|
|
600
|
+
* }
|
|
601
|
+
*
|
|
602
|
+
* interface FooObjective {
|
|
603
|
+
* type: ObjectiveType.Foo;
|
|
604
|
+
* fooThing: number;
|
|
605
|
+
* }
|
|
606
|
+
*
|
|
607
|
+
* interface BarObjective {
|
|
608
|
+
* type: ObjectiveType.Bar;
|
|
609
|
+
* barThing: string;
|
|
610
|
+
* }
|
|
611
|
+
*
|
|
612
|
+
* type Objective = FooObjective | BarObjective;
|
|
613
|
+
* type _Test = CompositionTypeSatisfiesEnum<Objective, ObjectiveType>;
|
|
614
|
+
* ```
|
|
615
|
+
*
|
|
616
|
+
* In this example, `Test` would be flagged by TypeScript because `Objective` does not contain an
|
|
617
|
+
* entry for `BazObjective`.
|
|
618
|
+
*/
|
|
619
|
+
type CompositionTypeSatisfiesEnum<T extends {
|
|
620
|
+
type: unknown;
|
|
621
|
+
}, Enum extends T["type"]> = unknown;
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Helper type to get a range of integers between 0 and N - 1.
|
|
625
|
+
*
|
|
626
|
+
* From:
|
|
627
|
+
* https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
|
|
628
|
+
*/
|
|
629
|
+
type NaturalNumbersLessThan<N extends number, Acc extends number[] = []> = Acc["length"] extends N ? Acc[number] : NaturalNumbersLessThan<N, [...Acc, Acc["length"]]>;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Helper type to get a range of integers. It is inclusive on the lower end and exclusive on the
|
|
633
|
+
* high end. (The "E" in the type name stands for exclusive.)
|
|
634
|
+
*
|
|
635
|
+
* For example, `ERange<3, 5>` will return `3 | 4`.
|
|
636
|
+
*
|
|
637
|
+
* From:
|
|
638
|
+
* https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
|
|
639
|
+
*/
|
|
640
|
+
type ERange<Low extends number, High extends number> = Exclude<NaturalNumbersLessThan<High>, NaturalNumbersLessThan<Low>>;
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Immutable is a utility type that will make the given array/map/set/object recursively read-only.
|
|
644
|
+
*
|
|
645
|
+
* You can use this type to easily build safe data structures.
|
|
646
|
+
*
|
|
647
|
+
* From: https://stackoverflow.com/questions/41879327/deepreadonly-object-typescript
|
|
648
|
+
*/
|
|
649
|
+
type Immutable<T> = T extends ImmutablePrimitive ? T : T extends Array<infer U> ? ImmutableArray<U> : T extends Map<infer K, infer V> ? ImmutableMap<K, V> : T extends Set<infer M> ? ImmutableSet<M> : ImmutableObject<T>;
|
|
650
|
+
type ImmutablePrimitive = undefined | null | boolean | string | number | Function;
|
|
651
|
+
type ImmutableArray<T> = ReadonlyArray<Immutable<T>>;
|
|
652
|
+
type ImmutableMap<K, V> = ReadonlyMap<Immutable<K>, Immutable<V>>;
|
|
653
|
+
type ImmutableSet<T> = ReadonlySet<Immutable<T>>;
|
|
654
|
+
type ImmutableObject<T> = {
|
|
655
|
+
readonly [K in keyof T]: Immutable<T[K]>;
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Helper type to get a range of integers between 0 and N.
|
|
660
|
+
*
|
|
661
|
+
* From:
|
|
662
|
+
* https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
|
|
663
|
+
*/
|
|
664
|
+
type NaturalNumbersLessThanOrEqualTo<N extends number, T extends number[] = []> = T extends [unknown, ...infer Tail] ? Tail["length"] extends N ? T[number] : NaturalNumbersLessThanOrEqualTo<N, [...T, T["length"]]> : NaturalNumbersLessThanOrEqualTo<N, [...T, T["length"]]>;
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Helper type to get a range of integers. It is inclusive on both ends. (The "I" in the type name
|
|
668
|
+
* stands for inclusive.)
|
|
669
|
+
*
|
|
670
|
+
* For example, `IRange<3, 5>` will return `3 | 4 | 5`.
|
|
671
|
+
*
|
|
672
|
+
* From:
|
|
673
|
+
* https://stackoverflow.com/questions/39494689/is-it-possible-to-restrict-number-to-a-certain-range
|
|
674
|
+
*/
|
|
675
|
+
type IRange<Low extends number, High extends number> = Exclude<NaturalNumbersLessThanOrEqualTo<High>, NaturalNumbersLessThan<Low>>;
|
|
676
|
+
|
|
677
|
+
type ObjectValues<T> = T[keyof T];
|
|
678
|
+
|
|
679
|
+
interface ReadonlyMapConstructor {
|
|
680
|
+
new (): ReadonlyMap<any, any>;
|
|
681
|
+
new <K, V>(entries?: ReadonlyArray<readonly [K, V]> | Iterable<readonly [K, V]> | null): ReadonlyMap<K, V>;
|
|
682
|
+
readonly prototype: ReadonlyMap<any, any>;
|
|
683
|
+
}
|
|
684
|
+
/** An alias for the `Map` constructor that returns a read-only map. */
|
|
685
|
+
declare const ReadonlyMap: ReadonlyMapConstructor;
|
|
686
|
+
|
|
687
|
+
interface ReadonlySetConstructor {
|
|
688
|
+
new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
|
|
689
|
+
readonly prototype: ReadonlySet<any>;
|
|
690
|
+
}
|
|
691
|
+
/** An alias for the `Set` constructor that returns a read-only set. */
|
|
692
|
+
declare const ReadonlySet: ReadonlySetConstructor;
|
|
693
|
+
|
|
694
|
+
type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Helper type to convert a read-only object into a writable object.
|
|
698
|
+
*
|
|
699
|
+
* This is the opposite of the built-in `Readonly` utility type.
|
|
700
|
+
*/
|
|
701
|
+
type Writeable<T> = {
|
|
702
|
+
-readonly [P in keyof T]: T[P];
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
export { type Add, type CompositionTypeSatisfiesEnum, type ERange, HOUR_IN_MILLISECONDS, type IRange, type Immutable, MINUTE_IN_MILLISECONDS, type NaturalNumbersLessThan, type NaturalNumbersLessThanOrEqualTo, type ObjectValues, ReadonlyMap as ReadonlyMap, type ReadonlyRecord, ReadonlySet as ReadonlySet, SECOND_IN_MILLISECONDS, type Subtract, type Tuple, type WidenLiteral, type Writeable, addSetsToSet, arrayCopyTwoDimensional, arrayEquals, arrayRemove, arrayRemoveInPlace, assertDefined, assertNotNull, capitalizeFirstLetter, clamp, combineSets, copySet, eRange, emptyArray, escapeHTMLCharacters, filterMap, getEnumEntries, getEnumKeys, getEnumValues, getNumConsecutiveDiacritics, getRandomArrayElement, getRandomArrayIndex, getRandomInt, hasDiacritic, hasEmoji, hasWhitespace, iRange, includes, interfaceSatisfiesEnum, isArray, isEnumValue, isFirstLetterCapitalized, isKebabCase, isKeyOf, isObject, isSemanticVersion, kebabCaseToCamelCase, mapFilter, mapFind, newArray, noop, normalizeString, objectFilter, objectKeysToReadonlySet, objectKeysToSet, objectToMap, objectToReadonlyMap, objectValuesToReadonlySet, objectValuesToSet, parseFloatSafe, parseIntSafe, parseSemanticVersion, removeLinesBetweenMarkers, removeLinesMatching, removeNonPrintableCharacters, removeWhitespace, repeat, setAdd, setHas, sortCaseInsensitive, sumArray, todo, trimPrefix, trimSuffix, truncateString, tupleEntries, tupleKeys };
|