ts-data-forge 9.0.1 → 11.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +1 -1
  2. package/dist/array/impl/array-utils-creation.d.mts +4 -4
  3. package/dist/array/impl/array-utils-creation.d.mts.map +1 -1
  4. package/dist/array/impl/array-utils-creation.mjs.map +1 -1
  5. package/dist/array/impl/array-utils-element-access.d.mts +4 -4
  6. package/dist/array/impl/array-utils-element-access.mjs.map +1 -1
  7. package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts +117 -0
  8. package/dist/array/impl/array-utils-length-bounded-array-cast.d.mts.map +1 -0
  9. package/dist/array/impl/array-utils-length-bounded-array-cast.mjs +37 -0
  10. package/dist/array/impl/array-utils-length-bounded-array-cast.mjs.map +1 -0
  11. package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts +159 -0
  12. package/dist/array/impl/array-utils-length-bounded-array-guard.d.mts.map +1 -0
  13. package/dist/array/impl/array-utils-length-bounded-array-guard.mjs +7 -0
  14. package/dist/array/impl/array-utils-length-bounded-array-guard.mjs.map +1 -0
  15. package/dist/array/impl/array-utils-modification.d.mts +11 -11
  16. package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
  17. package/dist/array/impl/array-utils-modification.mjs.map +1 -1
  18. package/dist/array/impl/array-utils-reducing-value.d.mts +9 -9
  19. package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
  20. package/dist/array/impl/array-utils-size.d.mts +3 -3
  21. package/dist/array/impl/array-utils-size.mjs.map +1 -1
  22. package/dist/array/impl/array-utils-slicing.d.mts +9 -9
  23. package/dist/array/impl/array-utils-slicing.d.mts.map +1 -1
  24. package/dist/array/impl/array-utils-slicing.mjs.map +1 -1
  25. package/dist/array/impl/array-utils-transformation.d.mts +16 -10
  26. package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
  27. package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
  28. package/dist/array/impl/array-utils-validation.d.mts +17 -17
  29. package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
  30. package/dist/array/impl/array-utils-validation.mjs +5 -5
  31. package/dist/array/impl/array-utils-validation.mjs.map +1 -1
  32. package/dist/array/impl/index.d.mts +2 -0
  33. package/dist/array/impl/index.d.mts.map +1 -1
  34. package/dist/array/impl/index.mjs +3 -1
  35. package/dist/array/impl/index.mjs.map +1 -1
  36. package/dist/entry-point.mjs +1 -1
  37. package/dist/guard/index.d.mts +0 -1
  38. package/dist/guard/index.d.mts.map +1 -1
  39. package/dist/guard/index.mjs +0 -1
  40. package/dist/guard/index.mjs.map +1 -1
  41. package/dist/index.d.mts +1 -0
  42. package/dist/index.d.mts.map +1 -1
  43. package/dist/index.mjs +1 -1
  44. package/dist/string/index.d.mts +2 -0
  45. package/dist/string/index.d.mts.map +1 -0
  46. package/dist/string/index.mjs +2 -0
  47. package/dist/string/index.mjs.map +1 -0
  48. package/dist/string/str.d.mts +273 -0
  49. package/dist/string/str.d.mts.map +1 -0
  50. package/dist/string/str.mjs +42 -0
  51. package/dist/string/str.mjs.map +1 -0
  52. package/package.json +30 -92
  53. package/src/array/impl/array-utils-creation.mts +5 -4
  54. package/src/array/impl/array-utils-element-access.mts +3 -3
  55. package/src/array/impl/array-utils-element-access.test.mts +3 -3
  56. package/src/array/impl/array-utils-length-bounded-array-cast.mts +190 -0
  57. package/src/array/impl/array-utils-length-bounded-array-cast.test.mts +118 -0
  58. package/src/array/impl/array-utils-length-bounded-array-guard.mts +195 -0
  59. package/src/array/impl/array-utils-length-bounded-array-guard.test.mts +169 -0
  60. package/src/array/impl/array-utils-modification.mts +16 -15
  61. package/src/array/impl/array-utils-modification.test.mts +10 -5
  62. package/src/array/impl/array-utils-reducing-value.mts +9 -9
  63. package/src/array/impl/array-utils-size.mts +2 -2
  64. package/src/array/impl/array-utils-slicing.mts +24 -13
  65. package/src/array/impl/array-utils-transformation.mts +22 -16
  66. package/src/array/impl/array-utils-transformation.test.mts +19 -19
  67. package/src/array/impl/array-utils-validation.mts +24 -24
  68. package/src/array/impl/array-utils-validation.test.mts +94 -92
  69. package/src/array/impl/index.mts +2 -0
  70. package/src/collections/queue.test.mts +1 -1
  71. package/src/guard/index.mts +0 -1
  72. package/src/index.mts +1 -0
  73. package/src/object/object.test.mts +3 -3
  74. package/src/string/index.mts +1 -0
  75. package/src/string/str.mts +371 -0
  76. package/src/string/str.test.mts +215 -0
  77. package/dist/guard/is-length-bounded-string.d.mts +0 -164
  78. package/dist/guard/is-length-bounded-string.d.mts.map +0 -1
  79. package/dist/guard/is-length-bounded-string.mjs +0 -7
  80. package/dist/guard/is-length-bounded-string.mjs.map +0 -1
  81. package/src/guard/is-length-bounded-string.mts +0 -199
  82. package/src/guard/is-length-bounded-string.test.mts +0 -170
