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
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { type BoundedLengthString, type FixedLengthString, type MaxLengthString, type MinLengthString, type SupportedLength } from 'ts-type-forge';
|
|
2
|
-
/**
|
|
3
|
-
* Type guard that checks if a string has at least `minLength` characters.
|
|
4
|
-
*
|
|
5
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
6
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
7
|
-
* Plane (e.g. emoji) count as 2.
|
|
8
|
-
*
|
|
9
|
-
* **Type Narrowing Behavior:**
|
|
10
|
-
*
|
|
11
|
-
* - Narrows the input to `MinLengthString<MinLength>` while preserving the
|
|
12
|
-
* original string type (e.g. literal types) via intersection.
|
|
13
|
-
* - The result participates in the length-constraint subtyping relation: for
|
|
14
|
-
* example, a value narrowed to `MinLengthString<3>` is assignable to
|
|
15
|
-
* `MinLengthString<1>` (= `NonEmptyString`).
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
*
|
|
19
|
-
* ```ts
|
|
20
|
-
* const input: string = 'very-secret-password';
|
|
21
|
-
*
|
|
22
|
-
* assert.isTrue(isMinLengthString(input, 12));
|
|
23
|
-
*
|
|
24
|
-
* assert.isFalse(isMinLengthString('short', 12));
|
|
25
|
-
*
|
|
26
|
-
* if (isMinLengthString(input, 12)) {
|
|
27
|
-
* const password: MinLengthString<12> = input;
|
|
28
|
-
*
|
|
29
|
-
* const nonEmpty: NonEmptyString = input; // OK (12 >= 1)
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @template S - The input string type (literal types are preserved).
|
|
34
|
-
* @template MinLength - The minimum number of characters (inclusive).
|
|
35
|
-
* @param s - The string to check.
|
|
36
|
-
* @param minLength - The minimum number of characters (inclusive).
|
|
37
|
-
* @returns `true` if `s.length >= minLength`, `false` otherwise. When `true`,
|
|
38
|
-
* TypeScript narrows `s` to `MinLengthString<MinLength> & S`.
|
|
39
|
-
*/
|
|
40
|
-
export declare const isMinLengthString: <S extends string, MinLength extends SupportedLength>(s: S, minLength: MinLength) => s is MinLengthString<MinLength> & S;
|
|
41
|
-
/**
|
|
42
|
-
* Type guard that checks if a string has at most `maxLength` characters.
|
|
43
|
-
*
|
|
44
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
45
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
46
|
-
* Plane (e.g. emoji) count as 2.
|
|
47
|
-
*
|
|
48
|
-
* **Type Narrowing Behavior:**
|
|
49
|
-
*
|
|
50
|
-
* - Narrows the input to `MaxLengthString<MaxLength>` while preserving the
|
|
51
|
-
* original string type (e.g. literal types) via intersection.
|
|
52
|
-
* - The result participates in the length-constraint subtyping relation: for
|
|
53
|
-
* example, a value narrowed to `MaxLengthString<16>` is assignable to
|
|
54
|
-
* `MaxLengthString<32>`.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
*
|
|
58
|
-
* ```ts
|
|
59
|
-
* const input: string = 'noshiro';
|
|
60
|
-
*
|
|
61
|
-
* assert.isTrue(isMaxLengthString(input, 32));
|
|
62
|
-
*
|
|
63
|
-
* assert.isFalse(isMaxLengthString(input, 3));
|
|
64
|
-
*
|
|
65
|
-
* if (isMaxLengthString(input, 32)) {
|
|
66
|
-
* const userName: MaxLengthString<32> = input;
|
|
67
|
-
*
|
|
68
|
-
* const relaxed: MaxLengthString<64> = input; // OK (32 <= 64)
|
|
69
|
-
* }
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* @template S - The input string type (literal types are preserved).
|
|
73
|
-
* @template MaxLength - The maximum number of characters (inclusive).
|
|
74
|
-
* @param s - The string to check.
|
|
75
|
-
* @param maxLength - The maximum number of characters (inclusive).
|
|
76
|
-
* @returns `true` if `s.length <= maxLength`, `false` otherwise. When `true`,
|
|
77
|
-
* TypeScript narrows `s` to `MaxLengthString<MaxLength> & S`.
|
|
78
|
-
*/
|
|
79
|
-
export declare const isMaxLengthString: <S extends string, MaxLength extends SupportedLength>(s: S, maxLength: MaxLength) => s is MaxLengthString<MaxLength> & S;
|
|
80
|
-
/**
|
|
81
|
-
* Type guard that checks if a string's length is within the inclusive range
|
|
82
|
-
* `[minLength, maxLength]`.
|
|
83
|
-
*
|
|
84
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
85
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
86
|
-
* Plane (e.g. emoji) count as 2.
|
|
87
|
-
*
|
|
88
|
-
* **Type Narrowing Behavior:**
|
|
89
|
-
*
|
|
90
|
-
* - Narrows the input to `BoundedLengthString<MinLength, MaxLength>` while
|
|
91
|
-
* preserving the original string type (e.g. literal types) via intersection.
|
|
92
|
-
* - Since `BoundedLengthString` is the intersection of `MinLengthString` and
|
|
93
|
-
* `MaxLengthString`, the narrowed value is assignable to both, and both
|
|
94
|
-
* bounds can be weakened independently.
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
*
|
|
98
|
-
* ```ts
|
|
99
|
-
* const input: string = 'user-12345678';
|
|
100
|
-
*
|
|
101
|
-
* assert.isTrue(isBoundedLengthString(input, 8, 16));
|
|
102
|
-
*
|
|
103
|
-
* assert.isFalse(isBoundedLengthString('user', 8, 16));
|
|
104
|
-
*
|
|
105
|
-
* if (isBoundedLengthString(input, 8, 16)) {
|
|
106
|
-
* const userId: BoundedLengthString<8, 16> = input;
|
|
107
|
-
*
|
|
108
|
-
* const relaxed: BoundedLengthString<1, 255> = input; // OK ([8, 16] ⊆ [1, 255])
|
|
109
|
-
* }
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* @template S - The input string type (literal types are preserved).
|
|
113
|
-
* @template MinLength - The minimum number of characters (inclusive).
|
|
114
|
-
* @template MaxLength - The maximum number of characters (inclusive).
|
|
115
|
-
* @param s - The string to check.
|
|
116
|
-
* @param minLength - The minimum number of characters (inclusive).
|
|
117
|
-
* @param maxLength - The maximum number of characters (inclusive).
|
|
118
|
-
* @returns `true` if `minLength <= s.length && s.length <= maxLength`, `false`
|
|
119
|
-
* otherwise. When `true`, TypeScript narrows `s` to
|
|
120
|
-
* `BoundedLengthString<MinLength, MaxLength> & S`.
|
|
121
|
-
*/
|
|
122
|
-
export declare const isBoundedLengthString: <S extends string, MinLength extends SupportedLength, MaxLength extends SupportedLength>(s: S, minLength: MinLength, maxLength: MaxLength) => s is BoundedLengthString<MinLength, MaxLength> & S;
|
|
123
|
-
/**
|
|
124
|
-
* Type guard that checks if a string has exactly `length` characters.
|
|
125
|
-
*
|
|
126
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
127
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
128
|
-
* Plane (e.g. emoji) count as 2.
|
|
129
|
-
*
|
|
130
|
-
* **Type Narrowing Behavior:**
|
|
131
|
-
*
|
|
132
|
-
* - Narrows the input to `FixedLengthString<Length>` while preserving the
|
|
133
|
-
* original string type (e.g. literal types) via intersection.
|
|
134
|
-
* - Since `FixedLengthString<Length>` is defined as
|
|
135
|
-
* `BoundedLengthString<Length, Length>`, the narrowed value is assignable to
|
|
136
|
-
* any `MinLengthString<N>` with `N <= Length` and any `MaxLengthString<N>`
|
|
137
|
-
* with `N >= Length`.
|
|
138
|
-
*
|
|
139
|
-
* @example
|
|
140
|
-
*
|
|
141
|
-
* ```ts
|
|
142
|
-
* const input: string = 'JP';
|
|
143
|
-
*
|
|
144
|
-
* assert.isTrue(isFixedLengthString(input, 2));
|
|
145
|
-
*
|
|
146
|
-
* assert.isFalse(isFixedLengthString(input, 3));
|
|
147
|
-
*
|
|
148
|
-
* if (isFixedLengthString(input, 2)) {
|
|
149
|
-
* const countryCode: FixedLengthString<2> = input;
|
|
150
|
-
*
|
|
151
|
-
* const atMost5: MaxLengthString<5> = input; // OK (2 <= 5)
|
|
152
|
-
* }
|
|
153
|
-
* ```
|
|
154
|
-
*
|
|
155
|
-
* @template S - The input string type (literal types are preserved).
|
|
156
|
-
* @template Length - The exact number of characters.
|
|
157
|
-
* @param s - The string to check.
|
|
158
|
-
* @param length - The exact number of characters.
|
|
159
|
-
* @returns `true` if `s.length === length`, `false` otherwise. When `true`,
|
|
160
|
-
* TypeScript narrows `s` to `FixedLengthString<Length> & S`.
|
|
161
|
-
*/
|
|
162
|
-
export declare const isFixedLengthString: <S extends string, Length extends SupportedLength>(s: S, length: Length) => s is FixedLengthString<Length> & S;
|
|
163
|
-
//# sourceMappingURL=is-length-bounded-string.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-length-bounded-string.d.mts","sourceRoot":"","sources":["../../src/guard/is-length-bounded-string.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAEvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,eAAO,MAAM,iBAAiB,GAC5B,CAAC,SAAS,MAAM,EAChB,SAAS,SAAS,eAAe,KAE9B,CAAC,aACO,SAAS,KACnB,CAAC,IAAI,eAAe,CAAC,SAAS,CAAC,GAAG,CAA0B,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,eAAO,MAAM,iBAAiB,GAC5B,CAAC,SAAS,MAAM,EAChB,SAAS,SAAS,eAAe,KAE9B,CAAC,aACO,SAAS,KACnB,CAAC,IAAI,eAAe,CAAC,SAAS,CAAC,GAAG,CAA0B,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,eAAO,MAAM,qBAAqB,GAChC,CAAC,SAAS,MAAM,EAChB,SAAS,SAAS,eAAe,EACjC,SAAS,SAAS,eAAe,KAE9B,CAAC,aACO,SAAS,aACT,SAAS,KACnB,CAAC,IAAI,mBAAmB,CAAC,SAAS,EAAE,SAAS,CAAC,GAAG,CACJ,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,eAAO,MAAM,mBAAmB,GAC9B,CAAC,SAAS,MAAM,EAChB,MAAM,SAAS,eAAe,KAE3B,CAAC,UACI,MAAM,KACb,CAAC,IAAI,iBAAiB,CAAC,MAAM,CAAC,GAAG,CAAwB,CAAC"}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
const isMinLengthString = (s, minLength) => s.length >= minLength;
|
|
2
|
-
const isMaxLengthString = (s, maxLength) => s.length <= maxLength;
|
|
3
|
-
const isBoundedLengthString = (s, minLength, maxLength) => s.length >= minLength && s.length <= maxLength;
|
|
4
|
-
const isFixedLengthString = (s, length) => s.length === length;
|
|
5
|
-
|
|
6
|
-
export { isBoundedLengthString, isFixedLengthString, isMaxLengthString, isMinLengthString };
|
|
7
|
-
//# sourceMappingURL=is-length-bounded-string.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"is-length-bounded-string.mjs","sources":["../../src/guard/is-length-bounded-string.mts"],"sourcesContent":["import {\n type BoundedLengthString,\n type FixedLengthString,\n type MaxLengthString,\n type MinLengthString,\n type SupportedLength,\n} from 'ts-type-forge';\n\n/**\n * Type guard that checks if a string has at least `minLength` characters.\n *\n * The length is measured in UTF-16 code units (the same unit as\n * `String.prototype.length`), so characters outside the Basic Multilingual\n * Plane (e.g. emoji) count as 2.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MinLengthString<MinLength>` while preserving the\n * original string type (e.g. literal types) via intersection.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MinLengthString<3>` is assignable to\n * `MinLengthString<1>` (= `NonEmptyString`).\n *\n * @example\n *\n * ```ts\n * const input: string = 'very-secret-password';\n *\n * assert.isTrue(isMinLengthString(input, 12));\n *\n * assert.isFalse(isMinLengthString('short', 12));\n *\n * if (isMinLengthString(input, 12)) {\n * const password: MinLengthString<12> = input;\n *\n * const nonEmpty: NonEmptyString = input; // OK (12 >= 1)\n * }\n * ```\n *\n * @template S - The input string type (literal types are preserved).\n * @template MinLength - The minimum number of characters (inclusive).\n * @param s - The string to check.\n * @param minLength - The minimum number of characters (inclusive).\n * @returns `true` if `s.length >= minLength`, `false` otherwise. When `true`,\n * TypeScript narrows `s` to `MinLengthString<MinLength> & S`.\n */\nexport const isMinLengthString = <\n S extends string,\n MinLength extends SupportedLength,\n>(\n s: S,\n minLength: MinLength,\n): s is MinLengthString<MinLength> & S => s.length >= minLength;\n\n/**\n * Type guard that checks if a string has at most `maxLength` characters.\n *\n * The length is measured in UTF-16 code units (the same unit as\n * `String.prototype.length`), so characters outside the Basic Multilingual\n * Plane (e.g. emoji) count as 2.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `MaxLengthString<MaxLength>` while preserving the\n * original string type (e.g. literal types) via intersection.\n * - The result participates in the length-constraint subtyping relation: for\n * example, a value narrowed to `MaxLengthString<16>` is assignable to\n * `MaxLengthString<32>`.\n *\n * @example\n *\n * ```ts\n * const input: string = 'noshiro';\n *\n * assert.isTrue(isMaxLengthString(input, 32));\n *\n * assert.isFalse(isMaxLengthString(input, 3));\n *\n * if (isMaxLengthString(input, 32)) {\n * const userName: MaxLengthString<32> = input;\n *\n * const relaxed: MaxLengthString<64> = input; // OK (32 <= 64)\n * }\n * ```\n *\n * @template S - The input string type (literal types are preserved).\n * @template MaxLength - The maximum number of characters (inclusive).\n * @param s - The string to check.\n * @param maxLength - The maximum number of characters (inclusive).\n * @returns `true` if `s.length <= maxLength`, `false` otherwise. When `true`,\n * TypeScript narrows `s` to `MaxLengthString<MaxLength> & S`.\n */\nexport const isMaxLengthString = <\n S extends string,\n MaxLength extends SupportedLength,\n>(\n s: S,\n maxLength: MaxLength,\n): s is MaxLengthString<MaxLength> & S => s.length <= maxLength;\n\n/**\n * Type guard that checks if a string's length is within the inclusive range\n * `[minLength, maxLength]`.\n *\n * The length is measured in UTF-16 code units (the same unit as\n * `String.prototype.length`), so characters outside the Basic Multilingual\n * Plane (e.g. emoji) count as 2.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `BoundedLengthString<MinLength, MaxLength>` while\n * preserving the original string type (e.g. literal types) via intersection.\n * - Since `BoundedLengthString` is the intersection of `MinLengthString` and\n * `MaxLengthString`, the narrowed value is assignable to both, and both\n * bounds can be weakened independently.\n *\n * @example\n *\n * ```ts\n * const input: string = 'user-12345678';\n *\n * assert.isTrue(isBoundedLengthString(input, 8, 16));\n *\n * assert.isFalse(isBoundedLengthString('user', 8, 16));\n *\n * if (isBoundedLengthString(input, 8, 16)) {\n * const userId: BoundedLengthString<8, 16> = input;\n *\n * const relaxed: BoundedLengthString<1, 255> = input; // OK ([8, 16] ⊆ [1, 255])\n * }\n * ```\n *\n * @template S - The input string type (literal types are preserved).\n * @template MinLength - The minimum number of characters (inclusive).\n * @template MaxLength - The maximum number of characters (inclusive).\n * @param s - The string to check.\n * @param minLength - The minimum number of characters (inclusive).\n * @param maxLength - The maximum number of characters (inclusive).\n * @returns `true` if `minLength <= s.length && s.length <= maxLength`, `false`\n * otherwise. When `true`, TypeScript narrows `s` to\n * `BoundedLengthString<MinLength, MaxLength> & S`.\n */\nexport const isBoundedLengthString = <\n S extends string,\n MinLength extends SupportedLength,\n MaxLength extends SupportedLength,\n>(\n s: S,\n minLength: MinLength,\n maxLength: MaxLength,\n): s is BoundedLengthString<MinLength, MaxLength> & S =>\n s.length >= minLength && s.length <= maxLength;\n\n/**\n * Type guard that checks if a string has exactly `length` characters.\n *\n * The length is measured in UTF-16 code units (the same unit as\n * `String.prototype.length`), so characters outside the Basic Multilingual\n * Plane (e.g. emoji) count as 2.\n *\n * **Type Narrowing Behavior:**\n *\n * - Narrows the input to `FixedLengthString<Length>` while preserving the\n * original string type (e.g. literal types) via intersection.\n * - Since `FixedLengthString<Length>` is defined as\n * `BoundedLengthString<Length, Length>`, the narrowed value is assignable to\n * any `MinLengthString<N>` with `N <= Length` and any `MaxLengthString<N>`\n * with `N >= Length`.\n *\n * @example\n *\n * ```ts\n * const input: string = 'JP';\n *\n * assert.isTrue(isFixedLengthString(input, 2));\n *\n * assert.isFalse(isFixedLengthString(input, 3));\n *\n * if (isFixedLengthString(input, 2)) {\n * const countryCode: FixedLengthString<2> = input;\n *\n * const atMost5: MaxLengthString<5> = input; // OK (2 <= 5)\n * }\n * ```\n *\n * @template S - The input string type (literal types are preserved).\n * @template Length - The exact number of characters.\n * @param s - The string to check.\n * @param length - The exact number of characters.\n * @returns `true` if `s.length === length`, `false` otherwise. When `true`,\n * TypeScript narrows `s` to `FixedLengthString<Length> & S`.\n */\nexport const isFixedLengthString = <\n S extends string,\n Length extends SupportedLength,\n>(\n s: S,\n length: Length,\n): s is FixedLengthString<Length> & S => s.length === length;\n"],"names":[],"mappings":"AA8CO,MAAM,iBAAA,GAAoB,CAI/B,CAAA,EACA,SAAA,KACwC,EAAE,MAAA,IAAU;AAwC/C,MAAM,iBAAA,GAAoB,CAI/B,CAAA,EACA,SAAA,KACwC,EAAE,MAAA,IAAU;AA4C/C,MAAM,qBAAA,GAAwB,CAKnC,CAAA,EACA,SAAA,EACA,cAEA,CAAA,CAAE,MAAA,IAAU,SAAA,IAAa,CAAA,CAAE,MAAA,IAAU;AAyChC,MAAM,mBAAA,GAAsB,CAIjC,CAAA,EACA,MAAA,KACuC,EAAE,MAAA,KAAW;;;;"}
|
|
@@ -1,199 +0,0 @@
|
|
|
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
|
-
* Type guard that checks if a string has at least `minLength` characters.
|
|
11
|
-
*
|
|
12
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
13
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
14
|
-
* Plane (e.g. emoji) count as 2.
|
|
15
|
-
*
|
|
16
|
-
* **Type Narrowing Behavior:**
|
|
17
|
-
*
|
|
18
|
-
* - Narrows the input to `MinLengthString<MinLength>` while preserving the
|
|
19
|
-
* original string type (e.g. literal types) via intersection.
|
|
20
|
-
* - The result participates in the length-constraint subtyping relation: for
|
|
21
|
-
* example, a value narrowed to `MinLengthString<3>` is assignable to
|
|
22
|
-
* `MinLengthString<1>` (= `NonEmptyString`).
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
*
|
|
26
|
-
* ```ts
|
|
27
|
-
* const input: string = 'very-secret-password';
|
|
28
|
-
*
|
|
29
|
-
* assert.isTrue(isMinLengthString(input, 12));
|
|
30
|
-
*
|
|
31
|
-
* assert.isFalse(isMinLengthString('short', 12));
|
|
32
|
-
*
|
|
33
|
-
* if (isMinLengthString(input, 12)) {
|
|
34
|
-
* const password: MinLengthString<12> = input;
|
|
35
|
-
*
|
|
36
|
-
* const nonEmpty: NonEmptyString = input; // OK (12 >= 1)
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @template S - The input string type (literal types are preserved).
|
|
41
|
-
* @template MinLength - The minimum number of characters (inclusive).
|
|
42
|
-
* @param s - The string to check.
|
|
43
|
-
* @param minLength - The minimum number of characters (inclusive).
|
|
44
|
-
* @returns `true` if `s.length >= minLength`, `false` otherwise. When `true`,
|
|
45
|
-
* TypeScript narrows `s` to `MinLengthString<MinLength> & S`.
|
|
46
|
-
*/
|
|
47
|
-
export const isMinLengthString = <
|
|
48
|
-
S extends string,
|
|
49
|
-
MinLength extends SupportedLength,
|
|
50
|
-
>(
|
|
51
|
-
s: S,
|
|
52
|
-
minLength: MinLength,
|
|
53
|
-
): s is MinLengthString<MinLength> & S => s.length >= minLength;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Type guard that checks if a string has at most `maxLength` characters.
|
|
57
|
-
*
|
|
58
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
59
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
60
|
-
* Plane (e.g. emoji) count as 2.
|
|
61
|
-
*
|
|
62
|
-
* **Type Narrowing Behavior:**
|
|
63
|
-
*
|
|
64
|
-
* - Narrows the input to `MaxLengthString<MaxLength>` while preserving the
|
|
65
|
-
* original string type (e.g. literal types) via intersection.
|
|
66
|
-
* - The result participates in the length-constraint subtyping relation: for
|
|
67
|
-
* example, a value narrowed to `MaxLengthString<16>` is assignable to
|
|
68
|
-
* `MaxLengthString<32>`.
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
*
|
|
72
|
-
* ```ts
|
|
73
|
-
* const input: string = 'noshiro';
|
|
74
|
-
*
|
|
75
|
-
* assert.isTrue(isMaxLengthString(input, 32));
|
|
76
|
-
*
|
|
77
|
-
* assert.isFalse(isMaxLengthString(input, 3));
|
|
78
|
-
*
|
|
79
|
-
* if (isMaxLengthString(input, 32)) {
|
|
80
|
-
* const userName: MaxLengthString<32> = input;
|
|
81
|
-
*
|
|
82
|
-
* const relaxed: MaxLengthString<64> = input; // OK (32 <= 64)
|
|
83
|
-
* }
|
|
84
|
-
* ```
|
|
85
|
-
*
|
|
86
|
-
* @template S - The input string type (literal types are preserved).
|
|
87
|
-
* @template MaxLength - The maximum number of characters (inclusive).
|
|
88
|
-
* @param s - The string to check.
|
|
89
|
-
* @param maxLength - The maximum number of characters (inclusive).
|
|
90
|
-
* @returns `true` if `s.length <= maxLength`, `false` otherwise. When `true`,
|
|
91
|
-
* TypeScript narrows `s` to `MaxLengthString<MaxLength> & S`.
|
|
92
|
-
*/
|
|
93
|
-
export const isMaxLengthString = <
|
|
94
|
-
S extends string,
|
|
95
|
-
MaxLength extends SupportedLength,
|
|
96
|
-
>(
|
|
97
|
-
s: S,
|
|
98
|
-
maxLength: MaxLength,
|
|
99
|
-
): s is MaxLengthString<MaxLength> & S => s.length <= maxLength;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Type guard that checks if a string's length is within the inclusive range
|
|
103
|
-
* `[minLength, maxLength]`.
|
|
104
|
-
*
|
|
105
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
106
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
107
|
-
* Plane (e.g. emoji) count as 2.
|
|
108
|
-
*
|
|
109
|
-
* **Type Narrowing Behavior:**
|
|
110
|
-
*
|
|
111
|
-
* - Narrows the input to `BoundedLengthString<MinLength, MaxLength>` while
|
|
112
|
-
* preserving the original string type (e.g. literal types) via intersection.
|
|
113
|
-
* - Since `BoundedLengthString` is the intersection of `MinLengthString` and
|
|
114
|
-
* `MaxLengthString`, the narrowed value is assignable to both, and both
|
|
115
|
-
* bounds can be weakened independently.
|
|
116
|
-
*
|
|
117
|
-
* @example
|
|
118
|
-
*
|
|
119
|
-
* ```ts
|
|
120
|
-
* const input: string = 'user-12345678';
|
|
121
|
-
*
|
|
122
|
-
* assert.isTrue(isBoundedLengthString(input, 8, 16));
|
|
123
|
-
*
|
|
124
|
-
* assert.isFalse(isBoundedLengthString('user', 8, 16));
|
|
125
|
-
*
|
|
126
|
-
* if (isBoundedLengthString(input, 8, 16)) {
|
|
127
|
-
* const userId: BoundedLengthString<8, 16> = input;
|
|
128
|
-
*
|
|
129
|
-
* const relaxed: BoundedLengthString<1, 255> = input; // OK ([8, 16] ⊆ [1, 255])
|
|
130
|
-
* }
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* @template S - The input string type (literal types are preserved).
|
|
134
|
-
* @template MinLength - The minimum number of characters (inclusive).
|
|
135
|
-
* @template MaxLength - The maximum number of characters (inclusive).
|
|
136
|
-
* @param s - The string to check.
|
|
137
|
-
* @param minLength - The minimum number of characters (inclusive).
|
|
138
|
-
* @param maxLength - The maximum number of characters (inclusive).
|
|
139
|
-
* @returns `true` if `minLength <= s.length && s.length <= maxLength`, `false`
|
|
140
|
-
* otherwise. When `true`, TypeScript narrows `s` to
|
|
141
|
-
* `BoundedLengthString<MinLength, MaxLength> & S`.
|
|
142
|
-
*/
|
|
143
|
-
export const isBoundedLengthString = <
|
|
144
|
-
S extends string,
|
|
145
|
-
MinLength extends SupportedLength,
|
|
146
|
-
MaxLength extends SupportedLength,
|
|
147
|
-
>(
|
|
148
|
-
s: S,
|
|
149
|
-
minLength: MinLength,
|
|
150
|
-
maxLength: MaxLength,
|
|
151
|
-
): s is BoundedLengthString<MinLength, MaxLength> & S =>
|
|
152
|
-
s.length >= minLength && s.length <= maxLength;
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Type guard that checks if a string has exactly `length` characters.
|
|
156
|
-
*
|
|
157
|
-
* The length is measured in UTF-16 code units (the same unit as
|
|
158
|
-
* `String.prototype.length`), so characters outside the Basic Multilingual
|
|
159
|
-
* Plane (e.g. emoji) count as 2.
|
|
160
|
-
*
|
|
161
|
-
* **Type Narrowing Behavior:**
|
|
162
|
-
*
|
|
163
|
-
* - Narrows the input to `FixedLengthString<Length>` while preserving the
|
|
164
|
-
* original string type (e.g. literal types) via intersection.
|
|
165
|
-
* - Since `FixedLengthString<Length>` is defined as
|
|
166
|
-
* `BoundedLengthString<Length, Length>`, the narrowed value is assignable to
|
|
167
|
-
* any `MinLengthString<N>` with `N <= Length` and any `MaxLengthString<N>`
|
|
168
|
-
* with `N >= Length`.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
*
|
|
172
|
-
* ```ts
|
|
173
|
-
* const input: string = 'JP';
|
|
174
|
-
*
|
|
175
|
-
* assert.isTrue(isFixedLengthString(input, 2));
|
|
176
|
-
*
|
|
177
|
-
* assert.isFalse(isFixedLengthString(input, 3));
|
|
178
|
-
*
|
|
179
|
-
* if (isFixedLengthString(input, 2)) {
|
|
180
|
-
* const countryCode: FixedLengthString<2> = input;
|
|
181
|
-
*
|
|
182
|
-
* const atMost5: MaxLengthString<5> = input; // OK (2 <= 5)
|
|
183
|
-
* }
|
|
184
|
-
* ```
|
|
185
|
-
*
|
|
186
|
-
* @template S - The input string type (literal types are preserved).
|
|
187
|
-
* @template Length - The exact number of characters.
|
|
188
|
-
* @param s - The string to check.
|
|
189
|
-
* @param length - The exact number of characters.
|
|
190
|
-
* @returns `true` if `s.length === length`, `false` otherwise. When `true`,
|
|
191
|
-
* TypeScript narrows `s` to `FixedLengthString<Length> & S`.
|
|
192
|
-
*/
|
|
193
|
-
export const isFixedLengthString = <
|
|
194
|
-
S extends string,
|
|
195
|
-
Length extends SupportedLength,
|
|
196
|
-
>(
|
|
197
|
-
s: S,
|
|
198
|
-
length: Length,
|
|
199
|
-
): s is FixedLengthString<Length> & S => s.length === length;
|
|
@@ -1,170 +0,0 @@
|
|
|
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 {
|
|
10
|
-
isBoundedLengthString,
|
|
11
|
-
isFixedLengthString,
|
|
12
|
-
isMaxLengthString,
|
|
13
|
-
isMinLengthString,
|
|
14
|
-
} from './is-length-bounded-string.mjs';
|
|
15
|
-
|
|
16
|
-
describe(isMinLengthString, () => {
|
|
17
|
-
test('should return true when the string is long enough', () => {
|
|
18
|
-
assert.isTrue(isMinLengthString('hello', 3));
|
|
19
|
-
|
|
20
|
-
assert.isTrue(isMinLengthString('hello', 5));
|
|
21
|
-
|
|
22
|
-
assert.isTrue(isMinLengthString('', 0));
|
|
23
|
-
|
|
24
|
-
assert.isTrue(isMinLengthString(' ', 1)); // Space counts
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
test('should return false when the string is too short', () => {
|
|
28
|
-
assert.isFalse(isMinLengthString('hi', 3));
|
|
29
|
-
|
|
30
|
-
assert.isFalse(isMinLengthString('', 1));
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('should measure length in UTF-16 code units', () => {
|
|
34
|
-
assert.isTrue(isMinLengthString('🎉', 2)); // Surrogate pair
|
|
35
|
-
|
|
36
|
-
assert.isFalse(isMinLengthString('🎉', 3));
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('should act as a type guard', () => {
|
|
40
|
-
const value: string = 'very-secret-password';
|
|
41
|
-
|
|
42
|
-
if (isMinLengthString(value, 12)) {
|
|
43
|
-
expectType<typeof value, MinLengthString<12>>('<=');
|
|
44
|
-
|
|
45
|
-
expectType<typeof value, NonEmptyString>('<='); // 12 >= 1
|
|
46
|
-
|
|
47
|
-
expectType<typeof value, MinLengthString<16>>('!<=');
|
|
48
|
-
|
|
49
|
-
expect(value.length).toBeGreaterThanOrEqual(12);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test('should preserve literal string types', () => {
|
|
54
|
-
const literal = 'hello';
|
|
55
|
-
|
|
56
|
-
if (isMinLengthString(literal, 3)) {
|
|
57
|
-
expectType<typeof literal, 'hello'>('<=');
|
|
58
|
-
|
|
59
|
-
expectType<typeof literal, MinLengthString<3>>('<=');
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe(isMaxLengthString, () => {
|
|
65
|
-
test('should return true when the string is short enough', () => {
|
|
66
|
-
assert.isTrue(isMaxLengthString('hello', 10));
|
|
67
|
-
|
|
68
|
-
assert.isTrue(isMaxLengthString('hello', 5));
|
|
69
|
-
|
|
70
|
-
assert.isTrue(isMaxLengthString('', 0));
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test('should return false when the string is too long', () => {
|
|
74
|
-
assert.isFalse(isMaxLengthString('hello', 3));
|
|
75
|
-
|
|
76
|
-
assert.isFalse(isMaxLengthString(' ', 0));
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('should measure length in UTF-16 code units', () => {
|
|
80
|
-
assert.isFalse(isMaxLengthString('🎉', 1)); // Surrogate pair
|
|
81
|
-
|
|
82
|
-
assert.isTrue(isMaxLengthString('🎉', 2));
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test('should act as a type guard', () => {
|
|
86
|
-
const value: string = 'noshiro';
|
|
87
|
-
|
|
88
|
-
if (isMaxLengthString(value, 32)) {
|
|
89
|
-
expectType<typeof value, MaxLengthString<32>>('<=');
|
|
90
|
-
|
|
91
|
-
expectType<typeof value, MaxLengthString<64>>('<='); // 32 <= 64
|
|
92
|
-
|
|
93
|
-
expectType<typeof value, MaxLengthString<16>>('!<=');
|
|
94
|
-
|
|
95
|
-
expect(value.length).toBeLessThanOrEqual(32);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe(isBoundedLengthString, () => {
|
|
101
|
-
test('should return true when the length is within the range', () => {
|
|
102
|
-
assert.isTrue(isBoundedLengthString('user-12345678', 8, 16));
|
|
103
|
-
|
|
104
|
-
assert.isTrue(isBoundedLengthString('12345678', 8, 16)); // Lower bound (inclusive)
|
|
105
|
-
|
|
106
|
-
assert.isTrue(isBoundedLengthString('1234567890123456', 8, 16)); // Upper bound (inclusive)
|
|
107
|
-
|
|
108
|
-
assert.isTrue(isBoundedLengthString('', 0, 0));
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test('should return false when the length is out of range', () => {
|
|
112
|
-
assert.isFalse(isBoundedLengthString('user', 8, 16));
|
|
113
|
-
|
|
114
|
-
assert.isFalse(isBoundedLengthString('12345678901234567', 8, 16));
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
test('should act as a type guard', () => {
|
|
118
|
-
const value: string = 'user-12345678';
|
|
119
|
-
|
|
120
|
-
if (isBoundedLengthString(value, 8, 16)) {
|
|
121
|
-
expectType<typeof value, BoundedLengthString<8, 16>>('<=');
|
|
122
|
-
|
|
123
|
-
expectType<typeof value, BoundedLengthString<1, 255>>('<='); // [8, 16] ⊆ [1, 255]
|
|
124
|
-
|
|
125
|
-
expectType<typeof value, MinLengthString<8>>('<=');
|
|
126
|
-
|
|
127
|
-
expectType<typeof value, MaxLengthString<16>>('<=');
|
|
128
|
-
|
|
129
|
-
expectType<typeof value, NonEmptyString>('<='); // 8 >= 1
|
|
130
|
-
|
|
131
|
-
expectType<typeof value, BoundedLengthString<10, 16>>('!<=');
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe(isFixedLengthString, () => {
|
|
137
|
-
test('should return true only for the exact length', () => {
|
|
138
|
-
assert.isTrue(isFixedLengthString('JP', 2));
|
|
139
|
-
|
|
140
|
-
assert.isTrue(isFixedLengthString('', 0));
|
|
141
|
-
|
|
142
|
-
assert.isFalse(isFixedLengthString('JPN', 2));
|
|
143
|
-
|
|
144
|
-
assert.isFalse(isFixedLengthString('J', 2));
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
test('should measure length in UTF-16 code units', () => {
|
|
148
|
-
assert.isTrue(isFixedLengthString('🎉', 2)); // Surrogate pair
|
|
149
|
-
|
|
150
|
-
assert.isFalse(isFixedLengthString('🎉', 1));
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test('should act as a type guard', () => {
|
|
154
|
-
const value: string = 'JP';
|
|
155
|
-
|
|
156
|
-
if (isFixedLengthString(value, 2)) {
|
|
157
|
-
expectType<typeof value, FixedLengthString<2>>('<=');
|
|
158
|
-
|
|
159
|
-
expectType<typeof value, BoundedLengthString<2, 2>>('<=');
|
|
160
|
-
|
|
161
|
-
expectType<typeof value, MaxLengthString<5>>('<='); // 2 <= 5
|
|
162
|
-
|
|
163
|
-
expectType<typeof value, MinLengthString<1>>('<='); // 2 >= 1
|
|
164
|
-
|
|
165
|
-
expectType<typeof value, NonEmptyString>('<=');
|
|
166
|
-
|
|
167
|
-
expectType<typeof value, FixedLengthString<3>>('!<=');
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
});
|