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.
- 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 +22 -0
- package/dist/functions/assert.d.mts +22 -0
- package/dist/functions/assert.d.ts +22 -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 +52 -0
- package/dist/functions/map.d.mts +52 -0
- package/dist/functions/map.d.ts +52 -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 +14 -0
- package/dist/functions/object.d.mts +14 -0
- package/dist/functions/object.d.ts +14 -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/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 +66 -65
- package/dist/index.d.cts +29 -705
- package/dist/index.d.mts +29 -705
- package/dist/index.d.ts +29 -705
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +66 -65
- 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 +12 -2
- package/src/constants.ts +5 -0
- package/src/functions/array.ts +8 -2
- package/src/functions/assert.ts +41 -0
- package/src/functions/enums.ts +6 -0
- package/src/functions/map.ts +7 -0
- package/src/functions/math.ts +6 -0
- package/src/functions/object.ts +8 -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/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 +13 -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
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to specify that a record should be read-only.
|
|
3
|
+
*
|
|
4
|
+
* This is the same thing as `Readonly<Record<K, V>>`.
|
|
5
|
+
*/
|
|
6
|
+
export type ReadonlyRecord<K extends string | number | symbol, V> = Readonly<Record<K, V>>;
|
|
7
|
+
//# sourceMappingURL=ReadonlyRecord.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReadonlyRecord.d.ts","sourceRoot":"","sources":["../../src/types/ReadonlyRecord.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,IAAI,QAAQ,CAC1E,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CACb,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
interface ReadonlySetConstructor {
|
|
2
|
+
new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
|
|
3
|
+
readonly prototype: ReadonlySet<any>;
|
|
4
|
+
}
|
|
5
|
+
/** An alias for the `Set` constructor that returns a read-only set. */
|
|
6
|
+
export declare const ReadonlySet: ReadonlySetConstructor;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=ReadonlySet.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
interface ReadonlySetConstructor {
|
|
2
|
+
new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
|
|
3
|
+
readonly prototype: ReadonlySet<any>;
|
|
4
|
+
}
|
|
5
|
+
/** An alias for the `Set` constructor that returns a read-only set. */
|
|
6
|
+
export declare const ReadonlySet: ReadonlySetConstructor;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=ReadonlySet.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
interface ReadonlySetConstructor {
|
|
2
|
+
new <T = any>(values?: readonly T[] | Iterable<T> | null): ReadonlySet<T>;
|
|
3
|
+
readonly prototype: ReadonlySet<any>;
|
|
4
|
+
}
|
|
5
|
+
/** An alias for the `Set` constructor that returns a read-only set. */
|
|
6
|
+
export declare const ReadonlySet: ReadonlySetConstructor;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=ReadonlySet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ReadonlySet.d.ts","sourceRoot":"","sources":["../../src/types/ReadonlySet.ts"],"names":[],"mappings":"AAEA,UAAU,sBAAsB;IAC9B,KAAK,CAAC,GAAG,GAAG,EAAE,MAAM,CAAC,EAAE,SAAS,CAAC,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;IAC1E,QAAQ,CAAC,SAAS,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;CACtC;AAED,uEAAuE;AACvE,eAAO,MAAM,WAAW,EAAU,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to represent a tuple of length N.
|
|
3
|
+
*
|
|
4
|
+
* From:
|
|
5
|
+
* https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
|
|
6
|
+
*/
|
|
7
|
+
export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
|
|
8
|
+
type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=Tuple.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to represent a tuple of length N.
|
|
3
|
+
*
|
|
4
|
+
* From:
|
|
5
|
+
* https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
|
|
6
|
+
*/
|
|
7
|
+
export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
|
|
8
|
+
type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=Tuple.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper type to represent a tuple of length N.
|
|
3
|
+
*
|
|
4
|
+
* From:
|
|
5
|
+
* https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type/52490977#52490977
|
|
6
|
+
*/
|
|
7
|
+
export type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _TupleOf<T, N, []> : never;
|
|
8
|
+
type _TupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _TupleOf<T, N, [T, ...R]>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=Tuple.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Tuple.d.ts","sourceRoot":"","sources":["../../src/types/Tuple.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,CAAC,GAChD,MAAM,SAAS,CAAC,GACd,CAAC,EAAE,GACH,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,GACpB,KAAK,CAAC;AAEV,KAAK,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,EAAE,IAAI,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAC3E,CAAC,GACD,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Helper type used to widen a const array to the corresponding base type. */
|
|
2
|
+
export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
|
|
3
|
+
//# sourceMappingURL=WidenLiteral.d.ts.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Helper type used to widen a const array to the corresponding base type. */
|
|
2
|
+
export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
|
|
3
|
+
//# sourceMappingURL=WidenLiteral.d.ts.map
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** Helper type used to widen a const array to the corresponding base type. */
|
|
2
|
+
export type WidenLiteral<T> = T extends string ? string : T extends number ? number : T extends boolean ? boolean : T extends bigint ? bigint : T extends symbol ? symbol : T;
|
|
3
|
+
//# sourceMappingURL=WidenLiteral.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WidenLiteral.d.ts","sourceRoot":"","sources":["../../src/types/WidenLiteral.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GAC1C,MAAM,GACN,CAAC,SAAS,MAAM,GACd,MAAM,GACN,CAAC,SAAS,OAAO,GACf,OAAO,GACP,CAAC,SAAS,MAAM,GACd,MAAM,GACN,CAAC,SAAS,MAAM,GACd,MAAM,GACN,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Writeable.d.ts","sourceRoot":"","sources":["../../src/types/Writeable.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "complete-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Helper functions for TypeScript projects.",
|
|
5
5
|
"keywords": [],
|
|
6
|
-
"homepage": "https://complete-
|
|
6
|
+
"homepage": "https://complete-ts.github.io/",
|
|
7
7
|
"bugs": {
|
|
8
8
|
"url": "https://github.com/complete-ts/complete/issues"
|
|
9
9
|
},
|
|
@@ -30,7 +30,17 @@
|
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsx ./scripts/build.ts",
|
|
33
|
+
"docs": "typedoc",
|
|
33
34
|
"lint": "tsx ./scripts/lint.ts",
|
|
34
35
|
"test": "glob \"./src/**/*.test.ts\" --cmd=\"node --import tsx --test --test-reporter spec\""
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^22.7.7",
|
|
39
|
+
"complete-node": "^1.7.4",
|
|
40
|
+
"eslint-plugin-sort-exports": "^0.9.1",
|
|
41
|
+
"glob": "^11.0.0",
|
|
42
|
+
"typescript": "^5.6.3",
|
|
43
|
+
"typescript-eslint": "^8.10.0",
|
|
44
|
+
"unbuild": "^2.0.0"
|
|
35
45
|
}
|
|
36
46
|
}
|
package/src/constants.ts
CHANGED
package/src/functions/array.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions that have to do with arrays.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import type { WidenLiteral } from "../index.js";
|
|
2
8
|
import { ReadonlySet } from "../types/ReadonlySet.js";
|
|
9
|
+
import { assertDefined } from "./assert.js";
|
|
3
10
|
import { getRandomInt } from "./random.js";
|
|
4
|
-
import { assertDefined } from "./utils.js";
|
|
5
11
|
|
|
6
12
|
/**
|
|
7
13
|
* Helper function to copy a two-dimensional array. Note that the sub-arrays will only be shallow
|
|
@@ -84,7 +90,7 @@ export function arrayRemoveInPlace<T>(
|
|
|
84
90
|
|
|
85
91
|
for (const element of elementsToRemove) {
|
|
86
92
|
const index = array.indexOf(element);
|
|
87
|
-
if (index
|
|
93
|
+
if (index !== -1) {
|
|
88
94
|
const removedElement = array.splice(index, 1);
|
|
89
95
|
removedElements.push(...removedElement);
|
|
90
96
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions that have to do with asserting.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
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
|
+
}
|
package/src/functions/enums.ts
CHANGED
package/src/functions/map.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions that have to do with
|
|
3
|
+
* [maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map).
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
* Helper function to get the values in a `Map` that match an arbitrary condition. Similar to the
|
|
3
10
|
* `Array.map` method, but works for maps.
|
package/src/functions/math.ts
CHANGED
package/src/functions/object.ts
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Helper
|
|
3
|
-
* `Array.map` method, but works for objects.
|
|
2
|
+
* Helper functions that have to do with objects.
|
|
4
3
|
*
|
|
5
|
-
*
|
|
4
|
+
* @module
|
|
6
5
|
*/
|
|
7
6
|
|
|
8
7
|
import type { ReadonlyRecord } from "../types/ReadonlyRecord.js";
|
|
9
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Helper function to get the values in an object that match an arbitrary condition. Similar to the
|
|
11
|
+
* `Array.map` method, but works for objects.
|
|
12
|
+
*
|
|
13
|
+
* This is efficient such that it avoids converting the object values into an array.
|
|
14
|
+
*/
|
|
10
15
|
// eslint-disable-next-line complete/no-mutable-return
|
|
11
16
|
export function objectFilter<K extends string | number | symbol, V>(
|
|
12
17
|
object: ReadonlyRecord<K, V>,
|
package/src/functions/random.ts
CHANGED
package/src/functions/set.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions that have to do with
|
|
3
|
+
* [sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
* Helper function to add all of the values in one set to another set. The first set passed will be
|
|
3
10
|
* modified in place.
|
package/src/functions/sort.ts
CHANGED
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 {
|
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";
|
|
@@ -11,17 +12,17 @@ export * from "./functions/string.js";
|
|
|
11
12
|
export * from "./functions/tuple.js";
|
|
12
13
|
export * from "./functions/types.js";
|
|
13
14
|
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";
|
|
15
|
+
export type * from "./types/AddSubtract.js";
|
|
16
|
+
export type * from "./types/CompositionTypeSatisfiesEnum.js";
|
|
17
|
+
export type * from "./types/ERange.js";
|
|
18
|
+
export type * from "./types/Immutable.js";
|
|
19
|
+
export type * from "./types/IRange.js";
|
|
20
|
+
export type * from "./types/NaturalNumbersLessThan.js";
|
|
21
|
+
export type * from "./types/NaturalNumbersLessThanOrEqualTo.js";
|
|
22
|
+
export type * from "./types/ObjectValues.js";
|
|
22
23
|
export * from "./types/ReadonlyMap.js";
|
|
23
|
-
export * from "./types/ReadonlyRecord.js";
|
|
24
|
+
export type * from "./types/ReadonlyRecord.js";
|
|
24
25
|
export * from "./types/ReadonlySet.js";
|
|
25
|
-
export * from "./types/Tuple.js";
|
|
26
|
-
export * from "./types/WidenLiteral.js";
|
|
27
|
-
export * from "./types/Writeable.js";
|
|
26
|
+
export type * from "./types/Tuple.js";
|
|
27
|
+
export type * from "./types/WidenLiteral.js";
|
|
28
|
+
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