typebox 1.0.66 → 1.0.67

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.
@@ -34,7 +34,8 @@ export declare function IsGreaterThan(left: string, right: string): string;
34
34
  export declare function IsLessThan(left: string, right: string): string;
35
35
  export declare function IsLessEqualThan(left: string, right: string): string;
36
36
  export declare function IsGreaterEqualThan(left: string, right: string): string;
37
- export declare function StringGraphemeCount(value: string): string;
37
+ export declare function IsMinLength(value: string, length: string): string;
38
+ export declare function IsMaxLength(value: string, length: string): string;
38
39
  export declare function Every(value: string, offset: string, params: [value: string, index: string], expression: string): string;
39
40
  export declare function Entries(value: string): string;
40
41
  export declare function Keys(value: string): string;
@@ -104,8 +104,11 @@ export function IsGreaterEqualThan(left, right) {
104
104
  // --------------------------------------------------------------------------
105
105
  // String
106
106
  // --------------------------------------------------------------------------
107
- export function StringGraphemeCount(value) {
108
- return `Guard.StringGraphemeCount(${value})`;
107
+ export function IsMinLength(value, length) {
108
+ return `Guard.IsMinLength(${value}, ${length})`;
109
+ }
110
+ export function IsMaxLength(value, length) {
111
+ return `Guard.IsMaxLength(${value}, ${length})`;
109
112
  }
110
113
  // --------------------------------------------------------------------------
111
114
  // Array
@@ -181,7 +184,7 @@ export function ReduceOr(operands) {
181
184
  return G.IsEqual(operands.length, 0) ? 'false' : operands.reduce((left, right) => Or(left, right));
182
185
  }
183
186
  // --------------------------------------------------------------------------
184
- // Arithmatic
187
+ // Arithmetic
185
188
  // --------------------------------------------------------------------------
186
189
  export function PrefixIncrement(expression) {
187
190
  return `++${expression}`;
@@ -37,6 +37,12 @@ export declare function IsMultipleOf(dividend: bigint | number, divisor: bigint
37
37
  /** Returns true if the value appears to be an instance of a class. */
38
38
  export declare function IsClassInstance(value: unknown): boolean;
39
39
  export declare function IsValueLike(value: unknown): value is bigint | boolean | null | number | string | undefined;
40
+ /** Returns the number of grapheme clusters in the string */
41
+ export declare function GraphemeCount(value: string): number;
42
+ /** Returns true if the string has at most the given number of graphemes */
43
+ export declare function IsMaxLength(value: string, length: number): boolean;
44
+ /** Returns true if the string has at least the given number of graphemes */
45
+ export declare function IsMinLength(value: string, length: number): boolean;
40
46
  export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
41
47
  export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
42
48
  /** Returns true if this value has this property key */
@@ -55,5 +61,3 @@ export declare function Symbols(value: Record<PropertyKey, unknown>): symbol[];
55
61
  export declare function Values(value: Record<PropertyKey, unknown>): unknown[];
56
62
  /** Tests values for deep equality */
57
63
  export declare function IsDeepEqual(left: unknown, right: unknown): boolean;
58
- /** Returns the number of Unicode Grapheme Clusters */
59
- export declare function StringGraphemeCount(value: string): number;
@@ -1,3 +1,4 @@
1
+ import * as String from './string.mjs';
1
2
  // --------------------------------------------------------------------------
2
3
  // Guards
3
4
  // --------------------------------------------------------------------------
@@ -127,6 +128,21 @@ export function IsValueLike(value) {
127
128
  IsUndefined(value);
128
129
  }
129
130
  // --------------------------------------------------------------------------
131
+ // String
132
+ // --------------------------------------------------------------------------
133
+ /** Returns the number of grapheme clusters in the string */
134
+ export function GraphemeCount(value) {
135
+ return String.GraphemeCount(value);
136
+ }
137
+ /** Returns true if the string has at most the given number of graphemes */
138
+ export function IsMaxLength(value, length) {
139
+ return String.IsMaxLengthFast(value, length);
140
+ }
141
+ /** Returns true if the string has at least the given number of graphemes */
142
+ export function IsMinLength(value, length) {
143
+ return String.IsMinLengthFast(value, length);
144
+ }
145
+ // --------------------------------------------------------------------------
130
146
  // Array
131
147
  // --------------------------------------------------------------------------
132
148
  export function Every(value, offset, callback) {
@@ -190,84 +206,3 @@ function DeepEqualArray(left, right) {
190
206
  export function IsDeepEqual(left, right) {
191
207
  return (IsArray(left) ? DeepEqualArray(left, right) : IsObject(left) ? DeepEqualObject(left, right) : IsEqual(left, right));
192
208
  }
193
- // --------------------------------------------------------------------------
194
- // StringGraphemeCountIntl - Intl.Segmenter Polyfill
195
- // --------------------------------------------------------------------------
196
- //
197
- // const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
198
- // function StringGraphemeCountIntl(value: string): number {
199
- // const iterator = segmenter.segment(value)[Symbol.iterator]()
200
- // let length = 0
201
- // while (!iterator.next().done) length++
202
- // return length
203
- // }
204
- //
205
- // --------------------------------------------------------------------------
206
- function IsRegionalIndicator(value) {
207
- return value >= 0x1F1E6 && value <= 0x1F1FF;
208
- }
209
- function IsVariationSelector(value) {
210
- return value >= 0xFE00 && value <= 0xFE0F;
211
- }
212
- function IsCombiningMark(value) {
213
- return ((value >= 0x0300 && value <= 0x036F) ||
214
- (value >= 0x1AB0 && value <= 0x1AFF) ||
215
- (value >= 0x1DC0 && value <= 0x1DFF) ||
216
- (value >= 0xFE20 && value <= 0xFE2F));
217
- }
218
- function CodePointLength(value) {
219
- return value > 0xFFFF ? 2 : 1;
220
- }
221
- function StringGraphemeCountIntl(value) {
222
- let count = 0;
223
- let index = 0;
224
- while (index < value.length) {
225
- const start = value.codePointAt(index);
226
- let clusterEnd = index + CodePointLength(start);
227
- // Combining marks & variation selectors
228
- while (clusterEnd < value.length) {
229
- const next = value.codePointAt(clusterEnd);
230
- if (IsCombiningMark(next) || IsVariationSelector(next)) {
231
- clusterEnd += CodePointLength(next);
232
- }
233
- else {
234
- break;
235
- }
236
- }
237
- // ZWJ sequences
238
- while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') {
239
- const next = value.codePointAt(clusterEnd + 1);
240
- clusterEnd += 1 + CodePointLength(next);
241
- }
242
- // Regional indicator pairs (flags)
243
- const first = IsRegionalIndicator(start);
244
- const second = clusterEnd < value.length && IsRegionalIndicator(value.codePointAt(clusterEnd));
245
- if (first && second) {
246
- const next = value.codePointAt(clusterEnd);
247
- clusterEnd += CodePointLength(next);
248
- }
249
- count++;
250
- index = clusterEnd;
251
- }
252
- return count;
253
- }
254
- // --------------------------------------------------------------------------
255
- // StringGraphemeCount
256
- // --------------------------------------------------------------------------
257
- function IsComplexGraphemeCodeUnit(value) {
258
- return ((value >= 0xD800 && value <= 0xDBFF) || // High surrogate
259
- (value >= 0x0300 && value <= 0x036F) || // Combining diacritical marks
260
- (value === 0x200D) // Zero-width joiner
261
- );
262
- }
263
- /** Returns the number of Unicode Grapheme Clusters */
264
- export function StringGraphemeCount(value) {
265
- let count = 0;
266
- for (let index = 0; index < value.length; index++) {
267
- if (IsComplexGraphemeCodeUnit(value.charCodeAt(index))) {
268
- return StringGraphemeCountIntl(value);
269
- }
270
- count++;
271
- }
272
- return count;
273
- }
@@ -0,0 +1,10 @@
1
+ /** Returns the number of grapheme clusters in a string */
2
+ export declare function GraphemeCount(value: string): number;
3
+ /** Checks if a string has at least a minimum number of grapheme clusters */
4
+ export declare function IsMinLength(value: string, minLength: number): boolean;
5
+ /** Checks if a string has at most a maximum number of grapheme clusters */
6
+ export declare function IsMaxLength(value: string, maxLength: number): boolean;
7
+ /** Fast check for minimum grapheme length, falls back to full check if needed */
8
+ export declare function IsMinLengthFast(value: string, minLength: number): boolean;
9
+ /** Fast check for maximum grapheme length, falls back to full check if needed */
10
+ export declare function IsMaxLengthFast(value: string, maxLength: number): boolean;
@@ -0,0 +1,154 @@
1
+ // --------------------------------------------------------------------------
2
+ // IsBetween
3
+ // --------------------------------------------------------------------------
4
+ function IsBetween(value, min, max) {
5
+ return value >= min && value <= max;
6
+ }
7
+ // --------------------------------------------------------------------------
8
+ // IsRegionalIndicator
9
+ // --------------------------------------------------------------------------
10
+ function IsRegionalIndicator(value) {
11
+ return IsBetween(value, 0x1F1E6, 0x1F1FF);
12
+ }
13
+ // --------------------------------------------------------------------------
14
+ // IsVariationSelector
15
+ // --------------------------------------------------------------------------
16
+ function IsVariationSelector(value) {
17
+ return IsBetween(value, 0xFE00, 0xFE0F);
18
+ }
19
+ // --------------------------------------------------------------------------
20
+ // IsCombiningMark
21
+ // --------------------------------------------------------------------------
22
+ function IsCombiningMark(value) {
23
+ return (IsBetween(value, 0x0300, 0x036F) ||
24
+ IsBetween(value, 0x1AB0, 0x1AFF) ||
25
+ IsBetween(value, 0x1DC0, 0x1DFF) ||
26
+ IsBetween(value, 0xFE20, 0xFE2F));
27
+ }
28
+ // --------------------------------------------------------------------------
29
+ // CodePointLength
30
+ // --------------------------------------------------------------------------
31
+ function CodePointLength(value) {
32
+ return value > 0xFFFF ? 2 : 1;
33
+ }
34
+ // --------------------------------------------------------------------------
35
+ // ConsumeModifiers (helper)
36
+ // --------------------------------------------------------------------------
37
+ function ConsumeModifiers(value, index) {
38
+ while (index < value.length) {
39
+ const point = value.codePointAt(index);
40
+ if (IsCombiningMark(point) || IsVariationSelector(point)) {
41
+ index += CodePointLength(point);
42
+ }
43
+ else {
44
+ break;
45
+ }
46
+ }
47
+ return index;
48
+ }
49
+ // --------------------------------------------------------------------------
50
+ // NextGraphemeClusterIndex
51
+ // --------------------------------------------------------------------------
52
+ function NextGraphemeClusterIndex(value, clusterStart) {
53
+ const startCP = value.codePointAt(clusterStart);
54
+ let clusterEnd = clusterStart + CodePointLength(startCP);
55
+ // Consume combining marks & variation selectors
56
+ clusterEnd = ConsumeModifiers(value, clusterEnd);
57
+ // Handle multi-ZWJ sequences
58
+ while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') {
59
+ const nextCP = value.codePointAt(clusterEnd + 1);
60
+ clusterEnd += 1 + CodePointLength(nextCP);
61
+ clusterEnd = ConsumeModifiers(value, clusterEnd);
62
+ }
63
+ // Handle regional indicator pairs (flags)
64
+ if (IsRegionalIndicator(startCP) &&
65
+ clusterEnd < value.length &&
66
+ IsRegionalIndicator(value.codePointAt(clusterEnd))) {
67
+ clusterEnd += CodePointLength(value.codePointAt(clusterEnd));
68
+ }
69
+ return clusterEnd;
70
+ }
71
+ // --------------------------------------------------------------------------
72
+ // IsGraphemeCodePoint
73
+ // --------------------------------------------------------------------------
74
+ function IsGraphemeCodePoint(value) {
75
+ return (IsBetween(value, 0xD800, 0xDBFF) || // High surrogate
76
+ IsBetween(value, 0x0300, 0x036F) || // Combining diacritical marks
77
+ (value === 0x200D) // Zero-width joiner
78
+ );
79
+ }
80
+ // --------------------------------------------------------------------------
81
+ // GraphemeCount
82
+ // --------------------------------------------------------------------------
83
+ /** Returns the number of grapheme clusters in a string */
84
+ export function GraphemeCount(value) {
85
+ let count = 0;
86
+ let index = 0;
87
+ while (index < value.length) {
88
+ index = NextGraphemeClusterIndex(value, index);
89
+ count++;
90
+ }
91
+ return count;
92
+ }
93
+ // --------------------------------------------------------------------------
94
+ // IsMinLength
95
+ // --------------------------------------------------------------------------
96
+ /** Checks if a string has at least a minimum number of grapheme clusters */
97
+ export function IsMinLength(value, minLength) {
98
+ let count = 0;
99
+ let index = 0;
100
+ while (index < value.length) {
101
+ index = NextGraphemeClusterIndex(value, index);
102
+ count++;
103
+ if (count >= minLength)
104
+ return true;
105
+ }
106
+ return false;
107
+ }
108
+ // --------------------------------------------------------------------------
109
+ // IsMaxLength
110
+ // --------------------------------------------------------------------------
111
+ /** Checks if a string has at most a maximum number of grapheme clusters */
112
+ export function IsMaxLength(value, maxLength) {
113
+ let count = 0;
114
+ let index = 0;
115
+ while (index < value.length) {
116
+ index = NextGraphemeClusterIndex(value, index);
117
+ count++;
118
+ if (count > maxLength)
119
+ return false;
120
+ }
121
+ return true;
122
+ }
123
+ // --------------------------------------------------------------------------
124
+ // IsMinLengthFast
125
+ // --------------------------------------------------------------------------
126
+ /** Fast check for minimum grapheme length, falls back to full check if needed */
127
+ export function IsMinLengthFast(value, minLength) {
128
+ let index = 0;
129
+ while (index < value.length) {
130
+ if (IsGraphemeCodePoint(value.charCodeAt(index))) {
131
+ return IsMinLength(value, minLength);
132
+ }
133
+ index++;
134
+ if (index >= minLength)
135
+ return true;
136
+ }
137
+ return false;
138
+ }
139
+ // --------------------------------------------------------------------------
140
+ // IsMaxLengthFast
141
+ // --------------------------------------------------------------------------
142
+ /** Fast check for maximum grapheme length, falls back to full check if needed */
143
+ export function IsMaxLengthFast(value, maxLength) {
144
+ let index = 0;
145
+ while (index < value.length) {
146
+ if (IsGraphemeCodePoint(value.charCodeAt(index))) {
147
+ return IsMaxLength(value, maxLength);
148
+ }
149
+ index++;
150
+ if (index > maxLength)
151
+ return false;
152
+ }
153
+ return true;
154
+ }
@@ -4,13 +4,13 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
4
4
  // Build
5
5
  // ------------------------------------------------------------------
6
6
  export function BuildMaxLength(stack, context, schema, value) {
7
- return E.IsLessEqualThan(E.StringGraphemeCount(value), E.Constant(schema.maxLength));
7
+ return E.IsMaxLength(value, E.Constant(schema.maxLength));
8
8
  }
9
9
  // ------------------------------------------------------------------
10
10
  // Check
11
11
  // ------------------------------------------------------------------
12
12
  export function CheckMaxLength(stack, context, schema, value) {
13
- return G.IsLessEqualThan(G.StringGraphemeCount(value), schema.maxLength);
13
+ return G.IsMaxLength(value, schema.maxLength);
14
14
  }
15
15
  // ------------------------------------------------------------------
16
16
  // Error
@@ -4,13 +4,13 @@ import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
4
4
  // Build
5
5
  // ------------------------------------------------------------------
6
6
  export function BuildMinLength(stack, context, schema, value) {
7
- return E.IsGreaterEqualThan(E.StringGraphemeCount(value), E.Constant(schema.minLength));
7
+ return E.IsMinLength(value, E.Constant(schema.minLength));
8
8
  }
9
9
  // ------------------------------------------------------------------
10
10
  // Check
11
11
  // ------------------------------------------------------------------
12
12
  export function CheckMinLength(stack, context, schema, value) {
13
- return G.IsGreaterEqualThan(G.StringGraphemeCount(value), schema.minLength);
13
+ return G.IsMinLength(value, schema.minLength);
14
14
  }
15
15
  // ------------------------------------------------------------------
16
16
  // Error
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.0.66",
4
+ "version": "1.0.67",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"