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.
- package/LICENSE +1 -1
- package/README.md +3 -3
- package/dist/constants.d.cts +7 -0
- package/dist/constants.d.mts +7 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/functions/array.d.cts +83 -0
- package/dist/functions/array.d.mts +83 -0
- package/dist/functions/array.d.ts +83 -0
- package/dist/functions/array.d.ts.map +1 -0
- package/dist/functions/assert.d.cts +45 -0
- package/dist/functions/assert.d.mts +45 -0
- package/dist/functions/assert.d.ts +45 -0
- package/dist/functions/assert.d.ts.map +1 -0
- package/dist/functions/enums.d.cts +73 -0
- package/dist/functions/enums.d.mts +73 -0
- package/dist/functions/enums.d.ts +73 -0
- package/dist/functions/enums.d.ts.map +1 -0
- package/dist/functions/map.d.cts +24 -0
- package/dist/functions/map.d.mts +24 -0
- package/dist/functions/map.d.ts +24 -0
- package/dist/functions/map.d.ts.map +1 -0
- package/dist/functions/math.d.cts +13 -0
- package/dist/functions/math.d.mts +13 -0
- package/dist/functions/math.d.ts +13 -0
- package/dist/functions/math.d.ts.map +1 -0
- package/dist/functions/object.d.cts +60 -0
- package/dist/functions/object.d.mts +60 -0
- package/dist/functions/object.d.ts +60 -0
- package/dist/functions/object.d.ts.map +1 -0
- package/dist/functions/random.d.cts +23 -0
- package/dist/functions/random.d.mts +23 -0
- package/dist/functions/random.d.ts +23 -0
- package/dist/functions/random.d.ts.map +1 -0
- package/dist/functions/set.d.cts +61 -0
- package/dist/functions/set.d.mts +61 -0
- package/dist/functions/set.d.ts +61 -0
- package/dist/functions/set.d.ts.map +1 -0
- package/dist/functions/sort.d.cts +14 -0
- package/dist/functions/sort.d.mts +14 -0
- package/dist/functions/sort.d.ts +14 -0
- package/dist/functions/sort.d.ts.map +1 -0
- package/dist/functions/string.d.cts +122 -0
- package/dist/functions/string.d.mts +122 -0
- package/dist/functions/string.d.ts +122 -0
- package/dist/functions/string.d.ts.map +1 -0
- package/dist/functions/string.test.d.cts +2 -0
- package/dist/functions/string.test.d.mts +2 -0
- package/dist/functions/string.test.d.ts +2 -0
- package/dist/functions/string.test.d.ts.map +1 -0
- package/dist/functions/time.d.cts +20 -0
- package/dist/functions/time.d.mts +20 -0
- package/dist/functions/time.d.ts +20 -0
- package/dist/functions/time.d.ts.map +1 -0
- package/dist/functions/tuple.d.cts +27 -0
- package/dist/functions/tuple.d.mts +27 -0
- package/dist/functions/tuple.d.ts +27 -0
- package/dist/functions/tuple.d.ts.map +1 -0
- package/dist/functions/types.d.cts +13 -0
- package/dist/functions/types.d.mts +13 -0
- package/dist/functions/types.d.ts +13 -0
- package/dist/functions/types.d.ts.map +1 -0
- package/dist/functions/utils.d.cts +131 -0
- package/dist/functions/utils.d.mts +131 -0
- package/dist/functions/utils.d.ts +131 -0
- package/dist/functions/utils.d.ts.map +1 -0
- package/dist/functions/utils.test.d.cts +2 -0
- package/dist/functions/utils.test.d.mts +2 -0
- package/dist/functions/utils.test.d.ts +2 -0
- package/dist/functions/utils.test.d.ts.map +1 -0
- package/dist/index.cjs +122 -77
- package/dist/index.d.cts +30 -705
- package/dist/index.d.mts +30 -705
- package/dist/index.d.ts +30 -705
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +118 -78
- package/dist/types/AddSubtract.d.cts +23 -0
- package/dist/types/AddSubtract.d.mts +23 -0
- package/dist/types/AddSubtract.d.ts +23 -0
- package/dist/types/AddSubtract.d.ts.map +1 -0
- package/dist/types/CompositionTypeSatisfiesEnum.d.cts +35 -0
- package/dist/types/CompositionTypeSatisfiesEnum.d.mts +35 -0
- package/dist/types/CompositionTypeSatisfiesEnum.d.ts +35 -0
- package/dist/types/CompositionTypeSatisfiesEnum.d.ts.map +1 -0
- package/dist/types/ERange.d.cts +12 -0
- package/dist/types/ERange.d.mts +12 -0
- package/dist/types/ERange.d.ts +12 -0
- package/dist/types/ERange.d.ts.map +1 -0
- package/dist/types/IRange.d.cts +13 -0
- package/dist/types/IRange.d.mts +13 -0
- package/dist/types/IRange.d.ts +13 -0
- package/dist/types/IRange.d.ts.map +1 -0
- package/dist/types/Immutable.d.cts +17 -0
- package/dist/types/Immutable.d.mts +17 -0
- package/dist/types/Immutable.d.ts +17 -0
- package/dist/types/Immutable.d.ts.map +1 -0
- package/dist/types/NaturalNumbersLessThan.d.cts +8 -0
- package/dist/types/NaturalNumbersLessThan.d.mts +8 -0
- package/dist/types/NaturalNumbersLessThan.d.ts +8 -0
- package/dist/types/NaturalNumbersLessThan.d.ts.map +1 -0
- package/dist/types/NaturalNumbersLessThanOrEqualTo.d.cts +8 -0
- package/dist/types/NaturalNumbersLessThanOrEqualTo.d.mts +8 -0
- package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts +8 -0
- package/dist/types/NaturalNumbersLessThanOrEqualTo.d.ts.map +1 -0
- package/dist/types/ObjectValues.d.cts +3 -0
- package/dist/types/ObjectValues.d.mts +3 -0
- package/dist/types/ObjectValues.d.ts +3 -0
- package/dist/types/ObjectValues.d.ts.map +1 -0
- package/dist/types/ReadonlyMap.d.cts +9 -0
- package/dist/types/ReadonlyMap.d.mts +9 -0
- package/dist/types/ReadonlyMap.d.ts +9 -0
- package/dist/types/ReadonlyMap.d.ts.map +1 -0
- package/dist/types/ReadonlyRecord.d.cts +7 -0
- package/dist/types/ReadonlyRecord.d.mts +7 -0
- package/dist/types/ReadonlyRecord.d.ts +7 -0
- package/dist/types/ReadonlyRecord.d.ts.map +1 -0
- package/dist/types/ReadonlySet.d.cts +8 -0
- package/dist/types/ReadonlySet.d.mts +8 -0
- package/dist/types/ReadonlySet.d.ts +8 -0
- package/dist/types/ReadonlySet.d.ts.map +1 -0
- package/dist/types/Tuple.d.cts +10 -0
- package/dist/types/Tuple.d.mts +10 -0
- package/dist/types/Tuple.d.ts +10 -0
- package/dist/types/Tuple.d.ts.map +1 -0
- package/dist/types/WidenLiteral.d.cts +3 -0
- package/dist/types/WidenLiteral.d.mts +3 -0
- package/dist/types/WidenLiteral.d.ts +3 -0
- package/dist/types/WidenLiteral.d.ts.map +1 -0
- package/dist/types/Writeable.d.cts +9 -0
- package/dist/types/Writeable.d.mts +9 -0
- package/dist/types/Writeable.d.ts +9 -0
- package/dist/types/Writeable.d.ts.map +1 -0
- package/package.json +13 -2
- package/src/constants.ts +5 -0
- package/src/functions/array.ts +8 -2
- package/src/functions/assert.ts +86 -0
- package/src/functions/enums.ts +6 -0
- package/src/functions/map.ts +7 -45
- package/src/functions/math.ts +6 -0
- package/src/functions/object.ts +90 -3
- package/src/functions/random.ts +6 -0
- package/src/functions/set.ts +7 -0
- package/src/functions/sort.ts +6 -0
- package/src/functions/string.ts +25 -0
- package/src/functions/time.ts +26 -0
- package/src/functions/tuple.ts +6 -0
- package/src/functions/types.ts +6 -0
- package/src/functions/utils.test.ts +2 -8
- package/src/functions/utils.ts +7 -37
- package/src/index.ts +14 -12
- package/src/types/AddSubtract.ts +10 -2
- package/src/types/CompositionTypeSatisfiesEnum.ts +5 -3
- package/src/types/ObjectValues.ts +1 -0
- package/src/types/ReadonlyRecord.ts +5 -0
- package/src/types/Tuple.ts +1 -0
- package/src/types/WidenLiteral.ts +1 -0
package/src/functions/string.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions that have to do with strings.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import { parseIntSafe } from "./utils.js";
|
|
2
8
|
|
|
3
9
|
// When regular expressions are located at the root instead of inside the function, the functions
|
|
@@ -17,6 +23,7 @@ const KEBAB_CASE_REGEX =
|
|
|
17
23
|
const SEMANTIC_VERSION_REGEX = /^v*(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/;
|
|
18
24
|
const WHITESPACE_REGEX = /\s/g;
|
|
19
25
|
|
|
26
|
+
/** Helper function to capitalize the first letter of a string. */
|
|
20
27
|
export function capitalizeFirstLetter(string: string): string {
|
|
21
28
|
if (string === "") {
|
|
22
29
|
return string;
|
|
@@ -43,6 +50,12 @@ export function escapeHTMLCharacters(string: string): string {
|
|
|
43
50
|
.replaceAll("'", "'");
|
|
44
51
|
}
|
|
45
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Helper function to get the number of consecutive diacritics in a string.
|
|
55
|
+
*
|
|
56
|
+
* This is useful for sanitization purposes, since many subsequent diacritics can be a sign of a
|
|
57
|
+
* malicious string.
|
|
58
|
+
*/
|
|
46
59
|
export function getNumConsecutiveDiacritics(string: string): number {
|
|
47
60
|
// First, normalize with Normalization Form Canonical Decomposition (NFD) so that diacritics are
|
|
48
61
|
// separated from other characters:
|
|
@@ -66,6 +79,7 @@ export function getNumConsecutiveDiacritics(string: string): number {
|
|
|
66
79
|
return maxConsecutiveDiacritic;
|
|
67
80
|
}
|
|
68
81
|
|
|
82
|
+
/** Helper function to check if a string has one or more diacritics in it. */
|
|
69
83
|
export function hasDiacritic(string: string): boolean {
|
|
70
84
|
// First, normalize with Normalization Form Canonical Decomposition (NFD) so that diacritics are
|
|
71
85
|
// separated from other characters:
|
|
@@ -75,6 +89,11 @@ export function hasDiacritic(string: string): boolean {
|
|
|
75
89
|
return DIACRITIC_REGEX.test(normalizedString);
|
|
76
90
|
}
|
|
77
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Helper function to check if a string has one or more emoji in it.
|
|
94
|
+
*
|
|
95
|
+
* Under the hood, this uses the same regex check as the Zod library.
|
|
96
|
+
*/
|
|
78
97
|
export function hasEmoji(string: string): boolean {
|
|
79
98
|
return EMOJI_REGEX.test(string);
|
|
80
99
|
}
|
|
@@ -107,6 +126,7 @@ export function isSemanticVersion(versionString: string): boolean {
|
|
|
107
126
|
return match !== null;
|
|
108
127
|
}
|
|
109
128
|
|
|
129
|
+
/** Helper function to convert a string from kebab-case to camel-case. */
|
|
110
130
|
export function kebabCaseToCamelCase(string: string): string {
|
|
111
131
|
return string.replaceAll(/-./g, (match) => {
|
|
112
132
|
const firstLetterOfWord = match[1];
|
|
@@ -154,8 +174,13 @@ export function normalizeString(string: string): string {
|
|
|
154
174
|
*/
|
|
155
175
|
export function parseSemanticVersion(versionString: string):
|
|
156
176
|
| {
|
|
177
|
+
/** The first number inside of the semantic version. */
|
|
157
178
|
majorVersion: number;
|
|
179
|
+
|
|
180
|
+
/** The second number inside of the semantic version. */
|
|
158
181
|
minorVersion: number;
|
|
182
|
+
|
|
183
|
+
/** The third number inside of the semantic version. */
|
|
159
184
|
patchVersion: number;
|
|
160
185
|
}
|
|
161
186
|
| undefined {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions having to do with time.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Helper function to get the number of elapsed seconds since a starting time.
|
|
9
|
+
*
|
|
10
|
+
* This function always returns a whole number (using `Math.floor` on the result).
|
|
11
|
+
*
|
|
12
|
+
* For example:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* const startTime = Date.now();
|
|
16
|
+
* doSomething();
|
|
17
|
+
* const elapsedSeconds = getElapsedSeconds(startTime);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function getElapsedSeconds(startTime: number): number {
|
|
21
|
+
const endTime = Date.now();
|
|
22
|
+
const elapsedMilliseconds = endTime - startTime;
|
|
23
|
+
const elapsedSeconds = elapsedMilliseconds / 1000;
|
|
24
|
+
|
|
25
|
+
return Math.floor(elapsedSeconds);
|
|
26
|
+
}
|
package/src/functions/tuple.ts
CHANGED
package/src/functions/types.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { deepStrictEqual, strictEqual } from "node:assert";
|
|
2
2
|
import test, { describe } from "node:test";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
assertNotNull,
|
|
6
|
-
eRange,
|
|
7
|
-
iRange,
|
|
8
|
-
parseFloatSafe,
|
|
9
|
-
parseIntSafe,
|
|
10
|
-
} from "./utils.js";
|
|
3
|
+
import { assertDefined, assertNotNull } from "./assert.js";
|
|
4
|
+
import { eRange, iRange, parseFloatSafe, parseIntSafe } from "./utils.js";
|
|
11
5
|
|
|
12
6
|
describe("assertsDefined", () => {
|
|
13
7
|
/** We use a value of null since it is the least arbitrary non-undefined value. */
|
package/src/functions/utils.ts
CHANGED
|
@@ -1,45 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions that do not belong to any category in particular.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
// When regexes are located at the root instead of inside the function, the functions are tested to
|
|
2
8
|
// perform 11% faster.
|
|
3
9
|
|
|
4
10
|
const FLOAT_REGEX = /^-?\d*\.?\d+$/;
|
|
5
11
|
const INTEGER_REGEX = /^-?\d+$/;
|
|
6
12
|
|
|
7
|
-
/**
|
|
8
|
-
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
9
|
-
*
|
|
10
|
-
* This is useful to have TypeScript narrow a `T | undefined` value to `T` in a concise way.
|
|
11
|
-
*/
|
|
12
|
-
export function assertDefined<T>(
|
|
13
|
-
value: T,
|
|
14
|
-
...[msg]: [undefined] extends [T]
|
|
15
|
-
? [string]
|
|
16
|
-
: [
|
|
17
|
-
"The assertion is useless because the provided value does not contain undefined.",
|
|
18
|
-
]
|
|
19
|
-
): asserts value is Exclude<T, undefined> {
|
|
20
|
-
if (value === undefined) {
|
|
21
|
-
throw new TypeError(msg);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Helper function to throw an error if the provided value is equal to `null`.
|
|
27
|
-
*
|
|
28
|
-
* This is useful to have TypeScript narrow a `T | null` value to `T` in a concise way.
|
|
29
|
-
*/
|
|
30
|
-
export function assertNotNull<T>(
|
|
31
|
-
value: T,
|
|
32
|
-
...[msg]: [null] extends [T]
|
|
33
|
-
? [string]
|
|
34
|
-
: [
|
|
35
|
-
"The assertion is useless because the provided value does not contain null.",
|
|
36
|
-
]
|
|
37
|
-
): asserts value is Exclude<T, null> {
|
|
38
|
-
if (value === null) {
|
|
39
|
-
throw new TypeError(msg);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
13
|
/**
|
|
44
14
|
* Helper function to get an iterator of integers with the specified range, inclusive on the lower
|
|
45
15
|
* end and exclusive on the high end. (The "e" in the function name stands for exclusive.) Thus,
|
|
@@ -102,7 +72,7 @@ export function* eRange(
|
|
|
102
72
|
* If you want an array instead of an iterator, use the spread operator like this:
|
|
103
73
|
*
|
|
104
74
|
* ```ts
|
|
105
|
-
* const myArray = [...
|
|
75
|
+
* const myArray = [...iRange(1, 3)];
|
|
106
76
|
* ```
|
|
107
77
|
*
|
|
108
78
|
* @param start The integer to start at.
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./constants.js";
|
|
2
2
|
export * from "./functions/array.js";
|
|
3
|
+
export * from "./functions/assert.js";
|
|
3
4
|
export * from "./functions/enums.js";
|
|
4
5
|
export * from "./functions/map.js";
|
|
5
6
|
export * from "./functions/math.js";
|
|
@@ -8,20 +9,21 @@ export * from "./functions/random.js";
|
|
|
8
9
|
export * from "./functions/set.js";
|
|
9
10
|
export * from "./functions/sort.js";
|
|
10
11
|
export * from "./functions/string.js";
|
|
12
|
+
export * from "./functions/time.js";
|
|
11
13
|
export * from "./functions/tuple.js";
|
|
12
14
|
export * from "./functions/types.js";
|
|
13
15
|
export * from "./functions/utils.js";
|
|
14
|
-
export * from "./types/AddSubtract.js";
|
|
15
|
-
export * from "./types/CompositionTypeSatisfiesEnum.js";
|
|
16
|
-
export * from "./types/ERange.js";
|
|
17
|
-
export * from "./types/Immutable.js";
|
|
18
|
-
export * from "./types/IRange.js";
|
|
19
|
-
export * from "./types/NaturalNumbersLessThan.js";
|
|
20
|
-
export * from "./types/NaturalNumbersLessThanOrEqualTo.js";
|
|
21
|
-
export * from "./types/ObjectValues.js";
|
|
16
|
+
export type * from "./types/AddSubtract.js";
|
|
17
|
+
export type * from "./types/CompositionTypeSatisfiesEnum.js";
|
|
18
|
+
export type * from "./types/ERange.js";
|
|
19
|
+
export type * from "./types/Immutable.js";
|
|
20
|
+
export type * from "./types/IRange.js";
|
|
21
|
+
export type * from "./types/NaturalNumbersLessThan.js";
|
|
22
|
+
export type * from "./types/NaturalNumbersLessThanOrEqualTo.js";
|
|
23
|
+
export type * from "./types/ObjectValues.js";
|
|
22
24
|
export * from "./types/ReadonlyMap.js";
|
|
23
|
-
export * from "./types/ReadonlyRecord.js";
|
|
25
|
+
export type * from "./types/ReadonlyRecord.js";
|
|
24
26
|
export * from "./types/ReadonlySet.js";
|
|
25
|
-
export * from "./types/Tuple.js";
|
|
26
|
-
export * from "./types/WidenLiteral.js";
|
|
27
|
-
export * from "./types/Writeable.js";
|
|
27
|
+
export type * from "./types/Tuple.js";
|
|
28
|
+
export type * from "./types/WidenLiteral.js";
|
|
29
|
+
export type * from "./types/Writeable.js";
|
package/src/types/AddSubtract.ts
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to add two other types.
|
|
3
|
+
*
|
|
4
|
+
* From: https://gist.github.com/ryandabler/8b4ff4f36aed47bc09acc03174638468
|
|
5
|
+
*/
|
|
2
6
|
export type Add<A extends number, B extends number> = Length<
|
|
3
7
|
[...BuildTuple<A>, ...BuildTuple<B>]
|
|
4
8
|
>;
|
|
5
9
|
|
|
6
|
-
/**
|
|
10
|
+
/**
|
|
11
|
+
* Helper type to subtract two other types.
|
|
12
|
+
*
|
|
13
|
+
* From: https://gist.github.com/ryandabler/8b4ff4f36aed47bc09acc03174638468
|
|
14
|
+
*/
|
|
7
15
|
export type Subtract<A extends number, B extends number> = A extends A
|
|
8
16
|
? BuildTuple<A> extends [...infer U, ...BuildTuple<B>]
|
|
9
17
|
? Length<U>
|
|
@@ -29,9 +29,11 @@
|
|
|
29
29
|
* entry for `BazObjective`.
|
|
30
30
|
*/
|
|
31
31
|
export type CompositionTypeSatisfiesEnum<
|
|
32
|
-
T extends {
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
T extends {
|
|
33
|
+
/** The type of the discriminated union. */
|
|
34
|
+
type: unknown;
|
|
35
|
+
},
|
|
36
|
+
_Enum extends T["type"],
|
|
35
37
|
> = unknown;
|
|
36
38
|
|
|
37
39
|
// -----
|
package/src/types/Tuple.ts
CHANGED