ts-data-forge 10.0.0 → 11.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/dist/array/impl/array-utils-creation.d.mts +3 -3
- package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-creation.mjs.map +1 -1
- package/dist/array/impl/array-utils-element-access.d.mts +4 -4
- package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
- package/dist/{guard/as-length-bounded-array.d.mts → array/impl/array-utils-length-bounded-array-cast.d.mts} +9 -9
- package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts.map +1 -0
- package/dist/{guard/as-length-bounded-array.mjs → array/impl/array-utils-length-bounded-array-cast.mjs} +2 -2
- package/dist/array/impl/array-utils-length-bounded-array-cast.mjs.map +1 -0
- package/dist/{guard/is-length-bounded-array.d.mts → array/impl/array-utils-length-bounded-array-guard.d.mts} +13 -13
- package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts.map +1 -0
- package/dist/{guard/is-length-bounded-array.mjs → array/impl/array-utils-length-bounded-array-guard.mjs} +1 -1
- package/dist/array/impl/array-utils-length-bounded-array-guard.mjs.map +1 -0
- package/dist/array/impl/array-utils-modification.d.mts +9 -9
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts +9 -9
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/array-utils-size.d.mts +3 -3
- package/dist/array/impl/array-utils-size.mjs.map +1 -1
- package/dist/array/impl/array-utils-slicing.d.mts +9 -9
- package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
- package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
- package/dist/array/impl/array-utils-transformation.d.mts +16 -10
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/index.d.mts +2 -0
- package/dist/array/impl/index.d.mts.map +1 -1
- package/dist/array/impl/index.mjs +2 -0
- package/dist/array/impl/index.mjs.map +1 -1
- package/dist/entry-point.mjs +1 -3
- package/dist/entry-point.mjs.map +1 -1
- package/dist/guard/index.d.mts +0 -3
- package/dist/guard/index.d.mts.map +1 -1
- package/dist/guard/index.mjs +0 -3
- package/dist/guard/index.mjs.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -3
- package/dist/index.mjs.map +1 -1
- package/dist/number/branded-types/finite-number.d.mts +47 -61
- package/dist/number/branded-types/finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/finite-number.mjs +37 -41
- package/dist/number/branded-types/finite-number.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +40 -46
- package/dist/number/branded-types/int.d.mts.map +1 -1
- package/dist/number/branded-types/int.mjs +29 -31
- package/dist/number/branded-types/int.mjs.map +1 -1
- package/dist/number/branded-types/int16.d.mts +65 -58
- package/dist/number/branded-types/int16.d.mts.map +1 -1
- package/dist/number/branded-types/int16.mjs +49 -45
- package/dist/number/branded-types/int16.mjs.map +1 -1
- package/dist/number/branded-types/int32.d.mts +65 -58
- package/dist/number/branded-types/int32.d.mts.map +1 -1
- package/dist/number/branded-types/int32.mjs +49 -45
- package/dist/number/branded-types/int32.mjs.map +1 -1
- package/dist/number/branded-types/negative-finite-number.d.mts +68 -59
- package/dist/number/branded-types/negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/negative-finite-number.mjs +52 -46
- package/dist/number/branded-types/negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/negative-int.d.mts +60 -48
- package/dist/number/branded-types/negative-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-int.mjs +40 -36
- package/dist/number/branded-types/negative-int.mjs.map +1 -1
- package/dist/number/branded-types/negative-safe-int.d.mts +57 -56
- package/dist/number/branded-types/negative-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/negative-safe-int.mjs +40 -41
- package/dist/number/branded-types/negative-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.d.mts +74 -64
- package/dist/number/branded-types/non-negative-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-finite-number.mjs +58 -51
- package/dist/number/branded-types/non-negative-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +64 -57
- package/dist/number/branded-types/non-negative-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int16.mjs +47 -43
- package/dist/number/branded-types/non-negative-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +64 -58
- package/dist/number/branded-types/non-negative-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-negative-int32.mjs +47 -44
- package/dist/number/branded-types/non-negative-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.d.mts +64 -54
- package/dist/number/branded-types/non-positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-finite-number.mjs +48 -41
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-int.d.mts +41 -146
- package/dist/number/branded-types/non-positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-int.mjs +27 -107
- package/dist/number/branded-types/non-positive-int.mjs.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.d.mts +54 -53
- package/dist/number/branded-types/non-positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-positive-safe-int.mjs +37 -39
- package/dist/number/branded-types/non-positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.d.mts +62 -49
- package/dist/number/branded-types/non-zero-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-finite-number.mjs +46 -37
- package/dist/number/branded-types/non-zero-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int.d.mts +51 -38
- package/dist/number/branded-types/non-zero-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int.mjs +31 -27
- package/dist/number/branded-types/non-zero-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int16.d.mts +52 -53
- package/dist/number/branded-types/non-zero-int16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int16.mjs +36 -39
- package/dist/number/branded-types/non-zero-int16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-int32.d.mts +52 -54
- package/dist/number/branded-types/non-zero-int32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-int32.mjs +36 -40
- package/dist/number/branded-types/non-zero-int32.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.d.mts +53 -54
- package/dist/number/branded-types/non-zero-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-safe-int.mjs +36 -40
- package/dist/number/branded-types/non-zero-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.d.mts +52 -53
- package/dist/number/branded-types/non-zero-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint16.mjs +35 -39
- package/dist/number/branded-types/non-zero-uint16.mjs.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.d.mts +52 -53
- package/dist/number/branded-types/non-zero-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/non-zero-uint32.mjs +35 -39
- package/dist/number/branded-types/non-zero-uint32.mjs.map +1 -1
- package/dist/number/branded-types/positive-finite-number.d.mts +74 -63
- package/dist/number/branded-types/positive-finite-number.d.mts.map +1 -1
- package/dist/number/branded-types/positive-finite-number.mjs +58 -50
- package/dist/number/branded-types/positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +46 -68
- package/dist/number/branded-types/positive-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int.mjs +34 -49
- package/dist/number/branded-types/positive-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +61 -57
- package/dist/number/branded-types/positive-int16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int16.mjs +44 -43
- package/dist/number/branded-types/positive-int16.mjs.map +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +61 -57
- package/dist/number/branded-types/positive-int32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-int32.mjs +44 -43
- package/dist/number/branded-types/positive-int32.mjs.map +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +62 -57
- package/dist/number/branded-types/positive-safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/positive-safe-int.mjs +45 -43
- package/dist/number/branded-types/positive-safe-int.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +61 -57
- package/dist/number/branded-types/positive-uint16.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint16.mjs +44 -43
- package/dist/number/branded-types/positive-uint16.mjs.map +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +61 -58
- package/dist/number/branded-types/positive-uint32.d.mts.map +1 -1
- package/dist/number/branded-types/positive-uint32.mjs +44 -44
- package/dist/number/branded-types/positive-uint32.mjs.map +1 -1
- package/dist/number/branded-types/safe-int.d.mts +43 -62
- package/dist/number/branded-types/safe-int.d.mts.map +1 -1
- package/dist/number/branded-types/safe-int.mjs +35 -42
- package/dist/number/branded-types/safe-int.mjs.map +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +62 -57
- package/dist/number/branded-types/safe-uint.d.mts.map +1 -1
- package/dist/number/branded-types/safe-uint.mjs +46 -44
- package/dist/number/branded-types/safe-uint.mjs.map +1 -1
- package/dist/number/branded-types/uint.d.mts +63 -50
- package/dist/number/branded-types/uint.d.mts.map +1 -1
- package/dist/number/branded-types/uint.mjs +42 -39
- package/dist/number/branded-types/uint.mjs.map +1 -1
- package/dist/number/branded-types/uint16.d.mts +60 -58
- package/dist/number/branded-types/uint16.d.mts.map +1 -1
- package/dist/number/branded-types/uint16.mjs +44 -43
- package/dist/number/branded-types/uint16.mjs.map +1 -1
- package/dist/number/branded-types/uint32.d.mts +59 -55
- package/dist/number/branded-types/uint32.d.mts.map +1 -1
- package/dist/number/branded-types/uint32.mjs +42 -42
- package/dist/number/branded-types/uint32.mjs.map +1 -1
- package/dist/number/enum/int8.d.mts +56 -65
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/int8.mjs +44 -43
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.d.mts +60 -58
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/number/enum/uint8.mjs +44 -43
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/string/index.d.mts +2 -0
- package/dist/string/index.d.mts.map +1 -0
- package/dist/string/index.mjs +2 -0
- package/dist/string/index.mjs.map +1 -0
- package/dist/string/str.d.mts +273 -0
- package/dist/string/str.d.mts.map +1 -0
- package/dist/string/str.mjs +42 -0
- package/dist/string/str.mjs.map +1 -0
- package/package.json +31 -92
- package/src/array/impl/array-utils-creation.mts +2 -1
- package/src/array/impl/array-utils-element-access.mts +3 -3
- package/src/array/impl/array-utils-element-access.test.mts +3 -3
- package/src/{guard/as-length-bounded-array.mts → array/impl/array-utils-length-bounded-array-cast.mts} +9 -9
- package/src/{guard/as-length-bounded-array.test.mts → array/impl/array-utils-length-bounded-array-cast.test.mts} +2 -2
- package/src/{guard/is-length-bounded-array.mts → array/impl/array-utils-length-bounded-array-guard.mts} +12 -12
- package/src/{guard/is-length-bounded-array.test.mts → array/impl/array-utils-length-bounded-array-guard.test.mts} +2 -2
- package/src/array/impl/array-utils-modification.mts +9 -8
- package/src/array/impl/array-utils-modification.test.mts +6 -1
- package/src/array/impl/array-utils-reducing-value.mts +9 -9
- package/src/array/impl/array-utils-size.mts +2 -2
- package/src/array/impl/array-utils-slicing.mts +24 -13
- package/src/array/impl/array-utils-transformation.mts +18 -12
- package/src/array/impl/array-utils-transformation.test.mts +12 -12
- package/src/array/impl/index.mts +2 -0
- package/src/guard/index.mts +0 -3
- package/src/index.mts +1 -0
- package/src/number/branded-types/finite-number.mts +47 -61
- package/src/number/branded-types/int.mts +40 -46
- package/src/number/branded-types/int16.mts +65 -58
- package/src/number/branded-types/int32.mts +65 -58
- package/src/number/branded-types/negative-finite-number.mts +68 -68
- package/src/number/branded-types/negative-int.mts +60 -57
- package/src/number/branded-types/negative-safe-int.mts +57 -65
- package/src/number/branded-types/non-negative-finite-number.mts +74 -64
- package/src/number/branded-types/non-negative-int16.mts +64 -57
- package/src/number/branded-types/non-negative-int32.mts +64 -58
- package/src/number/branded-types/non-positive-finite-number.mts +64 -54
- package/src/number/branded-types/non-positive-int.mts +41 -146
- package/src/number/branded-types/non-positive-safe-int.mts +54 -53
- package/src/number/branded-types/non-zero-finite-number.mts +62 -51
- package/src/number/branded-types/non-zero-int.mts +51 -38
- package/src/number/branded-types/non-zero-int16.mts +52 -53
- package/src/number/branded-types/non-zero-int32.mts +52 -54
- package/src/number/branded-types/non-zero-safe-int.mts +53 -54
- package/src/number/branded-types/non-zero-uint16.mts +52 -53
- package/src/number/branded-types/non-zero-uint32.mts +52 -53
- package/src/number/branded-types/positive-finite-number.mts +74 -63
- package/src/number/branded-types/positive-int.mts +46 -68
- package/src/number/branded-types/positive-int16.mts +61 -57
- package/src/number/branded-types/positive-int32.mts +61 -57
- package/src/number/branded-types/positive-safe-int.mts +62 -57
- package/src/number/branded-types/positive-uint16.mts +61 -57
- package/src/number/branded-types/positive-uint32.mts +61 -58
- package/src/number/branded-types/safe-int.mts +43 -62
- package/src/number/branded-types/safe-uint.mts +62 -57
- package/src/number/branded-types/uint.mts +63 -50
- package/src/number/branded-types/uint16.mts +60 -58
- package/src/number/branded-types/uint32.mts +59 -55
- package/src/number/enum/int8.mts +56 -65
- package/src/number/enum/uint8.mts +60 -58
- package/src/string/index.mts +1 -0
- package/src/string/str.mts +371 -0
- package/src/string/str.test.mts +215 -0
- package/dist/guard/as-length-bounded-array.d.mts.map +0 -1
- package/dist/guard/as-length-bounded-array.mjs.map +0 -1
- package/dist/guard/is-length-bounded-array.d.mts.map +0 -1
- package/dist/guard/is-length-bounded-array.mjs.map +0 -1
- package/dist/guard/is-length-bounded-string.d.mts +0 -163
- package/dist/guard/is-length-bounded-string.d.mts.map +0 -1
- package/dist/guard/is-length-bounded-string.mjs +0 -7
- package/dist/guard/is-length-bounded-string.mjs.map +0 -1
- package/src/guard/is-length-bounded-string.mts +0 -199
- package/src/guard/is-length-bounded-string.test.mts +0 -170
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BoundedLengthString,
|
|
3
|
+
type FixedLengthString,
|
|
4
|
+
type MaxLengthString,
|
|
5
|
+
type MinLengthString,
|
|
6
|
+
type SupportedLength,
|
|
7
|
+
} from 'ts-type-forge';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Namespace providing type guards and runtime-checked casts for the branded
|
|
11
|
+
* length-constrained string types (`MinLengthString`, `MaxLengthString`,
|
|
12
|
+
* `BoundedLengthString`, `FixedLengthString`).
|
|
13
|
+
*
|
|
14
|
+
* Lengths are measured in UTF-16 code units (the same unit as
|
|
15
|
+
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
16
|
+
* Plane (e.g. emoji) count as 2.
|
|
17
|
+
*/
|
|
18
|
+
export namespace Str {
|
|
19
|
+
/**
|
|
20
|
+
* Type guard that checks if a string has at least `minLength` characters.
|
|
21
|
+
*
|
|
22
|
+
* **Type Narrowing Behavior:**
|
|
23
|
+
*
|
|
24
|
+
* - Narrows the input to `MinLengthString<MinLength>` while preserving the
|
|
25
|
+
* original string type (e.g. literal types) via intersection.
|
|
26
|
+
* - The result participates in the length-constraint subtyping relation: for
|
|
27
|
+
* example, a value narrowed to `MinLengthString<3>` is assignable to
|
|
28
|
+
* `MinLengthString<1>` (= `NonEmptyString`).
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const input: string = 'very-secret-password';
|
|
34
|
+
*
|
|
35
|
+
* assert.isTrue(Str.isMinLengthString(input, 12));
|
|
36
|
+
*
|
|
37
|
+
* assert.isFalse(Str.isMinLengthString('short', 12));
|
|
38
|
+
*
|
|
39
|
+
* if (Str.isMinLengthString(input, 12)) {
|
|
40
|
+
* const nonEmpty: NonEmptyString = input; // OK (12 >= 1)
|
|
41
|
+
*
|
|
42
|
+
* assert.isTrue(nonEmpty.length >= 12);
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @template S - The input string type (literal types are preserved).
|
|
47
|
+
* @template MinLength - The minimum number of characters (inclusive).
|
|
48
|
+
* @param s - The string to check.
|
|
49
|
+
* @param minLength - The minimum number of characters (inclusive).
|
|
50
|
+
* @returns `true` if `s.length >= minLength`, `false` otherwise. When `true`,
|
|
51
|
+
* TypeScript narrows `s` to `MinLengthString<MinLength> & S`.
|
|
52
|
+
*/
|
|
53
|
+
export const isMinLengthString = <
|
|
54
|
+
S extends string,
|
|
55
|
+
MinLength extends SupportedLength,
|
|
56
|
+
>(
|
|
57
|
+
s: S,
|
|
58
|
+
minLength: MinLength,
|
|
59
|
+
): s is MinLengthString<MinLength> & S => s.length >= minLength;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Type guard that checks if a string has at most `maxLength` characters.
|
|
63
|
+
*
|
|
64
|
+
* **Type Narrowing Behavior:**
|
|
65
|
+
*
|
|
66
|
+
* - Narrows the input to `MaxLengthString<MaxLength>` while preserving the
|
|
67
|
+
* original string type (e.g. literal types) via intersection.
|
|
68
|
+
* - The result participates in the length-constraint subtyping relation: for
|
|
69
|
+
* example, a value narrowed to `MaxLengthString<16>` is assignable to
|
|
70
|
+
* `MaxLengthString<32>`.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* const input: string = 'noshiro';
|
|
76
|
+
*
|
|
77
|
+
* assert.isTrue(Str.isMaxLengthString(input, 32));
|
|
78
|
+
*
|
|
79
|
+
* assert.isFalse(Str.isMaxLengthString(input, 3));
|
|
80
|
+
*
|
|
81
|
+
* if (Str.isMaxLengthString(input, 32)) {
|
|
82
|
+
* const relaxed: MaxLengthString<64> = input; // OK (32 <= 64)
|
|
83
|
+
*
|
|
84
|
+
* assert.isTrue(relaxed.length <= 32);
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @template S - The input string type (literal types are preserved).
|
|
89
|
+
* @template MaxLength - The maximum number of characters (inclusive).
|
|
90
|
+
* @param s - The string to check.
|
|
91
|
+
* @param maxLength - The maximum number of characters (inclusive).
|
|
92
|
+
* @returns `true` if `s.length <= maxLength`, `false` otherwise. When `true`,
|
|
93
|
+
* TypeScript narrows `s` to `MaxLengthString<MaxLength> & S`.
|
|
94
|
+
*/
|
|
95
|
+
export const isMaxLengthString = <
|
|
96
|
+
S extends string,
|
|
97
|
+
MaxLength extends SupportedLength,
|
|
98
|
+
>(
|
|
99
|
+
s: S,
|
|
100
|
+
maxLength: MaxLength,
|
|
101
|
+
): s is MaxLengthString<MaxLength> & S => s.length <= maxLength;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Type guard that checks if a string's length is within the inclusive range
|
|
105
|
+
* `[minLength, maxLength]`.
|
|
106
|
+
*
|
|
107
|
+
* **Type Narrowing Behavior:**
|
|
108
|
+
*
|
|
109
|
+
* - Narrows the input to `BoundedLengthString<MinLength, MaxLength>` while
|
|
110
|
+
* preserving the original string type (e.g. literal types) via intersection.
|
|
111
|
+
* - Since `BoundedLengthString` is the intersection of `MinLengthString` and
|
|
112
|
+
* `MaxLengthString`, the narrowed value is assignable to both, and both
|
|
113
|
+
* bounds can be weakened independently.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
*
|
|
117
|
+
* ```ts
|
|
118
|
+
* const input: string = 'user-12345678';
|
|
119
|
+
*
|
|
120
|
+
* assert.isTrue(Str.isBoundedLengthString(input, 8, 16));
|
|
121
|
+
*
|
|
122
|
+
* assert.isFalse(Str.isBoundedLengthString('user', 8, 16));
|
|
123
|
+
*
|
|
124
|
+
* if (Str.isBoundedLengthString(input, 8, 16)) {
|
|
125
|
+
* const userId: BoundedLengthString<1, 255> = input; // OK ([8, 16] ⊆ [1, 255])
|
|
126
|
+
*
|
|
127
|
+
* assert.isTrue(userId.length >= 8 && userId.length <= 16);
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @template S - The input string type (literal types are preserved).
|
|
132
|
+
* @template MinLength - The minimum number of characters (inclusive).
|
|
133
|
+
* @template MaxLength - The maximum number of characters (inclusive).
|
|
134
|
+
* @param s - The string to check.
|
|
135
|
+
* @param minLength - The minimum number of characters (inclusive).
|
|
136
|
+
* @param maxLength - The maximum number of characters (inclusive).
|
|
137
|
+
* @returns `true` if `minLength <= s.length && s.length <= maxLength`, `false`
|
|
138
|
+
* otherwise. When `true`, TypeScript narrows `s` to
|
|
139
|
+
* `BoundedLengthString<MinLength, MaxLength> & S`.
|
|
140
|
+
*/
|
|
141
|
+
export const isBoundedLengthString = <
|
|
142
|
+
S extends string,
|
|
143
|
+
MinLength extends SupportedLength,
|
|
144
|
+
MaxLength extends SupportedLength,
|
|
145
|
+
>(
|
|
146
|
+
s: S,
|
|
147
|
+
minLength: MinLength,
|
|
148
|
+
maxLength: MaxLength,
|
|
149
|
+
): s is BoundedLengthString<MinLength, MaxLength> & S =>
|
|
150
|
+
s.length >= minLength && s.length <= maxLength;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Type guard that checks if a string has exactly `length` characters.
|
|
154
|
+
*
|
|
155
|
+
* **Type Narrowing Behavior:**
|
|
156
|
+
*
|
|
157
|
+
* - Narrows the input to `FixedLengthString<Length>` while preserving the
|
|
158
|
+
* original string type (e.g. literal types) via intersection.
|
|
159
|
+
* - Since `FixedLengthString<Length>` is defined as
|
|
160
|
+
* `BoundedLengthString<Length, Length>`, the narrowed value is assignable to
|
|
161
|
+
* any `MinLengthString<N>` with `N <= Length` and any `MaxLengthString<N>`
|
|
162
|
+
* with `N >= Length`.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
*
|
|
166
|
+
* ```ts
|
|
167
|
+
* const input: string = 'JP';
|
|
168
|
+
*
|
|
169
|
+
* assert.isTrue(Str.isFixedLengthString(input, 2));
|
|
170
|
+
*
|
|
171
|
+
* assert.isFalse(Str.isFixedLengthString(input, 3));
|
|
172
|
+
*
|
|
173
|
+
* if (Str.isFixedLengthString(input, 2)) {
|
|
174
|
+
* const atMost5: MaxLengthString<5> = input; // OK (2 <= 5)
|
|
175
|
+
*
|
|
176
|
+
* assert.isTrue(atMost5.length === 2);
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* @template S - The input string type (literal types are preserved).
|
|
181
|
+
* @template Length - The exact number of characters.
|
|
182
|
+
* @param s - The string to check.
|
|
183
|
+
* @param length - The exact number of characters.
|
|
184
|
+
* @returns `true` if `s.length === length`, `false` otherwise. When `true`,
|
|
185
|
+
* TypeScript narrows `s` to `FixedLengthString<Length> & S`.
|
|
186
|
+
*/
|
|
187
|
+
export const isFixedLengthString = <
|
|
188
|
+
S extends string,
|
|
189
|
+
Length extends SupportedLength,
|
|
190
|
+
>(
|
|
191
|
+
s: S,
|
|
192
|
+
length: Length,
|
|
193
|
+
): s is FixedLengthString<Length> & S => s.length === length;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Casts a string to `MinLengthString<MinLength>` after checking that it has
|
|
197
|
+
* at least `minLength` characters.
|
|
198
|
+
*
|
|
199
|
+
* Use this instead of writing `s as unknown as MinLengthString<N>`: the
|
|
200
|
+
* length is verified at runtime, and the original string type (e.g. literal
|
|
201
|
+
* types) is preserved via intersection.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
*
|
|
205
|
+
* ```ts
|
|
206
|
+
* const password = Str.asMinLengthString('very-secret-password', 12);
|
|
207
|
+
*
|
|
208
|
+
* const nonEmpty: NonEmptyString = password; // OK (>= 1)
|
|
209
|
+
*
|
|
210
|
+
* assert.isTrue(nonEmpty.length >= 12);
|
|
211
|
+
*
|
|
212
|
+
* assert.throws(() => Str.asMinLengthString('short', 12)); // length 5 < 12
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @template S - The input string type (literal types are preserved).
|
|
216
|
+
* @template MinLength - The minimum number of characters (inclusive).
|
|
217
|
+
* @param s - The string to cast.
|
|
218
|
+
* @param minLength - The minimum number of characters (inclusive).
|
|
219
|
+
* @returns `s` typed as `MinLengthString<MinLength> & S`.
|
|
220
|
+
* @throws {TypeError} If `s.length < minLength`.
|
|
221
|
+
*/
|
|
222
|
+
export const asMinLengthString = <
|
|
223
|
+
S extends string,
|
|
224
|
+
MinLength extends SupportedLength,
|
|
225
|
+
>(
|
|
226
|
+
s: S,
|
|
227
|
+
minLength: MinLength,
|
|
228
|
+
): MinLengthString<MinLength> & S => {
|
|
229
|
+
if (!isMinLengthString(s, minLength)) {
|
|
230
|
+
throw new TypeError(
|
|
231
|
+
`Expected a string of length >= ${minLength}, got a string of length ${s.length}`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return s;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Casts a string to `MaxLengthString<MaxLength>` after checking that it has
|
|
240
|
+
* at most `maxLength` characters.
|
|
241
|
+
*
|
|
242
|
+
* Use this instead of writing `s as unknown as MaxLengthString<N>`: the
|
|
243
|
+
* length is verified at runtime, and the original string type (e.g. literal
|
|
244
|
+
* types) is preserved via intersection.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
*
|
|
248
|
+
* ```ts
|
|
249
|
+
* const userName = Str.asMaxLengthString('noshiro', 32);
|
|
250
|
+
*
|
|
251
|
+
* const relaxed: MaxLengthString<64> = userName; // OK (32 <= 64)
|
|
252
|
+
*
|
|
253
|
+
* assert.isTrue(relaxed.length <= 32);
|
|
254
|
+
*
|
|
255
|
+
* assert.throws(() => Str.asMaxLengthString('noshiro', 3)); // length 7 > 3
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* @template S - The input string type (literal types are preserved).
|
|
259
|
+
* @template MaxLength - The maximum number of characters (inclusive).
|
|
260
|
+
* @param s - The string to cast.
|
|
261
|
+
* @param maxLength - The maximum number of characters (inclusive).
|
|
262
|
+
* @returns `s` typed as `MaxLengthString<MaxLength> & S`.
|
|
263
|
+
* @throws {TypeError} If `s.length > maxLength`.
|
|
264
|
+
*/
|
|
265
|
+
export const asMaxLengthString = <
|
|
266
|
+
S extends string,
|
|
267
|
+
MaxLength extends SupportedLength,
|
|
268
|
+
>(
|
|
269
|
+
s: S,
|
|
270
|
+
maxLength: MaxLength,
|
|
271
|
+
): MaxLengthString<MaxLength> & S => {
|
|
272
|
+
if (!isMaxLengthString(s, maxLength)) {
|
|
273
|
+
throw new TypeError(
|
|
274
|
+
`Expected a string of length <= ${maxLength}, got a string of length ${s.length}`,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return s;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Casts a string to `BoundedLengthString<MinLength, MaxLength>` after
|
|
283
|
+
* checking that its length is within the inclusive range
|
|
284
|
+
* `[minLength, maxLength]`.
|
|
285
|
+
*
|
|
286
|
+
* Use this instead of writing `s as unknown as BoundedLengthString<M, N>`:
|
|
287
|
+
* the length is verified at runtime, and the original string type (e.g.
|
|
288
|
+
* literal types) is preserved via intersection.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
*
|
|
292
|
+
* ```ts
|
|
293
|
+
* const userId = Str.asBoundedLengthString('user-12345678', 8, 16);
|
|
294
|
+
*
|
|
295
|
+
* const relaxed: BoundedLengthString<1, 255> = userId; // OK ([8, 16] ⊆ [1, 255])
|
|
296
|
+
*
|
|
297
|
+
* assert.isTrue(relaxed.length >= 8 && relaxed.length <= 16);
|
|
298
|
+
*
|
|
299
|
+
* assert.throws(() => Str.asBoundedLengthString('user', 8, 16)); // length 4 < 8
|
|
300
|
+
* ```
|
|
301
|
+
*
|
|
302
|
+
* @template S - The input string type (literal types are preserved).
|
|
303
|
+
* @template MinLength - The minimum number of characters (inclusive).
|
|
304
|
+
* @template MaxLength - The maximum number of characters (inclusive).
|
|
305
|
+
* @param s - The string to cast.
|
|
306
|
+
* @param minLength - The minimum number of characters (inclusive).
|
|
307
|
+
* @param maxLength - The maximum number of characters (inclusive).
|
|
308
|
+
* @returns `s` typed as `BoundedLengthString<MinLength, MaxLength> & S`.
|
|
309
|
+
* @throws {TypeError} If `s.length < minLength` or `s.length > maxLength`.
|
|
310
|
+
*/
|
|
311
|
+
export const asBoundedLengthString = <
|
|
312
|
+
S extends string,
|
|
313
|
+
MinLength extends SupportedLength,
|
|
314
|
+
MaxLength extends SupportedLength,
|
|
315
|
+
>(
|
|
316
|
+
s: S,
|
|
317
|
+
minLength: MinLength,
|
|
318
|
+
maxLength: MaxLength,
|
|
319
|
+
): BoundedLengthString<MinLength, MaxLength> & S => {
|
|
320
|
+
if (!isBoundedLengthString(s, minLength, maxLength)) {
|
|
321
|
+
throw new TypeError(
|
|
322
|
+
`Expected a string of length in [${minLength}, ${maxLength}], got a string of length ${s.length}`,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return s;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Casts a string to `FixedLengthString<Length>` after checking that it has
|
|
331
|
+
* exactly `length` characters.
|
|
332
|
+
*
|
|
333
|
+
* Use this instead of writing `s as unknown as FixedLengthString<N>`: the
|
|
334
|
+
* length is verified at runtime, and the original string type (e.g. literal
|
|
335
|
+
* types) is preserved via intersection.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
*
|
|
339
|
+
* ```ts
|
|
340
|
+
* const countryCode = Str.asFixedLengthString('JP', 2);
|
|
341
|
+
*
|
|
342
|
+
* const atMost5: MaxLengthString<5> = countryCode; // OK (2 <= 5)
|
|
343
|
+
*
|
|
344
|
+
* assert.isTrue(atMost5.length === 2);
|
|
345
|
+
*
|
|
346
|
+
* assert.throws(() => Str.asFixedLengthString('JP', 3)); // length 2 !== 3
|
|
347
|
+
* ```
|
|
348
|
+
*
|
|
349
|
+
* @template S - The input string type (literal types are preserved).
|
|
350
|
+
* @template Length - The exact number of characters.
|
|
351
|
+
* @param s - The string to cast.
|
|
352
|
+
* @param length - The exact number of characters.
|
|
353
|
+
* @returns `s` typed as `FixedLengthString<Length> & S`.
|
|
354
|
+
* @throws {TypeError} If `s.length !== length`.
|
|
355
|
+
*/
|
|
356
|
+
export const asFixedLengthString = <
|
|
357
|
+
S extends string,
|
|
358
|
+
Length extends SupportedLength,
|
|
359
|
+
>(
|
|
360
|
+
s: S,
|
|
361
|
+
length: Length,
|
|
362
|
+
): FixedLengthString<Length> & S => {
|
|
363
|
+
if (!isFixedLengthString(s, length)) {
|
|
364
|
+
throw new TypeError(
|
|
365
|
+
`Expected a string of length ${length}, got a string of length ${s.length}`,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return s;
|
|
370
|
+
};
|
|
371
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BoundedLengthString,
|
|
3
|
+
type FixedLengthString,
|
|
4
|
+
type MaxLengthString,
|
|
5
|
+
type MinLengthString,
|
|
6
|
+
type NonEmptyString,
|
|
7
|
+
} from 'ts-type-forge';
|
|
8
|
+
import { expectType } from '../expect-type.mjs';
|
|
9
|
+
import { Str } from './str.mjs';
|
|
10
|
+
|
|
11
|
+
describe(Str.isMinLengthString, () => {
|
|
12
|
+
test('should return true when the string is long enough', () => {
|
|
13
|
+
assert.isTrue(Str.isMinLengthString('hello', 3));
|
|
14
|
+
|
|
15
|
+
assert.isTrue(Str.isMinLengthString('hello', 5));
|
|
16
|
+
|
|
17
|
+
assert.isTrue(Str.isMinLengthString('', 0));
|
|
18
|
+
|
|
19
|
+
assert.isTrue(Str.isMinLengthString(' ', 1)); // Space counts
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('should return false when the string is too short', () => {
|
|
23
|
+
assert.isFalse(Str.isMinLengthString('hi', 3));
|
|
24
|
+
|
|
25
|
+
assert.isFalse(Str.isMinLengthString('', 1));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should measure length in UTF-16 code units', () => {
|
|
29
|
+
assert.isTrue(Str.isMinLengthString('🎉', 2)); // Surrogate pair
|
|
30
|
+
|
|
31
|
+
assert.isFalse(Str.isMinLengthString('🎉', 3));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should act as a type guard', () => {
|
|
35
|
+
const value: string = 'very-secret-password';
|
|
36
|
+
|
|
37
|
+
if (Str.isMinLengthString(value, 12)) {
|
|
38
|
+
expectType<typeof value, MinLengthString<12>>('<=');
|
|
39
|
+
|
|
40
|
+
expectType<typeof value, NonEmptyString>('<='); // 12 >= 1
|
|
41
|
+
|
|
42
|
+
expectType<typeof value, MinLengthString<16>>('!<=');
|
|
43
|
+
|
|
44
|
+
expect(value.length).toBeGreaterThanOrEqual(12);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('should preserve literal string types', () => {
|
|
49
|
+
const literal = 'hello';
|
|
50
|
+
|
|
51
|
+
if (Str.isMinLengthString(literal, 3)) {
|
|
52
|
+
expectType<typeof literal, 'hello'>('<=');
|
|
53
|
+
|
|
54
|
+
expectType<typeof literal, MinLengthString<3>>('<=');
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe(Str.isMaxLengthString, () => {
|
|
60
|
+
test('should return true when the string is short enough', () => {
|
|
61
|
+
assert.isTrue(Str.isMaxLengthString('hello', 10));
|
|
62
|
+
|
|
63
|
+
assert.isTrue(Str.isMaxLengthString('hello', 5));
|
|
64
|
+
|
|
65
|
+
assert.isTrue(Str.isMaxLengthString('', 0));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('should return false when the string is too long', () => {
|
|
69
|
+
assert.isFalse(Str.isMaxLengthString('hello', 3));
|
|
70
|
+
|
|
71
|
+
assert.isFalse(Str.isMaxLengthString(' ', 0));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('should measure length in UTF-16 code units', () => {
|
|
75
|
+
assert.isFalse(Str.isMaxLengthString('🎉', 1)); // Surrogate pair
|
|
76
|
+
|
|
77
|
+
assert.isTrue(Str.isMaxLengthString('🎉', 2));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('should act as a type guard', () => {
|
|
81
|
+
const value: string = 'noshiro';
|
|
82
|
+
|
|
83
|
+
if (Str.isMaxLengthString(value, 32)) {
|
|
84
|
+
expectType<typeof value, MaxLengthString<32>>('<=');
|
|
85
|
+
|
|
86
|
+
expectType<typeof value, MaxLengthString<64>>('<='); // 32 <= 64
|
|
87
|
+
|
|
88
|
+
expectType<typeof value, MaxLengthString<16>>('!<=');
|
|
89
|
+
|
|
90
|
+
expect(value.length).toBeLessThanOrEqual(32);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe(Str.isBoundedLengthString, () => {
|
|
96
|
+
test('should return true when the length is within the range', () => {
|
|
97
|
+
assert.isTrue(Str.isBoundedLengthString('user-12345678', 8, 16));
|
|
98
|
+
|
|
99
|
+
assert.isTrue(Str.isBoundedLengthString('12345678', 8, 16)); // Lower bound (inclusive)
|
|
100
|
+
|
|
101
|
+
assert.isTrue(Str.isBoundedLengthString('1234567890123456', 8, 16)); // Upper bound (inclusive)
|
|
102
|
+
|
|
103
|
+
assert.isTrue(Str.isBoundedLengthString('', 0, 0));
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('should return false when the length is out of range', () => {
|
|
107
|
+
assert.isFalse(Str.isBoundedLengthString('user', 8, 16));
|
|
108
|
+
|
|
109
|
+
assert.isFalse(Str.isBoundedLengthString('12345678901234567', 8, 16));
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('should act as a type guard', () => {
|
|
113
|
+
const value: string = 'user-12345678';
|
|
114
|
+
|
|
115
|
+
if (Str.isBoundedLengthString(value, 8, 16)) {
|
|
116
|
+
expectType<typeof value, BoundedLengthString<8, 16>>('<=');
|
|
117
|
+
|
|
118
|
+
expectType<typeof value, BoundedLengthString<1, 255>>('<='); // [8, 16] ⊆ [1, 255]
|
|
119
|
+
|
|
120
|
+
expectType<typeof value, MinLengthString<8>>('<=');
|
|
121
|
+
|
|
122
|
+
expectType<typeof value, MaxLengthString<16>>('<=');
|
|
123
|
+
|
|
124
|
+
expectType<typeof value, NonEmptyString>('<='); // 8 >= 1
|
|
125
|
+
|
|
126
|
+
expectType<typeof value, BoundedLengthString<10, 16>>('!<=');
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe(Str.isFixedLengthString, () => {
|
|
132
|
+
test('should return true only for the exact length', () => {
|
|
133
|
+
assert.isTrue(Str.isFixedLengthString('JP', 2));
|
|
134
|
+
|
|
135
|
+
assert.isTrue(Str.isFixedLengthString('', 0));
|
|
136
|
+
|
|
137
|
+
assert.isFalse(Str.isFixedLengthString('JPN', 2));
|
|
138
|
+
|
|
139
|
+
assert.isFalse(Str.isFixedLengthString('J', 2));
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test('should measure length in UTF-16 code units', () => {
|
|
143
|
+
assert.isTrue(Str.isFixedLengthString('🎉', 2)); // Surrogate pair
|
|
144
|
+
|
|
145
|
+
assert.isFalse(Str.isFixedLengthString('🎉', 1));
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('should act as a type guard', () => {
|
|
149
|
+
const value: string = 'JP';
|
|
150
|
+
|
|
151
|
+
if (Str.isFixedLengthString(value, 2)) {
|
|
152
|
+
expectType<typeof value, FixedLengthString<2>>('<=');
|
|
153
|
+
|
|
154
|
+
expectType<typeof value, BoundedLengthString<2, 2>>('<=');
|
|
155
|
+
|
|
156
|
+
expectType<typeof value, MaxLengthString<5>>('<='); // 2 <= 5
|
|
157
|
+
|
|
158
|
+
expectType<typeof value, MinLengthString<1>>('<='); // 2 >= 1
|
|
159
|
+
|
|
160
|
+
expectType<typeof value, NonEmptyString>('<=');
|
|
161
|
+
|
|
162
|
+
expectType<typeof value, FixedLengthString<3>>('!<=');
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('Str length-bounded casts', () => {
|
|
168
|
+
test('asMinLengthString returns the value and narrows its type', () => {
|
|
169
|
+
const result = Str.asMinLengthString('very-secret', 3);
|
|
170
|
+
|
|
171
|
+
expectType<typeof result, MinLengthString<3>>('<=');
|
|
172
|
+
|
|
173
|
+
assert.strictEqual(result, 'very-secret');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('asMinLengthString throws when too short', () => {
|
|
177
|
+
assert.throws(() => Str.asMinLengthString('hi', 3), TypeError);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('asMaxLengthString returns the value and narrows its type', () => {
|
|
181
|
+
const result = Str.asMaxLengthString('noshiro', 32);
|
|
182
|
+
|
|
183
|
+
expectType<typeof result, MaxLengthString<32>>('<=');
|
|
184
|
+
|
|
185
|
+
assert.strictEqual(result, 'noshiro');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
test('asMaxLengthString throws when too long', () => {
|
|
189
|
+
assert.throws(() => Str.asMaxLengthString('noshiro', 3), TypeError);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('asBoundedLengthString returns the value and narrows its type', () => {
|
|
193
|
+
const result = Str.asBoundedLengthString('user-12345678', 8, 16);
|
|
194
|
+
|
|
195
|
+
expectType<typeof result, BoundedLengthString<8, 16>>('<=');
|
|
196
|
+
|
|
197
|
+
assert.strictEqual(result, 'user-12345678');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('asBoundedLengthString throws when out of range', () => {
|
|
201
|
+
assert.throws(() => Str.asBoundedLengthString('user', 8, 16), TypeError);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('asFixedLengthString returns the value and narrows its type', () => {
|
|
205
|
+
const result = Str.asFixedLengthString('JP', 2);
|
|
206
|
+
|
|
207
|
+
expectType<typeof result, FixedLengthString<2>>('<=');
|
|
208
|
+
|
|
209
|
+
assert.strictEqual(result, 'JP');
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test('asFixedLengthString throws when the length differs', () => {
|
|
213
|
+
assert.throws(() => Str.asFixedLengthString('JP', 3), TypeError);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"as-length-bounded-array.d.mts","sourceRoot":"","sources":["../../src/guard/as-length-bounded-array.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAQvB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,kBAAkB,GAC7B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,MAAM,SAAS,eAAe,MAE1B,EAAE,UACE,MAAM,KACb,gBAAgB,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAQzC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,gBAAgB,GAC3B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,SAAS,SAAS,eAAe,MAE7B,EAAE,aACK,SAAS,KACnB,cAAc,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAQ1C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,gBAAgB,GAC3B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,SAAS,SAAS,eAAe,MAE7B,EAAE,aACK,SAAS,KACnB,cAAc,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAQ1C,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,eAAO,MAAM,oBAAoB,GAC/B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,SAAS,SAAS,eAAe,EACjC,SAAS,SAAS,eAAe,MAE7B,EAAE,aACK,SAAS,aACT,SAAS,KACnB,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAQzD,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"as-length-bounded-array.mjs","sources":["../../src/guard/as-length-bounded-array.mts"],"sourcesContent":["import {\n type BoundedLengthArray,\n type FixedLengthArray,\n type MaxLengthArray,\n type MinLengthArray,\n type SupportedLength,\n} from 'ts-type-forge';\nimport {\n isBoundedLengthArray,\n isFixedLengthArray,\n isMaxLengthArray,\n isMinLengthArray,\n} from './is-length-bounded-array.mjs';\n\n/**\n * Casts an array to `FixedLengthArray<Length, E>` after checking that it has\n * exactly `length` elements.\n *\n * Use this instead of writing `xs as unknown as FixedLengthArray<N, E>`: the\n * length is verified at runtime, and the original array type (e.g. tuple\n * types) is preserved via intersection, so the result also benefits from the\n * structural tuple prefix (in-range indexed access without `undefined` under\n * `noUncheckedIndexedAccess`).\n *\n * @example\n *\n * ```ts\n * const rgb = asFixedLengthArray([255, 128, 0], 3);\n *\n * const atMost5: MaxLengthArray<5, number> = rgb; // OK (3 <= 5)\n * const red: number = rgb[0]; // OK — no `undefined`\n *\n * // asFixedLengthArray([255, 128], 3); // throws TypeError\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template Length - The exact number of elements.\n * @param xs - The array to cast.\n * @param length - The exact number of elements.\n * @returns `xs` typed as `FixedLengthArray<Length, Xs[number]> & Xs`.\n * @throws {TypeError} If `xs.length !== length`.\n */\nexport const asFixedLengthArray = <\n Xs extends readonly unknown[],\n Length extends SupportedLength,\n>(\n xs: Xs,\n length: Length,\n): FixedLengthArray<Length, Xs[number]> & Xs => {\n if (!isFixedLengthArray(xs, length)) {\n throw new TypeError(\n `Expected an array of length ${length}, got an array of length ${xs.length}`,\n );\n }\n\n return xs;\n};\n\n/**\n * Casts an array to `MinLengthArray<MinLength, E>` after checking that it has\n * at least `minLength` elements.\n *\n * Use this instead of writing `xs as unknown as MinLengthArray<N, E>`: the\n * length is verified at runtime, and the original array type (e.g. tuple\n * types) is preserved via intersection, so the result also benefits from the\n * structural tuple prefix (indexed access below `min(N, 10)` without\n * `undefined` under `noUncheckedIndexedAccess`).\n *\n * @example\n *\n * ```ts\n * const history = asMinLengthArray([0, 1, 2, 3], 3);\n *\n * const nonEmpty: MinLengthArray<1, number> = history; // OK (3 >= 1)\n * const first: number = history[0]; // OK — no `undefined`\n *\n * // asMinLengthArray([0], 3); // throws TypeError\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MinLength - The minimum number of elements (inclusive).\n * @param xs - The array to cast.\n * @param minLength - The minimum number of elements (inclusive).\n * @returns `xs` typed as `MinLengthArray<MinLength, Xs[number]> & Xs`.\n * @throws {TypeError} If `xs.length < minLength`.\n */\nexport const asMinLengthArray = <\n Xs extends readonly unknown[],\n MinLength extends SupportedLength,\n>(\n xs: Xs,\n minLength: MinLength,\n): MinLengthArray<MinLength, Xs[number]> & Xs => {\n if (!isMinLengthArray(xs, minLength)) {\n throw new TypeError(\n `Expected an array of length >= ${minLength}, got an array of length ${xs.length}`,\n );\n }\n\n return xs;\n};\n\n/**\n * Casts an array to `MaxLengthArray<MaxLength, E>` after checking that it has\n * at most `maxLength` elements.\n *\n * Use this instead of writing `xs as unknown as MaxLengthArray<N, E>`: the\n * length is verified at runtime, and the original array type (e.g. tuple\n * types) is preserved via intersection.\n *\n * @example\n *\n * ```ts\n * const tags = asMaxLengthArray(['a', 'b', 'c'], 8);\n *\n * const relaxed: MaxLengthArray<16, string> = tags; // OK (8 <= 16)\n *\n * // asMaxLengthArray(['a', 'b', 'c'], 2); // throws TypeError\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @param xs - The array to cast.\n * @param maxLength - The maximum number of elements (inclusive).\n * @returns `xs` typed as `MaxLengthArray<MaxLength, Xs[number]> & Xs`.\n * @throws {TypeError} If `xs.length > maxLength`.\n */\nexport const asMaxLengthArray = <\n Xs extends readonly unknown[],\n MaxLength extends SupportedLength,\n>(\n xs: Xs,\n maxLength: MaxLength,\n): MaxLengthArray<MaxLength, Xs[number]> & Xs => {\n if (!isMaxLengthArray(xs, maxLength)) {\n throw new TypeError(\n `Expected an array of length <= ${maxLength}, got an array of length ${xs.length}`,\n );\n }\n\n return xs;\n};\n\n/**\n * Casts an array to `BoundedLengthArray<MinLength, MaxLength, E>` after\n * checking that its length is within the inclusive range\n * `[minLength, maxLength]`.\n *\n * Use this instead of writing\n * `xs as unknown as BoundedLengthArray<Min, Max, E>`: the length is verified\n * at runtime, and the original array type (e.g. tuple types) is preserved via\n * intersection.\n *\n * @example\n *\n * ```ts\n * const selection = asBoundedLengthArray([1, 2, 3], 1, 5);\n *\n * const relaxed: BoundedLengthArray<0, 100, number> = selection; // OK\n *\n * // asBoundedLengthArray([], 1, 5); // throws TypeError\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MinLength - The minimum number of elements (inclusive).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @param xs - The array to cast.\n * @param minLength - The minimum number of elements (inclusive).\n * @param maxLength - The maximum number of elements (inclusive).\n * @returns `xs` typed as\n * `BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs`.\n * @throws {TypeError} If `xs.length < minLength || xs.length > maxLength`.\n */\nexport const asBoundedLengthArray = <\n Xs extends readonly unknown[],\n MinLength extends SupportedLength,\n MaxLength extends SupportedLength,\n>(\n xs: Xs,\n minLength: MinLength,\n maxLength: MaxLength,\n): BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs => {\n if (!isBoundedLengthArray(xs, minLength, maxLength)) {\n throw new TypeError(\n `Expected an array of length in [${minLength}, ${maxLength}], got an array of length ${xs.length}`,\n );\n }\n\n return xs;\n};\n"],"names":[],"mappings":";;AA0CO,MAAM,kBAAA,GAAqB,CAIhC,EAAA,EACA,MAAA,KAC8C;AAC9C,EAAA,IAAI,CAAC,kBAAA,CAAmB,EAAA,EAAI,MAAM,CAAA,EAAG;AACnC,IAAA,MAAM,IAAI,SAAA;AAAA,MACR,CAAA,4BAAA,EAA+B,MAAM,CAAA,yBAAA,EAA4B,EAAA,CAAG,MAAM,CAAA;AAAA,KAC5E;AAAA,EACF;AAEA,EAAA,OAAO,EAAA;AACT;AA8BO,MAAM,gBAAA,GAAmB,CAI9B,EAAA,EACA,SAAA,KAC+C;AAC/C,EAAA,IAAI,CAAC,gBAAA,CAAiB,EAAA,EAAI,SAAS,CAAA,EAAG;AACpC,IAAA,MAAM,IAAI,SAAA;AAAA,MACR,CAAA,+BAAA,EAAkC,SAAS,CAAA,yBAAA,EAA4B,EAAA,CAAG,MAAM,CAAA;AAAA,KAClF;AAAA,EACF;AAEA,EAAA,OAAO,EAAA;AACT;AA2BO,MAAM,gBAAA,GAAmB,CAI9B,EAAA,EACA,SAAA,KAC+C;AAC/C,EAAA,IAAI,CAAC,gBAAA,CAAiB,EAAA,EAAI,SAAS,CAAA,EAAG;AACpC,IAAA,MAAM,IAAI,SAAA;AAAA,MACR,CAAA,+BAAA,EAAkC,SAAS,CAAA,yBAAA,EAA4B,EAAA,CAAG,MAAM,CAAA;AAAA,KAClF;AAAA,EACF;AAEA,EAAA,OAAO,EAAA;AACT;AAgCO,MAAM,oBAAA,GAAuB,CAKlC,EAAA,EACA,SAAA,EACA,SAAA,KAC8D;AAC9D,EAAA,IAAI,CAAC,oBAAA,CAAqB,EAAA,EAAI,SAAA,EAAW,SAAS,CAAA,EAAG;AACnD,IAAA,MAAM,IAAI,SAAA;AAAA,MACR,mCAAmC,SAAS,CAAA,EAAA,EAAK,SAAS,CAAA,0BAAA,EAA6B,GAAG,MAAM,CAAA;AAAA,KAClG;AAAA,EACF;AAEA,EAAA,OAAO,EAAA;AACT;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-length-bounded-array.d.mts","sourceRoot":"","sources":["../../src/guard/is-length-bounded-array.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,gBAAgB,GAC3B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,SAAS,SAAS,eAAe,MAE7B,EAAE,aACK,SAAS,KACnB,EAAE,IAAI,cAAc,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAA4B,CAAC;AAE9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,eAAO,MAAM,gBAAgB,GAC3B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,SAAS,SAAS,eAAe,MAE7B,EAAE,aACK,SAAS,KACnB,EAAE,IAAI,cAAc,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAA4B,CAAC;AAE9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,eAAO,MAAM,oBAAoB,GAC/B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,SAAS,SAAS,eAAe,EACjC,SAAS,SAAS,eAAe,MAE7B,EAAE,aACK,SAAS,aACT,SAAS,KACnB,EAAE,IAAI,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EACd,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,eAAO,MAAM,kBAAkB,GAC7B,EAAE,SAAS,SAAS,OAAO,EAAE,EAC7B,MAAM,SAAS,eAAe,MAE1B,EAAE,UACE,MAAM,KACb,EAAE,IAAI,gBAAgB,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,EAA0B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-length-bounded-array.mjs","sources":["../../src/guard/is-length-bounded-array.mts"],"sourcesContent":["import {\n type BoundedLengthArray,\n type FixedLengthArray,\n type MaxLengthArray,\n type MinLengthArray,\n type SupportedLength,\n} from 'ts-type-forge';\n\n/**\n * Type guard that checks if an array has at least `minLength` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MinLengthArray<MinLength, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isMinLengthTuple`, which narrows to a structural tuple type.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MinLengthArray<3, E>` is assignable to\n * `MinLengthArray<1, E>`.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [0, 1, 2, 3];\n *\n * assert.isTrue(isMinLengthArray(input, 3));\n *\n * assert.isFalse(isMinLengthArray([0], 3));\n *\n * if (isMinLengthArray(input, 3)) {\n * const history: MinLengthArray<3, number> = input;\n *\n * const nonEmpty: MinLengthArray<1, number> = input; // OK (3 >= 1)\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MinLength - The minimum number of elements (inclusive).\n * @param xs - The array to check.\n * @param minLength - The minimum number of elements (inclusive).\n * @returns `true` if `xs.length >= minLength`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `MinLengthArray<MinLength, Xs[number]> & Xs`.\n */\nexport const isMinLengthArray = <\n Xs extends readonly unknown[],\n MinLength extends SupportedLength,\n>(\n xs: Xs,\n minLength: MinLength,\n): xs is MinLengthArray<MinLength, Xs[number]> & Xs => xs.length >= minLength;\n\n/**\n * Type guard that checks if an array has at most `maxLength` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MaxLengthArray<MaxLength, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isMaxLengthTuple`, which narrows to a union of tuple types.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MaxLengthArray<8, E>` is assignable to\n * `MaxLengthArray<16, E>`.\n *\n * @example\n *\n * ```ts\n * const input: readonly string[] = ['a', 'b', 'c'];\n *\n * assert.isTrue(isMaxLengthArray(input, 8));\n *\n * assert.isFalse(isMaxLengthArray(input, 2));\n *\n * if (isMaxLengthArray(input, 8)) {\n * const tags: MaxLengthArray<8, string> = input;\n *\n * const relaxed: MaxLengthArray<16, string> = input; // OK (8 <= 16)\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @param xs - The array to check.\n * @param maxLength - The maximum number of elements (inclusive).\n * @returns `true` if `xs.length <= maxLength`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `MaxLengthArray<MaxLength, Xs[number]> & Xs`.\n */\nexport const isMaxLengthArray = <\n Xs extends readonly unknown[],\n MaxLength extends SupportedLength,\n>(\n xs: Xs,\n maxLength: MaxLength,\n): xs is MaxLengthArray<MaxLength, Xs[number]> & Xs => xs.length <= maxLength;\n\n/**\n * Type guard that checks if an array's length is within the inclusive range\n * `[minLength, maxLength]`.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `BoundedLengthArray<MinLength, MaxLength, E>` while\n * preserving the original array type (e.g. tuple types) via intersection.\n * - Since `BoundedLengthArray` is the intersection of `MinLengthArray` and\n * `MaxLengthArray`, the narrowed value is assignable to both, and both\n * bounds can be weakened independently.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isBoundedLengthTuple`, which narrows to a union of tuple types.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [1, 2, 3];\n *\n * assert.isTrue(isBoundedLengthArray(input, 1, 5));\n *\n * assert.isFalse(isBoundedLengthArray([], 1, 5));\n *\n * if (isBoundedLengthArray(input, 1, 5)) {\n * const selection: BoundedLengthArray<1, 5, number> = input;\n *\n * const relaxed: BoundedLengthArray<0, 100, number> = input; // OK ([1, 5] ⊆ [0, 100])\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template MinLength - The minimum number of elements (inclusive).\n * @template MaxLength - The maximum number of elements (inclusive).\n * @param xs - The array to check.\n * @param minLength - The minimum number of elements (inclusive).\n * @param maxLength - The maximum number of elements (inclusive).\n * @returns `true` if `minLength <= xs.length && xs.length <= maxLength`,\n * `false` otherwise. When `true`, TypeScript narrows `xs` to\n * `BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs`.\n */\nexport const isBoundedLengthArray = <\n Xs extends readonly unknown[],\n MinLength extends SupportedLength,\n MaxLength extends SupportedLength,\n>(\n xs: Xs,\n minLength: MinLength,\n maxLength: MaxLength,\n): xs is BoundedLengthArray<MinLength, MaxLength, Xs[number]> & Xs =>\n xs.length >= minLength && xs.length <= maxLength;\n\n/**\n * Type guard that checks if an array has exactly `length` elements.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `FixedLengthArray<Length, E>` while preserving the\n * original array type (e.g. tuple types) via intersection.\n * - Since `FixedLengthArray<Length, E>` is defined as\n * `BoundedLengthArray<Length, Length, E>`, the narrowed value is assignable\n * to any `MinLengthArray<N, E>` with `N <= Length` and any\n * `MaxLengthArray<N, E>` with `N >= Length`.\n * - The length constraint lives only in the brand, so this stays cheap for the\n * type checker even when the element type is large — unlike\n * `isFixedLengthTuple`, which narrows to a structural tuple type.\n *\n * @example\n *\n * ```ts\n * const input: readonly number[] = [255, 128, 0];\n *\n * assert.isTrue(isFixedLengthArray(input, 3));\n *\n * assert.isFalse(isFixedLengthArray(input, 4));\n *\n * if (isFixedLengthArray(input, 3)) {\n * const rgb: FixedLengthArray<3, number> = input;\n *\n * const atMost5: MaxLengthArray<5, number> = input; // OK (3 <= 5)\n * }\n * ```\n *\n * @template Xs - The input array type (tuple types are preserved).\n * @template Length - The exact number of elements.\n * @param xs - The array to check.\n * @param length - The exact number of elements.\n * @returns `true` if `xs.length === length`, `false` otherwise. When `true`,\n * TypeScript narrows `xs` to `FixedLengthArray<Length, Xs[number]> & Xs`.\n */\nexport const isFixedLengthArray = <\n Xs extends readonly unknown[],\n Length extends SupportedLength,\n>(\n xs: Xs,\n length: Length,\n): xs is FixedLengthArray<Length, Xs[number]> & Xs => xs.length === length;\n"],"names":[],"mappings":"AA6CO,MAAM,gBAAA,GAAmB,CAI9B,EAAA,EACA,SAAA,KACqD,GAAG,MAAA,IAAU;AAuC7D,MAAM,gBAAA,GAAmB,CAI9B,EAAA,EACA,SAAA,KACqD,GAAG,MAAA,IAAU;AA2C7D,MAAM,oBAAA,GAAuB,CAKlC,EAAA,EACA,SAAA,EACA,cAEA,EAAA,CAAG,MAAA,IAAU,SAAA,IAAa,EAAA,CAAG,MAAA,IAAU;AAwClC,MAAM,kBAAA,GAAqB,CAIhC,EAAA,EACA,MAAA,KACoD,GAAG,MAAA,KAAW;;;;"}
|