typebox 1.0.64 → 1.0.66

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.
@@ -37,8 +37,6 @@ 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 Unicode Grapheme Clusters */
41
- export declare function StringGraphemeCount(value: string): number;
42
40
  export declare function Every<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
43
41
  export declare function EveryAll<T>(value: T[], offset: number, callback: (value: T, index: number) => boolean): boolean;
44
42
  /** Returns true if this value has this property key */
@@ -57,3 +55,5 @@ export declare function Symbols(value: Record<PropertyKey, unknown>): symbol[];
57
55
  export declare function Values(value: Record<PropertyKey, unknown>): unknown[];
58
56
  /** Tests values for deep equality */
59
57
  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;
@@ -127,13 +127,6 @@ export function IsValueLike(value) {
127
127
  IsUndefined(value);
128
128
  }
129
129
  // --------------------------------------------------------------------------
130
- // String
131
- // --------------------------------------------------------------------------
132
- /** Returns the number of Unicode Grapheme Clusters */
133
- export function StringGraphemeCount(value) {
134
- return Array.from(value).length;
135
- }
136
- // --------------------------------------------------------------------------
137
130
  // Array
138
131
  // --------------------------------------------------------------------------
139
132
  export function Every(value, offset, callback) {
@@ -197,3 +190,84 @@ function DeepEqualArray(left, right) {
197
190
  export function IsDeepEqual(left, right) {
198
191
  return (IsArray(left) ? DeepEqualArray(left, right) : IsObject(left) ? DeepEqualObject(left, right) : IsEqual(left, right));
199
192
  }
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,8 @@
1
+ import type { XSchema } from '../types/schema.mjs';
2
+ import type { XStaticSchema } from './schema.mjs';
3
+ import type { XIf } from '../types/if.mjs';
4
+ import type { XElse } from '../types/else.mjs';
5
+ import type { XThen } from '../types/then.mjs';
6
+ type XStaticIfReduce<Types extends unknown[], Result extends unknown = never> = (Types extends [infer Left extends unknown, ...infer Right extends unknown[]] ? XStaticIfReduce<Right, Result | Left> : Result);
7
+ export type XStaticIf<Stack extends string[], Root extends XSchema, Schema extends XIf, IfSchema extends XSchema, Then extends unknown[] = Schema extends XThen<infer ThenSchema extends XSchema> ? [XStaticSchema<Stack, Root, IfSchema> & XStaticSchema<Stack, Root, ThenSchema>] : [], Else extends unknown[] = Schema extends XElse<infer ElseSchema extends XSchema> ? [...Then, XStaticSchema<Stack, Root, ElseSchema>] : Then, Result extends unknown = Else extends [] ? unknown : XStaticIfReduce<Else>> = Result;
8
+ export {};
@@ -0,0 +1,2 @@
1
+ // deno-fmt-ignore-file
2
+ export {};
@@ -3,6 +3,7 @@ import type { XAnyOf } from '../types/anyOf.mjs';
3
3
  import type { XAllOf } from '../types/allOf.mjs';
4
4
  import type { XConst } from '../types/const.mjs';
5
5
  import type { XEnum } from '../types/enum.mjs';
6
+ import type { XIf } from '../types/if.mjs';
6
7
  import type { XItems } from '../types/items.mjs';
7
8
  import type { XOneOf } from '../types/oneOf.mjs';
8
9
  import type { XPatternProperties } from '../types/patternProperties.mjs';
@@ -18,6 +19,7 @@ import type { XStaticAllOf } from './allOf.mjs';
18
19
  import type { XStaticAnyOf } from './anyOf.mjs';
19
20
  import type { XStaticConst } from './const.mjs';
20
21
  import type { XStaticEnum } from './enum.mjs';
22
+ import type { XStaticIf } from './if.mjs';
21
23
  import type { XStaticItems } from './items.mjs';
22
24
  import type { XStaticOneOf } from './oneOf.mjs';
23
25
  import type { XStaticPatternProperties } from './patternProperties.mjs';
@@ -32,7 +34,7 @@ type TFromKeywords<Stack extends string[], Root extends XSchema, Schema extends
32
34
  Schema extends XAllOf<infer Types extends XSchema[]> ? XStaticAllOf<Stack, Root, Types> : unknown,
33
35
  Schema extends XAnyOf<infer Types extends XSchema[]> ? XStaticAnyOf<Stack, Root, Types> : unknown,
34
36
  Schema extends XConst<infer Value extends unknown> ? XStaticConst<Value> : unknown,
35
- Schema extends XEnum<infer Values extends unknown[]> ? XStaticEnum<Values> : unknown,
37
+ Schema extends XIf<infer Type extends XSchema> ? XStaticIf<Stack, Root, Schema, Type> : Schema extends XEnum<infer Values extends unknown[]> ? XStaticEnum<Values> : unknown,
36
38
  Schema extends XItems<infer Types extends XSchema[] | XSchema> ? XStaticItems<Stack, Root, Schema, Types> : unknown,
37
39
  Schema extends XOneOf<infer Types extends XSchema[]> ? XStaticOneOf<Stack, Root, Types> : unknown,
38
40
  Schema extends XPatternProperties<infer Properties extends Record<PropertyKey, XSchema>> ? XStaticPatternProperties<Stack, Root, Properties> : unknown,
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.64",
4
+ "version": "1.0.66",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
package/readme.md CHANGED
@@ -25,9 +25,13 @@ $ npm install typebox
25
25
 
26
26
  ## Usage
27
27
 
28
+ A TypeScript engine for Json Schema [Reference](https://tsplay.dev/wOyMRm)
29
+
28
30
  ```typescript
29
31
  import Type from 'typebox'
30
32
 
33
+ // Json Schema
34
+
31
35
  const T = Type.Object({ // const T = {
32
36
  x: Type.Number(), // type: 'object',
33
37
  y: Type.Number(), // required: ['x', 'y', 'z'],
@@ -43,6 +47,26 @@ type T = Type.Static<typeof T> // type T = {
43
47
  // y: number,
44
48
  // z: number
45
49
  // }
50
+
51
+ // TypeScript
52
+
53
+ const { S } = Type.Script({ T }, `
54
+ type RenameKey<K> =
55
+ K extends 'x' ? 'a' :
56
+ K extends 'y' ? 'b' :
57
+ K extends 'z' ? 'c' :
58
+ K
59
+
60
+ type S = {
61
+ readonly [K in keyof T as RenameKey<K>]: string
62
+ }
63
+ `)
64
+
65
+ type S = Type.Static<typeof S> // type S = {
66
+ // readonly a: string,
67
+ // readonly b: string,
68
+ // readonly c: string
69
+ // }
46
70
  ```
47
71
 
48
72
  ## Overview
@@ -75,9 +99,9 @@ If upgrading from `@sinclair/typebox` refer to the 1.0 migration guide at the fo
75
99
 
76
100
  ## Type
77
101
 
78
- [Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgYwgDsBneBOAXkSIDoB5YgK1Q0YACgDecSVOkzZcuAHoFcOkxbs4YypLIAuTmi4A5AK4hiqKCICUAGnkPHspVMJp9eCAKEw8t7XBI+siGpuaWNvZO0Q4uklCoAI4mwAkAJvoA2ngU9nhIfvgAXngAuv6SRcHcYRZW1jGNcnFwYNhosMCojPpaAL4NTUPDisrSeppwbqge9GZ1eHB9FSOrjS2SQZPTs-OWi8sBa8exY1JV20S74VAHRycP0i1994+PLi+U04gaIahcAGUYABDGDAGgAHmmEAwiAAfCcXN9WBwtG90aMpBM5jcVhiHi0tji6nj8ccWhdiZZXmSRh8gA)
102
+ [Documentation](https://sinclairzx81.github.io/typebox/#/docs/type/overview) | [Example](https://tsplay.dev/NaMoBN)
79
103
 
80
- TypeBox includes many functions to create Json Schema types. Each function returns a small Json Schema fragment that corresponds to a TypeScript type. TypeBox uses function composition to combine schema fragments into more complex types. It provides a set of functions that are used to model Json Schema schematics as well as a set of functions that model constructs native to JavaScript and TypeScript.
104
+ TypeBox provides many functions to create Json Schema types. Each function returns a small Json Schema fragment that can be composed into more complex types. TypeBox includes a set of functions that are used to construct Json Schema compliant schematics as well as a set of extended functions that return schematics for constructs native to JavaScript.
81
105
 
82
106
  ## Example
83
107
 
@@ -123,9 +147,9 @@ const S = Type.Number({ // const S = {
123
147
 
124
148
  ## Value
125
149
 
126
- [Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBANQIYBsCuBTOAzKERwDkMAnmBgEYQAeA9AG6qaEBQokscAKmVrvkVLkq1ViwDGEAHYBneFzgBebrwB0AeQoArDOJgAKAN4s4cagC4V5VQDk0IChij6AlABoTcEpZ7W7Dp1cPUwAvHzV-R2cXFgBfGIlpOTgAQSVEJgxVAAUkKBkMfS43OENTcorKqqraWjhJWXgUy2NTCzgARhLqnt6+vtry9ql7KOCvSwAGbv7ZuerB0284EYCocbC4Sfmd3bhFuE3VqLiXPfOdwdj01VugA)
150
+ [Documentation](https://sinclairzx81.github.io/typebox/#/docs/value/overview) | [Example](https://tsplay.dev/W4YE1w)
127
151
 
128
- The Value module provides functions that perform typed operations on JavaScript values. It includes functions such as Check, Parse, Clone, Encode, Decode and as well as advanced functions to perform structural Diff and Patch on dynamic JavaScript values.
152
+ The Value submodule provides functions for validation and other typed operations on JavaScript values. It includes functions such as Check, Parse, Clone, Encode, and Decode, as well as advanced functions for performing structural Diff and Patch operations on dynamic JavaScript values.
129
153
 
130
154
  ```typescript
131
155
  import Value from 'typebox/value'
@@ -154,9 +178,9 @@ const A = Value.Parse(T, { // const A: {
154
178
 
155
179
  ## Compile
156
180
 
157
- [Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1IChRJY4AKlRxES5YXXrcurAHYBneMjgBeFGw4AKIdQB0AeRoArLMxiakcK9Zu3bjRnDmKUALjgA1AIYZgAEy8YaAAeBFwAGkFDEzNQrit6dx0sXQA5AFcQGiwoTQBKSLsi4pKrB2tEwQysnPD4uAok4TTM7NyC0s6u8qtGqtba+oAvJr1qtvyuqZKeuBH+mqguXDy86fWNm3LcAD4drhkIBXgAQVUUXQAFLyh5LAtNjfLnU-cEesqABkLH37tZyqyAZQOq9dwARh+f2hcFmfSBi1Bc3cnxhaNhjis8wRbWWa3RBO2510JKAA)
181
+ [Documentation](https://sinclairzx81.github.io/typebox/#/docs/compile/overview) | [Example](https://tsplay.dev/WyraZw)
158
182
 
159
- The Compile module is a high-performance JIT compiler that transforms types into efficient runtime validators. The compiler is optimized for both fast compilation and validation.
183
+ The Compile submodule is a high-performance Json Schema compliant JIT compiler that compiles schematics into efficient runtime validators. The compiler is optimized for fast compilation and validation and is known to be one of the fastest validation solutions available for JavaScript.
160
184
 
161
185
  ```typescript
162
186
  import { Compile } from 'typebox/compile'
@@ -182,9 +206,9 @@ const A = C.Parse({ // const A: {
182
206
 
183
207
  ## Script
184
208
 
185
- [Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgGUBjKYMGVAE0SOqYgB2AZ3gI4AXh5oAdM1bsAFAAMA3nEpw4ZAFxwBAVxDFUUADTrNSXSNYCA5uY1wAXrtIQANqgCGA9QF8lAEpNULDwiMio6PC6OH5hUV0EAHliACtUJhgAHhUnGMKi4rC4zR1EADlDYzMCkobG0vpLZIYYWwd6pp7ispdkgCEITx8Bbt7JqLj-AD5qOIApIUFGJgALVBBvOABJA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2OhwA6KGBgE9rbqZfgqJlwWkYfOSYJ59D7Y6nc6XcF+VIwv---9DzwCBiAAK1QJ97TgShvzAbAIwKXgAL-Q9nnhOBxj8d5MQw8Z3gJXC-DgMlv1pABHRRgFpdRkJQ+hmQ7PB3jwCdmJXcUgA)
209
+ [Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://tsplay.dev/Wk6L1m) | [Example 2](https://tsplay.dev/NnrJoN)
186
210
 
187
- TypeBox includes a TypeScript scripting engine able to parse TypeScript types directly into Json Schema. The engine uses symmetric runtime and type-level parsing and returns typed safe schematics from TypeScript types (including computed types). The engine is designed for TypeScript 7 native compiler but is supported in TypeScript 5 and above.
211
+ TypeBox is a runtime TypeScript DSL engine that can create, transform, and compute Json Schema using native TypeScript syntax. The engine is implemented symmetrically at runtime and within the TypeScript type system, and is intended for use with the TypeScript 7 native compiler and above.
188
212
 
189
213
  ```typescript
190
214
  // Scripted Type
@@ -228,7 +252,7 @@ type S = Type.Static<typeof S> // type S = {
228
252
 
229
253
  ## Schema
230
254
 
231
- [Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgYQuYAbApnAvnAMyjTgHIYBPMLAIwgA8B6AYzTEy1ICgfWA7AM7xkcALwo2HABQIucOJWoAuMhBoArLMxikANHLhQsARwCuwIwBMVAbVL09ZCo9IAvUgF198sMWqxgLAEVWXl5ehCFKiwVUj5TEBosKFI8bzCKSMUYsnjE5NTcdPlXLOjYvKSUvANcLlwASl4IQXgAQTEUADoABQBDKAEsGTDRsfGJycZGOH4hODaQgwi4AAZdSc2t7YnpsJXK5PTMtY2d84uxvfkTw6h00rgARkvXy+u4R7v6hre-95m+HEXRBQA) | [Example 2](https://tsplay.dev/mxrl7w)
255
+ [Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://tsplay.dev/Wvrv3W) | [Example 2](https://tsplay.dev/m3g0ym)
232
256
 
233
257
  TypeBox is built upon a high-performance validation infrastructure that supports the direct compilation and inference of Json Schema schematics. TypeBox implements Draft 3 to 2020-12 and is compliance tested via the official Json Schema [Test Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite). It offers high-performance JIT compilation with automatic fallback to dynamic checking in JIT restricted environments.
234
258