@@ -1,164 +0,0 @@
1
- import { type BoundedLengthString, type FixedLengthString, type MaxLengthString, type MinLengthString } from 'ts-type-forge';
2
- import { type SizeType } from '../types.mjs';
3
- /**
4
- * Type guard that checks if a string has at least `minLength` characters.
5
- *
6
- * The length is measured in UTF-16 code units (the same unit as
7
- * `String.prototype.length`), so characters outside the Basic Multilingual
8
- * Plane (e.g. emoji) count as 2.
9
- *
10
- * **Type Narrowing Behavior:**
11
- *
12
- * - Narrows the input to `MinLengthString<MinLength>` while preserving the
13
- * original string type (e.g. literal types) via intersection.
14
- * - The result participates in the length-constraint subtyping relation: for
15
- * example, a value narrowed to `MinLengthString<3>` is assignable to
16
- * `MinLengthString<1>` (= `NonEmptyString`).
17
- *
18
- * @example
19
- *
20
- * ```ts
21
- * const input: string = 'very-secret-password';
22
- *
23
- * assert.isTrue(isMinLengthString(input, 12));
24
- *
25
- * assert.isFalse(isMinLengthString('short', 12));
26
- *
27
- * if (isMinLengthString(input, 12)) {
28
- * const password: MinLengthString<12> = input;
29
- *
30
- * const nonEmpty: NonEmptyString = input; // OK (12 >= 1)
31
- * }
32
- * ```
33
- *
34
- * @template S - The input string type (literal types are preserved).
35
- * @template MinLength - The minimum number of characters (inclusive).
36
- * @param s - The string to check.
37
- * @param minLength - The minimum number of characters (inclusive).
38
- * @returns `true` if `s.length >= minLength`, `false` otherwise. When `true`,
39
- * TypeScript narrows `s` to `MinLengthString<MinLength> & S`.
40
- */
41
- export declare const isMinLengthString: <S extends string, MinLength extends SizeType.ArgStr>(s: S, minLength: MinLength) => s is MinLengthString<MinLength> & S;
42
- /**
43
- * Type guard that checks if a string has at most `maxLength` characters.
44
- *
45
- * The length is measured in UTF-16 code units (the same unit as
46
- * `String.prototype.length`), so characters outside the Basic Multilingual
47
- * Plane (e.g. emoji) count as 2.
48
- *
49
- * **Type Narrowing Behavior:**
50
- *
51
- * - Narrows the input to `MaxLengthString<MaxLength>` while preserving the
52
- * original string type (e.g. literal types) via intersection.
53
- * - The result participates in the length-constraint subtyping relation: for
54
- * example, a value narrowed to `MaxLengthString<16>` is assignable to
55
- * `MaxLengthString<32>`.
56
- *
57
- * @example
58
- *
59
- * ```ts
60
- * const input: string = 'noshiro';
61
- *
62
- * assert.isTrue(isMaxLengthString(input, 32));
63
- *
64
- * assert.isFalse(isMaxLengthString(input, 3));
65
- *
66
- * if (isMaxLengthString(input, 32)) {
67
- * const userName: MaxLengthString<32> = input;
68
- *
69
- * const relaxed: MaxLengthString<64> = input; // OK (32 <= 64)
70
- * }
71
- * ```
72
- *
73
- * @template S - The input string type (literal types are preserved).
74
- * @template MaxLength - The maximum number of characters (inclusive).
75
- * @param s - The string to check.
76
- * @param maxLength - The maximum number of characters (inclusive).
77
- * @returns `true` if `s.length <= maxLength`, `false` otherwise. When `true`,
78
- * TypeScript narrows `s` to `MaxLengthString<MaxLength> & S`.
79
- */
80
- export declare const isMaxLengthString: <S extends string, MaxLength extends SizeType.ArgStr>(s: S, maxLength: MaxLength) => s is MaxLengthString<MaxLength> & S;
81
- /**
82
- * Type guard that checks if a string's length is within the inclusive range
83
- * `[minLength, maxLength]`.
84
- *
85
- * The length is measured in UTF-16 code units (the same unit as
86
- * `String.prototype.length`), so characters outside the Basic Multilingual
87
- * Plane (e.g. emoji) count as 2.
88
- *
89
- * **Type Narrowing Behavior:**
90
- *
91
- * - Narrows the input to `BoundedLengthString<MinLength, MaxLength>` while
92
- * preserving the original string type (e.g. literal types) via intersection.
93
- * - Since `BoundedLengthString` is the intersection of `MinLengthString` and
94
- * `MaxLengthString`, the narrowed value is assignable to both, and both
95
- * bounds can be weakened independently.
96
- *
97
- * @example
98
- *
99
- * ```ts
100
- * const input: string = 'user-12345678';
101
- *
102
- * assert.isTrue(isBoundedLengthString(input, 8, 16));
103
- *
104
- * assert.isFalse(isBoundedLengthString('user', 8, 16));
105
- *
106
- * if (isBoundedLengthString(input, 8, 16)) {
107
- * const userId: BoundedLengthString<8, 16> = input;
108
- *
109
- * const relaxed: BoundedLengthString<1, 255> = input; // OK ([8, 16] ⊆ [1, 255])
110
- * }
111
- * ```
112
- *
113
- * @template S - The input string type (literal types are preserved).
114
- * @template MinLength - The minimum number of characters (inclusive).
115
- * @template MaxLength - The maximum number of characters (inclusive).
116
- * @param s - The string to check.
117
- * @param minLength - The minimum number of characters (inclusive).
118
- * @param maxLength - The maximum number of characters (inclusive).
119
- * @returns `true` if `minLength <= s.length && s.length <= maxLength`, `false`
120
- * otherwise. When `true`, TypeScript narrows `s` to
121
- * `BoundedLengthString<MinLength, MaxLength> & S`.
122
- */
123
- export declare const isBoundedLengthString: <S extends string, MinLength extends SizeType.ArgStr, MaxLength extends SizeType.ArgStr>(s: S, minLength: MinLength, maxLength: MaxLength) => s is BoundedLengthString<MinLength, MaxLength> & S;
124
- /**
125
- * Type guard that checks if a string has exactly `length` characters.
126
- *
127
- * The length is measured in UTF-16 code units (the same unit as
128
- * `String.prototype.length`), so characters outside the Basic Multilingual
129
- * Plane (e.g. emoji) count as 2.
130
- *
131
- * **Type Narrowing Behavior:**
132
- *
133
- * - Narrows the input to `FixedLengthString<Length>` while preserving the
134
- * original string type (e.g. literal types) via intersection.
135
- * - Since `FixedLengthString<Length>` is defined as
136
- * `BoundedLengthString<Length, Length>`, the narrowed value is assignable to
137
- * any `MinLengthString<N>` with `N <= Length` and any `MaxLengthString<N>`
138
- * with `N >= Length`.
139
- *
140
- * @example
141
- *
142
- * ```ts
143
- * const input: string = 'JP';
144
- *
145
- * assert.isTrue(isFixedLengthString(input, 2));
146
- *
147
- * assert.isFalse(isFixedLengthString(input, 3));
148
- *
149
- * if (isFixedLengthString(input, 2)) {
150
- * const countryCode: FixedLengthString<2> = input;
151
- *
152
- * const atMost5: MaxLengthString<5> = input; // OK (2 <= 5)
153
- * }
154
- * ```
155
- *
156
- * @template S - The input string type (literal types are preserved).
157
- * @template Length - The exact number of characters.
158
- * @param s - The string to check.
159
- * @param length - The exact number of characters.
160
- * @returns `true` if `s.length === length`, `false` otherwise. When `true`,
161
- * TypeScript narrows `s` to `FixedLengthString<Length> & S`.
162
- */
163
- export declare const isFixedLengthString: <S extends string, Length extends SizeType.ArgStr>(s: S, length: Length) => s is FixedLengthString<Length> & S;
164
- //# 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,EACrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,eAAO,MAAM,iBAAiB,GAC5B,CAAC,SAAS,MAAM,EAChB,SAAS,SAAS,QAAQ,CAAC,MAAM,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,QAAQ,CAAC,MAAM,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,QAAQ,CAAC,MAAM,EACjC,SAAS,SAAS,QAAQ,CAAC,MAAM,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,QAAQ,CAAC,MAAM,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} from 'ts-type-forge';\nimport { type SizeType } from '../types.mjs';\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 SizeType.ArgStr,\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 SizeType.ArgStr,\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 SizeType.ArgStr,\n MaxLength extends SizeType.ArgStr,\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 SizeType.ArgStr,\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
- } from 'ts-type-forge';
7
- import { type SizeType } from '../types.mjs';
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 SizeType.ArgStr,
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 SizeType.ArgStr,
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 SizeType.ArgStr,
146
- MaxLength extends SizeType.ArgStr,
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 SizeType.ArgStr,
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
- });