complete-common 2.16.0 โ 2.16.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/dist/functions/assert.d.cts +27 -9
- package/dist/functions/assert.d.mts +27 -9
- package/dist/functions/assert.d.ts +27 -9
- package/dist/functions/assert.d.ts.map +1 -1
- package/dist/functions/string.d.cts +2 -2
- package/dist/functions/string.d.mts +2 -2
- package/dist/functions/string.d.ts +2 -2
- package/dist/functions/string.d.ts.map +1 -1
- package/dist/index.cjs +24 -15
- package/dist/index.mjs +24 -15
- package/package.json +1 -1
- package/src/functions/assert.ts +88 -44
- package/src/functions/string.test.ts +24 -0
- package/src/functions/string.ts +9 -5
|
@@ -5,29 +5,41 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { TranspiledEnum } from "../types/TranspiledEnum.js";
|
|
7
7
|
/** Helper function to throw an error if the provided value is not an array. */
|
|
8
|
-
export declare function assertArray(value:
|
|
8
|
+
export declare function assertArray<T>(value: T, ...[msg]: [T] extends [readonly unknown[]] ? [
|
|
9
|
+
"The assertion is useless because the provided value is already an array."
|
|
10
|
+
] : [string]): asserts value is T & unknown[];
|
|
9
11
|
/**
|
|
10
12
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
11
13
|
* a boolean.
|
|
12
14
|
*/
|
|
13
|
-
export declare function assertArrayBoolean(value:
|
|
15
|
+
export declare function assertArrayBoolean<T>(value: T, ...[msg]: [T] extends [readonly boolean[]] ? [
|
|
16
|
+
"The assertion is useless because the provided value is already a boolean array."
|
|
17
|
+
] : [string]): asserts value is T & boolean[];
|
|
14
18
|
/**
|
|
15
19
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
16
20
|
* a number.
|
|
17
21
|
*/
|
|
18
|
-
export declare function assertArrayNumber(value:
|
|
22
|
+
export declare function assertArrayNumber<T>(value: T, ...[msg]: [T] extends [readonly number[]] ? [
|
|
23
|
+
"The assertion is useless because the provided value is already a number array."
|
|
24
|
+
] : [string]): asserts value is T & number[];
|
|
19
25
|
/**
|
|
20
26
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
21
27
|
* an object (i.e. a TypeScript record).
|
|
22
28
|
*/
|
|
23
|
-
export declare function assertArrayObject(value:
|
|
29
|
+
export declare function assertArrayObject<T>(value: T, ...[msg]: [T] extends [ReadonlyArray<Record<string, unknown>>] ? [
|
|
30
|
+
"The assertion is useless because the provided value is already an object array."
|
|
31
|
+
] : [string]): asserts value is T & Array<Record<string, unknown>>;
|
|
24
32
|
/**
|
|
25
33
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
26
34
|
* a string.
|
|
27
35
|
*/
|
|
28
|
-
export declare function assertArrayString(value:
|
|
36
|
+
export declare function assertArrayString<T>(value: T, ...[msg]: [T] extends [readonly string[]] ? [
|
|
37
|
+
"The assertion is useless because the provided value is already a string array."
|
|
38
|
+
] : [string]): asserts value is T & string[];
|
|
29
39
|
/** Helper function to throw an error if the provided value is not a boolean. */
|
|
30
|
-
export declare function assertBoolean(value:
|
|
40
|
+
export declare function assertBoolean<T>(value: T, ...[msg]: [T] extends [boolean] ? [
|
|
41
|
+
"The assertion is useless because the provided value is already a boolean."
|
|
42
|
+
] : [string]): asserts value is T & boolean;
|
|
31
43
|
/**
|
|
32
44
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
33
45
|
*
|
|
@@ -64,7 +76,9 @@ export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T]
|
|
|
64
76
|
"The assertion is useless because the provided value does not contain null."
|
|
65
77
|
]): asserts value is Exclude<T, null>;
|
|
66
78
|
/** Helper function to throw an error if the provided value is not a number. */
|
|
67
|
-
export declare function assertNumber(value:
|
|
79
|
+
export declare function assertNumber<T>(value: T, ...[msg]: [T] extends [number] ? [
|
|
80
|
+
"The assertion is useless because the provided value is already a number."
|
|
81
|
+
] : [string]): asserts value is T & number;
|
|
68
82
|
/**
|
|
69
83
|
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
70
84
|
* record).
|
|
@@ -74,9 +88,13 @@ export declare function assertNumber(value: unknown, msg: string): asserts value
|
|
|
74
88
|
*
|
|
75
89
|
* Under the hood, this function uses the `isObject` helper function.
|
|
76
90
|
*/
|
|
77
|
-
export declare function assertObject(value:
|
|
91
|
+
export declare function assertObject<T>(value: T, ...[msg]: [T] extends [Record<string, unknown>] ? [
|
|
92
|
+
"The assertion is useless because the provided value is already an object."
|
|
93
|
+
] : [string]): asserts value is T & Record<string, unknown>;
|
|
78
94
|
/** Helper function to throw an error if the provided value is not a string. */
|
|
79
|
-
export declare function assertString(value:
|
|
95
|
+
export declare function assertString<T>(value: T, ...[msg]: [T] extends [string] ? [
|
|
96
|
+
"The assertion is useless because the provided value is already a string."
|
|
97
|
+
] : [string]): asserts value is T & string;
|
|
80
98
|
/** Helper function to throw an error if the provided value is not a string or an empty string. */
|
|
81
99
|
export declare function assertStringNotEmpty(value: unknown, msg: string): asserts value is string;
|
|
82
100
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -5,29 +5,41 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { TranspiledEnum } from "../types/TranspiledEnum.js";
|
|
7
7
|
/** Helper function to throw an error if the provided value is not an array. */
|
|
8
|
-
export declare function assertArray(value:
|
|
8
|
+
export declare function assertArray<T>(value: T, ...[msg]: [T] extends [readonly unknown[]] ? [
|
|
9
|
+
"The assertion is useless because the provided value is already an array."
|
|
10
|
+
] : [string]): asserts value is T & unknown[];
|
|
9
11
|
/**
|
|
10
12
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
11
13
|
* a boolean.
|
|
12
14
|
*/
|
|
13
|
-
export declare function assertArrayBoolean(value:
|
|
15
|
+
export declare function assertArrayBoolean<T>(value: T, ...[msg]: [T] extends [readonly boolean[]] ? [
|
|
16
|
+
"The assertion is useless because the provided value is already a boolean array."
|
|
17
|
+
] : [string]): asserts value is T & boolean[];
|
|
14
18
|
/**
|
|
15
19
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
16
20
|
* a number.
|
|
17
21
|
*/
|
|
18
|
-
export declare function assertArrayNumber(value:
|
|
22
|
+
export declare function assertArrayNumber<T>(value: T, ...[msg]: [T] extends [readonly number[]] ? [
|
|
23
|
+
"The assertion is useless because the provided value is already a number array."
|
|
24
|
+
] : [string]): asserts value is T & number[];
|
|
19
25
|
/**
|
|
20
26
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
21
27
|
* an object (i.e. a TypeScript record).
|
|
22
28
|
*/
|
|
23
|
-
export declare function assertArrayObject(value:
|
|
29
|
+
export declare function assertArrayObject<T>(value: T, ...[msg]: [T] extends [ReadonlyArray<Record<string, unknown>>] ? [
|
|
30
|
+
"The assertion is useless because the provided value is already an object array."
|
|
31
|
+
] : [string]): asserts value is T & Array<Record<string, unknown>>;
|
|
24
32
|
/**
|
|
25
33
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
26
34
|
* a string.
|
|
27
35
|
*/
|
|
28
|
-
export declare function assertArrayString(value:
|
|
36
|
+
export declare function assertArrayString<T>(value: T, ...[msg]: [T] extends [readonly string[]] ? [
|
|
37
|
+
"The assertion is useless because the provided value is already a string array."
|
|
38
|
+
] : [string]): asserts value is T & string[];
|
|
29
39
|
/** Helper function to throw an error if the provided value is not a boolean. */
|
|
30
|
-
export declare function assertBoolean(value:
|
|
40
|
+
export declare function assertBoolean<T>(value: T, ...[msg]: [T] extends [boolean] ? [
|
|
41
|
+
"The assertion is useless because the provided value is already a boolean."
|
|
42
|
+
] : [string]): asserts value is T & boolean;
|
|
31
43
|
/**
|
|
32
44
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
33
45
|
*
|
|
@@ -64,7 +76,9 @@ export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T]
|
|
|
64
76
|
"The assertion is useless because the provided value does not contain null."
|
|
65
77
|
]): asserts value is Exclude<T, null>;
|
|
66
78
|
/** Helper function to throw an error if the provided value is not a number. */
|
|
67
|
-
export declare function assertNumber(value:
|
|
79
|
+
export declare function assertNumber<T>(value: T, ...[msg]: [T] extends [number] ? [
|
|
80
|
+
"The assertion is useless because the provided value is already a number."
|
|
81
|
+
] : [string]): asserts value is T & number;
|
|
68
82
|
/**
|
|
69
83
|
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
70
84
|
* record).
|
|
@@ -74,9 +88,13 @@ export declare function assertNumber(value: unknown, msg: string): asserts value
|
|
|
74
88
|
*
|
|
75
89
|
* Under the hood, this function uses the `isObject` helper function.
|
|
76
90
|
*/
|
|
77
|
-
export declare function assertObject(value:
|
|
91
|
+
export declare function assertObject<T>(value: T, ...[msg]: [T] extends [Record<string, unknown>] ? [
|
|
92
|
+
"The assertion is useless because the provided value is already an object."
|
|
93
|
+
] : [string]): asserts value is T & Record<string, unknown>;
|
|
78
94
|
/** Helper function to throw an error if the provided value is not a string. */
|
|
79
|
-
export declare function assertString(value:
|
|
95
|
+
export declare function assertString<T>(value: T, ...[msg]: [T] extends [string] ? [
|
|
96
|
+
"The assertion is useless because the provided value is already a string."
|
|
97
|
+
] : [string]): asserts value is T & string;
|
|
80
98
|
/** Helper function to throw an error if the provided value is not a string or an empty string. */
|
|
81
99
|
export declare function assertStringNotEmpty(value: unknown, msg: string): asserts value is string;
|
|
82
100
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -5,29 +5,41 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { TranspiledEnum } from "../types/TranspiledEnum.js";
|
|
7
7
|
/** Helper function to throw an error if the provided value is not an array. */
|
|
8
|
-
export declare function assertArray(value:
|
|
8
|
+
export declare function assertArray<T>(value: T, ...[msg]: [T] extends [readonly unknown[]] ? [
|
|
9
|
+
"The assertion is useless because the provided value is already an array."
|
|
10
|
+
] : [string]): asserts value is T & unknown[];
|
|
9
11
|
/**
|
|
10
12
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
11
13
|
* a boolean.
|
|
12
14
|
*/
|
|
13
|
-
export declare function assertArrayBoolean(value:
|
|
15
|
+
export declare function assertArrayBoolean<T>(value: T, ...[msg]: [T] extends [readonly boolean[]] ? [
|
|
16
|
+
"The assertion is useless because the provided value is already a boolean array."
|
|
17
|
+
] : [string]): asserts value is T & boolean[];
|
|
14
18
|
/**
|
|
15
19
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
16
20
|
* a number.
|
|
17
21
|
*/
|
|
18
|
-
export declare function assertArrayNumber(value:
|
|
22
|
+
export declare function assertArrayNumber<T>(value: T, ...[msg]: [T] extends [readonly number[]] ? [
|
|
23
|
+
"The assertion is useless because the provided value is already a number array."
|
|
24
|
+
] : [string]): asserts value is T & number[];
|
|
19
25
|
/**
|
|
20
26
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
21
27
|
* an object (i.e. a TypeScript record).
|
|
22
28
|
*/
|
|
23
|
-
export declare function assertArrayObject(value:
|
|
29
|
+
export declare function assertArrayObject<T>(value: T, ...[msg]: [T] extends [ReadonlyArray<Record<string, unknown>>] ? [
|
|
30
|
+
"The assertion is useless because the provided value is already an object array."
|
|
31
|
+
] : [string]): asserts value is T & Array<Record<string, unknown>>;
|
|
24
32
|
/**
|
|
25
33
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
26
34
|
* a string.
|
|
27
35
|
*/
|
|
28
|
-
export declare function assertArrayString(value:
|
|
36
|
+
export declare function assertArrayString<T>(value: T, ...[msg]: [T] extends [readonly string[]] ? [
|
|
37
|
+
"The assertion is useless because the provided value is already a string array."
|
|
38
|
+
] : [string]): asserts value is T & string[];
|
|
29
39
|
/** Helper function to throw an error if the provided value is not a boolean. */
|
|
30
|
-
export declare function assertBoolean(value:
|
|
40
|
+
export declare function assertBoolean<T>(value: T, ...[msg]: [T] extends [boolean] ? [
|
|
41
|
+
"The assertion is useless because the provided value is already a boolean."
|
|
42
|
+
] : [string]): asserts value is T & boolean;
|
|
31
43
|
/**
|
|
32
44
|
* Helper function to throw an error if the provided value is equal to `undefined`.
|
|
33
45
|
*
|
|
@@ -64,7 +76,9 @@ export declare function assertNotNull<T>(value: T, ...[msg]: [null] extends [T]
|
|
|
64
76
|
"The assertion is useless because the provided value does not contain null."
|
|
65
77
|
]): asserts value is Exclude<T, null>;
|
|
66
78
|
/** Helper function to throw an error if the provided value is not a number. */
|
|
67
|
-
export declare function assertNumber(value:
|
|
79
|
+
export declare function assertNumber<T>(value: T, ...[msg]: [T] extends [number] ? [
|
|
80
|
+
"The assertion is useless because the provided value is already a number."
|
|
81
|
+
] : [string]): asserts value is T & number;
|
|
68
82
|
/**
|
|
69
83
|
* Helper function to throw an error if the provided value is not an object (i.e. a TypeScript
|
|
70
84
|
* record).
|
|
@@ -74,9 +88,13 @@ export declare function assertNumber(value: unknown, msg: string): asserts value
|
|
|
74
88
|
*
|
|
75
89
|
* Under the hood, this function uses the `isObject` helper function.
|
|
76
90
|
*/
|
|
77
|
-
export declare function assertObject(value:
|
|
91
|
+
export declare function assertObject<T>(value: T, ...[msg]: [T] extends [Record<string, unknown>] ? [
|
|
92
|
+
"The assertion is useless because the provided value is already an object."
|
|
93
|
+
] : [string]): asserts value is T & Record<string, unknown>;
|
|
78
94
|
/** Helper function to throw an error if the provided value is not a string. */
|
|
79
|
-
export declare function assertString(value:
|
|
95
|
+
export declare function assertString<T>(value: T, ...[msg]: [T] extends [string] ? [
|
|
96
|
+
"The assertion is useless because the provided value is already a string."
|
|
97
|
+
] : [string]): asserts value is T & string;
|
|
80
98
|
/** Helper function to throw an error if the provided value is not a string or an empty string. */
|
|
81
99
|
export declare function assertStringNotEmpty(value: unknown, msg: string): asserts value is string;
|
|
82
100
|
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../src/functions/assert.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAIjE,+EAA+E;AAC/E,wBAAgB,WAAW,
|
|
1
|
+
{"version":3,"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,MAAM,CAAC,GAC1B;IACE,0EAA0E;CAC3E,GACD,CAAC,MAAM,CAAC,GACX,OAAO,CAAC,KAAK,IAAI,CAAC,GAAG,MAAM,CAI7B;AAED,kGAAkG;AAClG,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,KAAK,IAAI,MAAM,CAMzB"}
|
|
@@ -87,7 +87,7 @@ export declare function parseSemanticVersion(versionString: string): {
|
|
|
87
87
|
*
|
|
88
88
|
* For example, by using a marker of "@foo":
|
|
89
89
|
*
|
|
90
|
-
* ```
|
|
90
|
+
* ```txt
|
|
91
91
|
* line1
|
|
92
92
|
* # @foo-start
|
|
93
93
|
* line2
|
|
@@ -98,7 +98,7 @@ export declare function parseSemanticVersion(versionString: string): {
|
|
|
98
98
|
*
|
|
99
99
|
* Would return:
|
|
100
100
|
*
|
|
101
|
-
* ```
|
|
101
|
+
* ```txt
|
|
102
102
|
* line1
|
|
103
103
|
* line4
|
|
104
104
|
* ```
|
|
@@ -87,7 +87,7 @@ export declare function parseSemanticVersion(versionString: string): {
|
|
|
87
87
|
*
|
|
88
88
|
* For example, by using a marker of "@foo":
|
|
89
89
|
*
|
|
90
|
-
* ```
|
|
90
|
+
* ```txt
|
|
91
91
|
* line1
|
|
92
92
|
* # @foo-start
|
|
93
93
|
* line2
|
|
@@ -98,7 +98,7 @@ export declare function parseSemanticVersion(versionString: string): {
|
|
|
98
98
|
*
|
|
99
99
|
* Would return:
|
|
100
100
|
*
|
|
101
|
-
* ```
|
|
101
|
+
* ```txt
|
|
102
102
|
* line1
|
|
103
103
|
* line4
|
|
104
104
|
* ```
|
|
@@ -87,7 +87,7 @@ export declare function parseSemanticVersion(versionString: string): {
|
|
|
87
87
|
*
|
|
88
88
|
* For example, by using a marker of "@foo":
|
|
89
89
|
*
|
|
90
|
-
* ```
|
|
90
|
+
* ```txt
|
|
91
91
|
* line1
|
|
92
92
|
* # @foo-start
|
|
93
93
|
* line2
|
|
@@ -98,7 +98,7 @@ export declare function parseSemanticVersion(versionString: string): {
|
|
|
98
98
|
*
|
|
99
99
|
* Would return:
|
|
100
100
|
*
|
|
101
|
-
* ```
|
|
101
|
+
* ```txt
|
|
102
102
|
* line1
|
|
103
103
|
* line4
|
|
104
104
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/functions/string.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/functions/string.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA8BH,kEAAkE;AAClE,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAU5D;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAO3D;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAqBlE;AAED,6EAA6E;AAC7E,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAOpD;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED,0FAA0F;AAC1F,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAErD;AAED,+EAA+E;AAC/E,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE5C;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEhE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED,+FAA+F;AAC/F,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEnD;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,aAA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MAAM,EAAE,MAAM,GAAG,MAAM,CAOjE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAMxE"}
|
package/dist/index.cjs
CHANGED
|
@@ -36,36 +36,44 @@ function isObject(variable) {
|
|
|
36
36
|
return typeof variable === "object" && variable !== null && !Array.isArray(variable);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
function assertArray(value, msg) {
|
|
39
|
+
function assertArray(value, ...[msg]) {
|
|
40
40
|
if (!Array.isArray(value)) {
|
|
41
41
|
throw new TypeError(msg);
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
-
function assertArrayBoolean(value, msg) {
|
|
45
|
-
|
|
44
|
+
function assertArrayBoolean(value, ...[msg]) {
|
|
45
|
+
if (!Array.isArray(value)) {
|
|
46
|
+
throw new TypeError(msg);
|
|
47
|
+
}
|
|
46
48
|
if (value.some((element) => typeof element !== "boolean")) {
|
|
47
49
|
throw new TypeError(msg);
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
|
-
function assertArrayNumber(value, msg) {
|
|
51
|
-
|
|
52
|
+
function assertArrayNumber(value, ...[msg]) {
|
|
53
|
+
if (!Array.isArray(value)) {
|
|
54
|
+
throw new TypeError(msg);
|
|
55
|
+
}
|
|
52
56
|
if (value.some((element) => typeof element !== "number")) {
|
|
53
57
|
throw new TypeError(msg);
|
|
54
58
|
}
|
|
55
59
|
}
|
|
56
|
-
function assertArrayObject(value, msg) {
|
|
57
|
-
|
|
60
|
+
function assertArrayObject(value, ...[msg]) {
|
|
61
|
+
if (!Array.isArray(value)) {
|
|
62
|
+
throw new TypeError(msg);
|
|
63
|
+
}
|
|
58
64
|
if (value.some((element) => !isObject(element))) {
|
|
59
65
|
throw new TypeError(msg);
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
|
-
function assertArrayString(value, msg) {
|
|
63
|
-
|
|
68
|
+
function assertArrayString(value, ...[msg]) {
|
|
69
|
+
if (!Array.isArray(value)) {
|
|
70
|
+
throw new TypeError(msg);
|
|
71
|
+
}
|
|
64
72
|
if (value.some((element) => typeof element !== "string")) {
|
|
65
73
|
throw new TypeError(msg);
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
|
-
function assertBoolean(value, msg) {
|
|
76
|
+
function assertBoolean(value, ...[msg]) {
|
|
69
77
|
if (typeof value !== "boolean") {
|
|
70
78
|
throw new TypeError(msg);
|
|
71
79
|
}
|
|
@@ -95,17 +103,17 @@ function assertNotNull(value, ...[msg]) {
|
|
|
95
103
|
throw new TypeError(msg);
|
|
96
104
|
}
|
|
97
105
|
}
|
|
98
|
-
function assertNumber(value, msg) {
|
|
106
|
+
function assertNumber(value, ...[msg]) {
|
|
99
107
|
if (typeof value !== "number") {
|
|
100
108
|
throw new TypeError(msg);
|
|
101
109
|
}
|
|
102
110
|
}
|
|
103
|
-
function assertObject(value, msg) {
|
|
111
|
+
function assertObject(value, ...[msg]) {
|
|
104
112
|
if (!isObject(value)) {
|
|
105
113
|
throw new TypeError(msg);
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
|
-
function assertString(value, msg) {
|
|
116
|
+
function assertString(value, ...[msg]) {
|
|
109
117
|
if (typeof value !== "string") {
|
|
110
118
|
throw new TypeError(msg);
|
|
111
119
|
}
|
|
@@ -449,7 +457,8 @@ const EMOJI_REGEX = /(\p{Extended_Pictographic}|[#*0-9]\uFE0F?\u20E3)/u;
|
|
|
449
457
|
const FIRST_LETTER_CAPITALIZED_REGEX = /^\p{Lu}/u;
|
|
450
458
|
const KEBAB_CASE_REGEX = /^[\da-z]+(?:-[\da-z]+)*$/;
|
|
451
459
|
const SEMANTIC_VERSION_REGEX = /^v*(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/;
|
|
452
|
-
const WHITESPACE_REGEX = /\s
|
|
460
|
+
const WHITESPACE_REGEX = /\s/;
|
|
461
|
+
const WHITESPACE_GLOBAL_REGEX = /\s/g;
|
|
453
462
|
const TITLE_CASE_BOUNDARY_REGEX = /(?<=[\da-z])(?=[A-Z])/g;
|
|
454
463
|
const UPPERCASE_REGEX = /^[A-Z]*$/;
|
|
455
464
|
const LOWERCASE_REGEX = /^[a-z]*$/;
|
|
@@ -574,7 +583,7 @@ function removeNonPrintableCharacters(string) {
|
|
|
574
583
|
return string.replaceAll(/\p{C}/gu, "");
|
|
575
584
|
}
|
|
576
585
|
function removeWhitespace(string) {
|
|
577
|
-
return string.replaceAll(
|
|
586
|
+
return string.replaceAll(WHITESPACE_GLOBAL_REGEX, "");
|
|
578
587
|
}
|
|
579
588
|
function titleCaseToKebabCase(string) {
|
|
580
589
|
return string.replaceAll(TITLE_CASE_BOUNDARY_REGEX, "-").replaceAll(/ +/g, "-").toLowerCase();
|
package/dist/index.mjs
CHANGED
|
@@ -34,36 +34,44 @@ function isObject(variable) {
|
|
|
34
34
|
return typeof variable === "object" && variable !== null && !Array.isArray(variable);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function assertArray(value, msg) {
|
|
37
|
+
function assertArray(value, ...[msg]) {
|
|
38
38
|
if (!Array.isArray(value)) {
|
|
39
39
|
throw new TypeError(msg);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
function assertArrayBoolean(value, msg) {
|
|
43
|
-
|
|
42
|
+
function assertArrayBoolean(value, ...[msg]) {
|
|
43
|
+
if (!Array.isArray(value)) {
|
|
44
|
+
throw new TypeError(msg);
|
|
45
|
+
}
|
|
44
46
|
if (value.some((element) => typeof element !== "boolean")) {
|
|
45
47
|
throw new TypeError(msg);
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
|
-
function assertArrayNumber(value, msg) {
|
|
49
|
-
|
|
50
|
+
function assertArrayNumber(value, ...[msg]) {
|
|
51
|
+
if (!Array.isArray(value)) {
|
|
52
|
+
throw new TypeError(msg);
|
|
53
|
+
}
|
|
50
54
|
if (value.some((element) => typeof element !== "number")) {
|
|
51
55
|
throw new TypeError(msg);
|
|
52
56
|
}
|
|
53
57
|
}
|
|
54
|
-
function assertArrayObject(value, msg) {
|
|
55
|
-
|
|
58
|
+
function assertArrayObject(value, ...[msg]) {
|
|
59
|
+
if (!Array.isArray(value)) {
|
|
60
|
+
throw new TypeError(msg);
|
|
61
|
+
}
|
|
56
62
|
if (value.some((element) => !isObject(element))) {
|
|
57
63
|
throw new TypeError(msg);
|
|
58
64
|
}
|
|
59
65
|
}
|
|
60
|
-
function assertArrayString(value, msg) {
|
|
61
|
-
|
|
66
|
+
function assertArrayString(value, ...[msg]) {
|
|
67
|
+
if (!Array.isArray(value)) {
|
|
68
|
+
throw new TypeError(msg);
|
|
69
|
+
}
|
|
62
70
|
if (value.some((element) => typeof element !== "string")) {
|
|
63
71
|
throw new TypeError(msg);
|
|
64
72
|
}
|
|
65
73
|
}
|
|
66
|
-
function assertBoolean(value, msg) {
|
|
74
|
+
function assertBoolean(value, ...[msg]) {
|
|
67
75
|
if (typeof value !== "boolean") {
|
|
68
76
|
throw new TypeError(msg);
|
|
69
77
|
}
|
|
@@ -93,17 +101,17 @@ function assertNotNull(value, ...[msg]) {
|
|
|
93
101
|
throw new TypeError(msg);
|
|
94
102
|
}
|
|
95
103
|
}
|
|
96
|
-
function assertNumber(value, msg) {
|
|
104
|
+
function assertNumber(value, ...[msg]) {
|
|
97
105
|
if (typeof value !== "number") {
|
|
98
106
|
throw new TypeError(msg);
|
|
99
107
|
}
|
|
100
108
|
}
|
|
101
|
-
function assertObject(value, msg) {
|
|
109
|
+
function assertObject(value, ...[msg]) {
|
|
102
110
|
if (!isObject(value)) {
|
|
103
111
|
throw new TypeError(msg);
|
|
104
112
|
}
|
|
105
113
|
}
|
|
106
|
-
function assertString(value, msg) {
|
|
114
|
+
function assertString(value, ...[msg]) {
|
|
107
115
|
if (typeof value !== "string") {
|
|
108
116
|
throw new TypeError(msg);
|
|
109
117
|
}
|
|
@@ -447,7 +455,8 @@ const EMOJI_REGEX = /(\p{Extended_Pictographic}|[#*0-9]\uFE0F?\u20E3)/u;
|
|
|
447
455
|
const FIRST_LETTER_CAPITALIZED_REGEX = /^\p{Lu}/u;
|
|
448
456
|
const KEBAB_CASE_REGEX = /^[\da-z]+(?:-[\da-z]+)*$/;
|
|
449
457
|
const SEMANTIC_VERSION_REGEX = /^v*(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/;
|
|
450
|
-
const WHITESPACE_REGEX = /\s
|
|
458
|
+
const WHITESPACE_REGEX = /\s/;
|
|
459
|
+
const WHITESPACE_GLOBAL_REGEX = /\s/g;
|
|
451
460
|
const TITLE_CASE_BOUNDARY_REGEX = /(?<=[\da-z])(?=[A-Z])/g;
|
|
452
461
|
const UPPERCASE_REGEX = /^[A-Z]*$/;
|
|
453
462
|
const LOWERCASE_REGEX = /^[a-z]*$/;
|
|
@@ -572,7 +581,7 @@ function removeNonPrintableCharacters(string) {
|
|
|
572
581
|
return string.replaceAll(/\p{C}/gu, "");
|
|
573
582
|
}
|
|
574
583
|
function removeWhitespace(string) {
|
|
575
|
-
return string.replaceAll(
|
|
584
|
+
return string.replaceAll(WHITESPACE_GLOBAL_REGEX, "");
|
|
576
585
|
}
|
|
577
586
|
function titleCaseToKebabCase(string) {
|
|
578
587
|
return string.replaceAll(TITLE_CASE_BOUNDARY_REGEX, "-").replaceAll(/ +/g, "-").toLowerCase();
|
package/package.json
CHANGED
package/src/functions/assert.ts
CHANGED
|
@@ -9,10 +9,14 @@ import { isEnumValue } from "./enums.js";
|
|
|
9
9
|
import { isObject } from "./types.js";
|
|
10
10
|
|
|
11
11
|
/** Helper function to throw an error if the provided value is not an array. */
|
|
12
|
-
export function assertArray(
|
|
13
|
-
value:
|
|
14
|
-
msg:
|
|
15
|
-
|
|
12
|
+
export function assertArray<T>(
|
|
13
|
+
value: T,
|
|
14
|
+
...[msg]: [T] extends [readonly unknown[]]
|
|
15
|
+
? [
|
|
16
|
+
"The assertion is useless because the provided value is already an array.",
|
|
17
|
+
]
|
|
18
|
+
: [string]
|
|
19
|
+
): asserts value is T & unknown[] {
|
|
16
20
|
if (!Array.isArray(value)) {
|
|
17
21
|
throw new TypeError(msg);
|
|
18
22
|
}
|
|
@@ -22,13 +26,19 @@ export function assertArray(
|
|
|
22
26
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
23
27
|
* a boolean.
|
|
24
28
|
*/
|
|
25
|
-
export function assertArrayBoolean(
|
|
26
|
-
value:
|
|
27
|
-
msg:
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
export function assertArrayBoolean<T>(
|
|
30
|
+
value: T,
|
|
31
|
+
...[msg]: [T] extends [readonly boolean[]]
|
|
32
|
+
? [
|
|
33
|
+
"The assertion is useless because the provided value is already a boolean array.",
|
|
34
|
+
]
|
|
35
|
+
: [string]
|
|
36
|
+
): asserts value is T & boolean[] {
|
|
37
|
+
if (!Array.isArray(value)) {
|
|
38
|
+
throw new TypeError(msg);
|
|
39
|
+
}
|
|
30
40
|
|
|
31
|
-
if (value.some((element) => typeof element !== "boolean")) {
|
|
41
|
+
if ((value as unknown[]).some((element) => typeof element !== "boolean")) {
|
|
32
42
|
throw new TypeError(msg);
|
|
33
43
|
}
|
|
34
44
|
}
|
|
@@ -37,13 +47,19 @@ export function assertArrayBoolean(
|
|
|
37
47
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
38
48
|
* a number.
|
|
39
49
|
*/
|
|
40
|
-
export function assertArrayNumber(
|
|
41
|
-
value:
|
|
42
|
-
msg:
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
export function assertArrayNumber<T>(
|
|
51
|
+
value: T,
|
|
52
|
+
...[msg]: [T] extends [readonly number[]]
|
|
53
|
+
? [
|
|
54
|
+
"The assertion is useless because the provided value is already a number array.",
|
|
55
|
+
]
|
|
56
|
+
: [string]
|
|
57
|
+
): asserts value is T & number[] {
|
|
58
|
+
if (!Array.isArray(value)) {
|
|
59
|
+
throw new TypeError(msg);
|
|
60
|
+
}
|
|
45
61
|
|
|
46
|
-
if (value.some((element) => typeof element !== "number")) {
|
|
62
|
+
if ((value as unknown[]).some((element) => typeof element !== "number")) {
|
|
47
63
|
throw new TypeError(msg);
|
|
48
64
|
}
|
|
49
65
|
}
|
|
@@ -52,13 +68,19 @@ export function assertArrayNumber(
|
|
|
52
68
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
53
69
|
* an object (i.e. a TypeScript record).
|
|
54
70
|
*/
|
|
55
|
-
export function assertArrayObject(
|
|
56
|
-
value:
|
|
57
|
-
msg: string,
|
|
58
|
-
|
|
59
|
-
|
|
71
|
+
export function assertArrayObject<T>(
|
|
72
|
+
value: T,
|
|
73
|
+
...[msg]: [T] extends [ReadonlyArray<Record<string, unknown>>]
|
|
74
|
+
? [
|
|
75
|
+
"The assertion is useless because the provided value is already an object array.",
|
|
76
|
+
]
|
|
77
|
+
: [string]
|
|
78
|
+
): asserts value is T & Array<Record<string, unknown>> {
|
|
79
|
+
if (!Array.isArray(value)) {
|
|
80
|
+
throw new TypeError(msg);
|
|
81
|
+
}
|
|
60
82
|
|
|
61
|
-
if (value.some((element) => !isObject(element))) {
|
|
83
|
+
if ((value as unknown[]).some((element) => !isObject(element))) {
|
|
62
84
|
throw new TypeError(msg);
|
|
63
85
|
}
|
|
64
86
|
}
|
|
@@ -67,22 +89,32 @@ export function assertArrayObject(
|
|
|
67
89
|
* Helper function to throw an error if the provided value is not an array with every element being
|
|
68
90
|
* a string.
|
|
69
91
|
*/
|
|
70
|
-
export function assertArrayString(
|
|
71
|
-
value:
|
|
72
|
-
msg: string
|
|
73
|
-
|
|
74
|
-
|
|
92
|
+
export function assertArrayString<T>(
|
|
93
|
+
value: T,
|
|
94
|
+
...[msg]: [T] extends [readonly string[]]
|
|
95
|
+
? [
|
|
96
|
+
"The assertion is useless because the provided value is already a string array.",
|
|
97
|
+
]
|
|
98
|
+
: [string]
|
|
99
|
+
): asserts value is T & string[] {
|
|
100
|
+
if (!Array.isArray(value)) {
|
|
101
|
+
throw new TypeError(msg);
|
|
102
|
+
}
|
|
75
103
|
|
|
76
|
-
if (value.some((element) => typeof element !== "string")) {
|
|
104
|
+
if ((value as unknown[]).some((element) => typeof element !== "string")) {
|
|
77
105
|
throw new TypeError(msg);
|
|
78
106
|
}
|
|
79
107
|
}
|
|
80
108
|
|
|
81
109
|
/** Helper function to throw an error if the provided value is not a boolean. */
|
|
82
|
-
export function assertBoolean(
|
|
83
|
-
value:
|
|
84
|
-
msg:
|
|
85
|
-
|
|
110
|
+
export function assertBoolean<T>(
|
|
111
|
+
value: T,
|
|
112
|
+
...[msg]: [T] extends [boolean]
|
|
113
|
+
? [
|
|
114
|
+
"The assertion is useless because the provided value is already a boolean.",
|
|
115
|
+
]
|
|
116
|
+
: [string]
|
|
117
|
+
): asserts value is T & boolean {
|
|
86
118
|
if (typeof value !== "boolean") {
|
|
87
119
|
throw new TypeError(msg);
|
|
88
120
|
}
|
|
@@ -175,10 +207,14 @@ export function assertNotNull<T>(
|
|
|
175
207
|
}
|
|
176
208
|
|
|
177
209
|
/** Helper function to throw an error if the provided value is not a number. */
|
|
178
|
-
export function assertNumber(
|
|
179
|
-
value:
|
|
180
|
-
msg:
|
|
181
|
-
|
|
210
|
+
export function assertNumber<T>(
|
|
211
|
+
value: T,
|
|
212
|
+
...[msg]: [T] extends [number]
|
|
213
|
+
? [
|
|
214
|
+
"The assertion is useless because the provided value is already a number.",
|
|
215
|
+
]
|
|
216
|
+
: [string]
|
|
217
|
+
): asserts value is T & number {
|
|
182
218
|
if (typeof value !== "number") {
|
|
183
219
|
throw new TypeError(msg);
|
|
184
220
|
}
|
|
@@ -193,20 +229,28 @@ export function assertNumber(
|
|
|
193
229
|
*
|
|
194
230
|
* Under the hood, this function uses the `isObject` helper function.
|
|
195
231
|
*/
|
|
196
|
-
export function assertObject(
|
|
197
|
-
value:
|
|
198
|
-
msg: string,
|
|
199
|
-
|
|
232
|
+
export function assertObject<T>(
|
|
233
|
+
value: T,
|
|
234
|
+
...[msg]: [T] extends [Record<string, unknown>]
|
|
235
|
+
? [
|
|
236
|
+
"The assertion is useless because the provided value is already an object.",
|
|
237
|
+
]
|
|
238
|
+
: [string]
|
|
239
|
+
): asserts value is T & Record<string, unknown> {
|
|
200
240
|
if (!isObject(value)) {
|
|
201
241
|
throw new TypeError(msg);
|
|
202
242
|
}
|
|
203
243
|
}
|
|
204
244
|
|
|
205
245
|
/** Helper function to throw an error if the provided value is not a string. */
|
|
206
|
-
export function assertString(
|
|
207
|
-
value:
|
|
208
|
-
msg: string
|
|
209
|
-
|
|
246
|
+
export function assertString<T>(
|
|
247
|
+
value: T,
|
|
248
|
+
...[msg]: [T] extends [string]
|
|
249
|
+
? [
|
|
250
|
+
"The assertion is useless because the provided value is already a string.",
|
|
251
|
+
]
|
|
252
|
+
: [string]
|
|
253
|
+
): asserts value is T & string {
|
|
210
254
|
if (typeof value !== "string") {
|
|
211
255
|
throw new TypeError(msg);
|
|
212
256
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
/* eslint-disable complete/require-ascii */
|
|
2
|
+
|
|
1
3
|
import { equal } from "node:assert";
|
|
2
4
|
import { describe, test } from "node:test";
|
|
3
5
|
import {
|
|
4
6
|
hasDiacritic,
|
|
5
7
|
hasEmoji,
|
|
8
|
+
isASCII,
|
|
6
9
|
isKebabCase,
|
|
7
10
|
titleCaseToKebabCase,
|
|
8
11
|
trimPrefix,
|
|
@@ -61,6 +64,27 @@ describe("hasDiacritic", () => {
|
|
|
61
64
|
});
|
|
62
65
|
});
|
|
63
66
|
|
|
67
|
+
describe("isASCII", () => {
|
|
68
|
+
test("should return true for ASCII strings", () => {
|
|
69
|
+
equal(isASCII("Hello World"), true);
|
|
70
|
+
equal(isASCII("1234567890"), true);
|
|
71
|
+
equal(isASCII("!@#$%^&*()_+"), true);
|
|
72
|
+
equal(isASCII(" \n\t\r"), true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("should return false for non-ASCII strings", () => {
|
|
76
|
+
equal(isASCII("Hello World ๐"), false);
|
|
77
|
+
equal(isASCII("รก"), false);
|
|
78
|
+
equal(isASCII("รจ"), false);
|
|
79
|
+
equal(isASCII("รด"), false);
|
|
80
|
+
equal(isASCII("๐"), false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("should handle empty string", () => {
|
|
84
|
+
equal(isASCII(""), true);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
64
88
|
describe("isKebabCase", () => {
|
|
65
89
|
test("should return true for valid kebab-case strings", () => {
|
|
66
90
|
equal(isKebabCase("hello-world"), true);
|
package/src/functions/string.ts
CHANGED
|
@@ -7,8 +7,11 @@
|
|
|
7
7
|
import { parseIntSafe } from "./utils.js";
|
|
8
8
|
|
|
9
9
|
// When regular expressions are located at the root instead of inside the function, the functions
|
|
10
|
-
// are tested to perform 11% faster.
|
|
10
|
+
// are tested to perform 11% faster. However, since the global flag makes a regex stateful, care has
|
|
11
|
+
// to be taken to ensure that regexes used for validation do not use the global flag. Otherwise,
|
|
12
|
+
// they will return inconsistent results due to the `lastIndex` property persisting between calls.
|
|
11
13
|
|
|
14
|
+
/** We use a "*" instead of a "+" so that an empty string will match. */
|
|
12
15
|
// eslint-disable-next-line no-control-regex
|
|
13
16
|
const ASCII_REGEX = /^[\u0000-\u007F]*$/;
|
|
14
17
|
|
|
@@ -23,7 +26,8 @@ const EMOJI_REGEX = /(\p{Extended_Pictographic}|[#*0-9]\uFE0F?\u20E3)/u;
|
|
|
23
26
|
const FIRST_LETTER_CAPITALIZED_REGEX = /^\p{Lu}/u;
|
|
24
27
|
const KEBAB_CASE_REGEX = /^[\da-z]+(?:-[\da-z]+)*$/;
|
|
25
28
|
const SEMANTIC_VERSION_REGEX = /^v*(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)/;
|
|
26
|
-
const WHITESPACE_REGEX = /\s
|
|
29
|
+
const WHITESPACE_REGEX = /\s/;
|
|
30
|
+
const WHITESPACE_GLOBAL_REGEX = /\s/g;
|
|
27
31
|
const TITLE_CASE_BOUNDARY_REGEX = /(?<=[\da-z])(?=[A-Z])/g;
|
|
28
32
|
const UPPERCASE_REGEX = /^[A-Z]*$/;
|
|
29
33
|
const LOWERCASE_REGEX = /^[a-z]*$/;
|
|
@@ -245,7 +249,7 @@ export function parseSemanticVersion(versionString: string):
|
|
|
245
249
|
*
|
|
246
250
|
* For example, by using a marker of "@foo":
|
|
247
251
|
*
|
|
248
|
-
* ```
|
|
252
|
+
* ```txt
|
|
249
253
|
* line1
|
|
250
254
|
* # @foo-start
|
|
251
255
|
* line2
|
|
@@ -256,7 +260,7 @@ export function parseSemanticVersion(versionString: string):
|
|
|
256
260
|
*
|
|
257
261
|
* Would return:
|
|
258
262
|
*
|
|
259
|
-
* ```
|
|
263
|
+
* ```txt
|
|
260
264
|
* line1
|
|
261
265
|
* line4
|
|
262
266
|
* ```
|
|
@@ -309,7 +313,7 @@ export function removeNonPrintableCharacters(string: string): string {
|
|
|
309
313
|
|
|
310
314
|
/** Helper function to remove all whitespace characters from a string. */
|
|
311
315
|
export function removeWhitespace(string: string): string {
|
|
312
|
-
return string.replaceAll(
|
|
316
|
+
return string.replaceAll(WHITESPACE_GLOBAL_REGEX, "");
|
|
313
317
|
}
|
|
314
318
|
|
|
315
319
|
/** Helper function to convert a string from TitleCase (PascalCase) to kebab-case. */
|