@nmtjs/type 0.6.4 → 0.7.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 (76) hide show
  1. package/dist/index.js +31 -31
  2. package/dist/index.js.map +1 -1
  3. package/dist/temporal.js +7 -8
  4. package/dist/temporal.js.map +1 -1
  5. package/dist/types/any.js +4 -4
  6. package/dist/types/any.js.map +1 -1
  7. package/dist/types/array.js +23 -23
  8. package/dist/types/array.js.map +1 -1
  9. package/dist/types/base.js +65 -66
  10. package/dist/types/base.js.map +1 -1
  11. package/dist/types/boolean.js +4 -4
  12. package/dist/types/boolean.js.map +1 -1
  13. package/dist/types/custom.js +10 -9
  14. package/dist/types/custom.js.map +1 -1
  15. package/dist/types/date.js +6 -13
  16. package/dist/types/date.js.map +1 -1
  17. package/dist/types/enum.js +7 -16
  18. package/dist/types/enum.js.map +1 -1
  19. package/dist/types/literal.js +7 -6
  20. package/dist/types/literal.js.map +1 -1
  21. package/dist/types/never.js +4 -4
  22. package/dist/types/never.js.map +1 -1
  23. package/dist/types/number.js +26 -93
  24. package/dist/types/number.js.map +1 -1
  25. package/dist/types/object.js +42 -31
  26. package/dist/types/object.js.map +1 -1
  27. package/dist/types/string.js +31 -47
  28. package/dist/types/string.js.map +1 -1
  29. package/dist/types/temporal.js +40 -41
  30. package/dist/types/temporal.js.map +1 -1
  31. package/dist/types/union.js +25 -18
  32. package/dist/types/union.js.map +1 -1
  33. package/dist/utils.js +0 -1
  34. package/dist/utils.js.map +1 -1
  35. package/package.json +7 -19
  36. package/src/index.ts +24 -25
  37. package/src/temporal.ts +8 -8
  38. package/src/types/any.ts +5 -3
  39. package/src/types/array.ts +24 -22
  40. package/src/types/base.ts +148 -81
  41. package/src/types/boolean.ts +5 -3
  42. package/src/types/custom.ts +43 -24
  43. package/src/types/date.ts +17 -16
  44. package/src/types/enum.ts +12 -22
  45. package/src/types/literal.ts +9 -6
  46. package/src/types/never.ts +5 -3
  47. package/src/types/number.ts +31 -93
  48. package/src/types/object.ts +44 -37
  49. package/src/types/string.ts +41 -39
  50. package/src/types/temporal.ts +72 -32
  51. package/src/types/union.ts +59 -50
  52. package/src/utils.ts +22 -22
  53. package/dist/compiler.js +0 -55
  54. package/dist/compiler.js.map +0 -1
  55. package/dist/formats.js +0 -127
  56. package/dist/formats.js.map +0 -1
  57. package/dist/inference.js +0 -1
  58. package/dist/inference.js.map +0 -1
  59. package/dist/parse.js +0 -145
  60. package/dist/parse.js.map +0 -1
  61. package/dist/runtime.js +0 -73
  62. package/dist/runtime.js.map +0 -1
  63. package/dist/schemas/default.js +0 -6
  64. package/dist/schemas/default.js.map +0 -1
  65. package/dist/schemas/discriminated-union.js +0 -9
  66. package/dist/schemas/discriminated-union.js.map +0 -1
  67. package/dist/schemas/nullable.js +0 -11
  68. package/dist/schemas/nullable.js.map +0 -1
  69. package/src/compiler.ts +0 -100
  70. package/src/formats.ts +0 -182
  71. package/src/inference.ts +0 -128
  72. package/src/parse.ts +0 -217
  73. package/src/runtime.ts +0 -137
  74. package/src/schemas/default.ts +0 -12
  75. package/src/schemas/discriminated-union.ts +0 -49
  76. package/src/schemas/nullable.ts +0 -20
package/src/parse.ts DELETED
@@ -1,217 +0,0 @@
1
- // --------------------------------------------------------------------------
2
- // Iterators
3
- // --------------------------------------------------------------------------
4
-
5
- /** Returns true if this value is an async iterator */
6
- export function IsAsyncIterator(value) {
7
- return IsObject(value) && Symbol.asyncIterator in value
8
- }
9
- /** Returns true if this value is an iterator */
10
- export function IsIterator(value) {
11
- return IsObject(value) && Symbol.iterator in value
12
- }
13
- // --------------------------------------------------------------------------
14
- // Object Instances
15
- // --------------------------------------------------------------------------
16
- /** Returns true if this value is not an instance of a class */
17
- export function IsStandardObject(value) {
18
- return (
19
- IsObject(value) &&
20
- (Object.getPrototypeOf(value) === Object.prototype ||
21
- Object.getPrototypeOf(value) === null)
22
- )
23
- }
24
- /** Returns true if this value is an instance of a class */
25
- export function IsInstanceObject(value) {
26
- return (
27
- IsObject(value) &&
28
- !IsArray(value) &&
29
- IsFunction(value.constructor) &&
30
- value.constructor.name !== 'Object'
31
- )
32
- }
33
- // --------------------------------------------------------------------------
34
- // JavaScript
35
- // --------------------------------------------------------------------------
36
- /** Returns true if this value is a Promise */
37
- export function IsPromise(value) {
38
- return value instanceof Promise
39
- }
40
- /** Returns true if this value is a Date */
41
- export function IsDate(value) {
42
- return value instanceof Date && Number.isFinite(value.getTime())
43
- }
44
- /** Returns true if this value is an instance of Map<K, T> */
45
- export function IsMap(value) {
46
- return value instanceof globalThis.Map
47
- }
48
- /** Returns true if this value is an instance of Set<T> */
49
- export function IsSet(value) {
50
- return value instanceof globalThis.Set
51
- }
52
- /** Returns true if this value is RegExp */
53
- export function IsRegExp(value) {
54
- return value instanceof globalThis.RegExp
55
- }
56
- /** Returns true if this value is a typed array */
57
- export function IsTypedArray(value) {
58
- return ArrayBuffer.isView(value)
59
- }
60
- /** Returns true if the value is a Int8Array */
61
- export function IsInt8Array(value) {
62
- return value instanceof globalThis.Int8Array
63
- }
64
- /** Returns true if the value is a Uint8Array */
65
- export function IsUint8Array(value) {
66
- return value instanceof globalThis.Uint8Array
67
- }
68
- /** Returns true if the value is a Uint8ClampedArray */
69
- export function IsUint8ClampedArray(value) {
70
- return value instanceof globalThis.Uint8ClampedArray
71
- }
72
- /** Returns true if the value is a Int16Array */
73
- export function IsInt16Array(value) {
74
- return value instanceof globalThis.Int16Array
75
- }
76
- /** Returns true if the value is a Uint16Array */
77
- export function IsUint16Array(value) {
78
- return value instanceof globalThis.Uint16Array
79
- }
80
- /** Returns true if the value is a Int32Array */
81
- export function IsInt32Array(value) {
82
- return value instanceof globalThis.Int32Array
83
- }
84
- /** Returns true if the value is a Uint32Array */
85
- export function IsUint32Array(value) {
86
- return value instanceof globalThis.Uint32Array
87
- }
88
- /** Returns true if the value is a Float32Array */
89
- export function IsFloat32Array(value) {
90
- return value instanceof globalThis.Float32Array
91
- }
92
- /** Returns true if the value is a Float64Array */
93
- export function IsFloat64Array(value) {
94
- return value instanceof globalThis.Float64Array
95
- }
96
- /** Returns true if the value is a BigInt64Array */
97
- export function IsBigInt64Array(value) {
98
- return value instanceof globalThis.BigInt64Array
99
- }
100
- /** Returns true if the value is a BigUint64Array */
101
- export function IsBigUint64Array(value) {
102
- return value instanceof globalThis.BigUint64Array
103
- }
104
- // --------------------------------------------------------------------------
105
- // PropertyKey
106
- // --------------------------------------------------------------------------
107
- /** Returns true if this value has this property key */
108
- export function HasPropertyKey(value, key) {
109
- return key in value
110
- }
111
- // --------------------------------------------------------------------------
112
- // Standard
113
- // --------------------------------------------------------------------------
114
- /** Returns true of this value is an object type */
115
- export function IsObject(value) {
116
- return value !== null && typeof value === 'object'
117
- }
118
- /** Returns true if this value is an array, but not a typed array */
119
- export function IsArray(value) {
120
- return Array.isArray(value) && !ArrayBuffer.isView(value)
121
- }
122
- /** Returns true if this value is an undefined */
123
- export function IsUndefined(value) {
124
- return value === undefined
125
- }
126
- /** Returns true if this value is an null */
127
- export function IsNull(value) {
128
- return value === null
129
- }
130
- /** Returns true if this value is an boolean */
131
- export function IsBoolean(value) {
132
- return typeof value === 'boolean'
133
- }
134
- /** Returns true if this value is an number */
135
- export function IsNumber(value) {
136
- return typeof value === 'number'
137
- }
138
- /** Returns true if this value is an integer */
139
- export function IsInteger(value) {
140
- return Number.isInteger(value)
141
- }
142
- /** Returns true if this value is bigint */
143
- export function IsBigInt(value) {
144
- return typeof value === 'bigint'
145
- }
146
- /** Returns true if this value is string */
147
- export function IsString(value) {
148
- return typeof value === 'string'
149
- }
150
- /** Returns true if this value is a function */
151
- export function IsFunction(value) {
152
- return typeof value === 'function'
153
- }
154
- /** Returns true if this value is a symbol */
155
- export function IsSymbol(value) {
156
- return typeof value === 'symbol'
157
- }
158
- /** Returns true if this value is a value type such as number, string, boolean */
159
- export function IsValueType(value) {
160
- // prettier-ignore
161
- return (
162
- IsBigInt(value) ||
163
- IsBoolean(value) ||
164
- IsNull(value) ||
165
- IsNumber(value) ||
166
- IsString(value) ||
167
- IsSymbol(value) ||
168
- IsUndefined(value)
169
- )
170
- }
171
-
172
- // ------------------------------------------------------------------
173
- // Clonable
174
- // ------------------------------------------------------------------
175
- function FromObject(value, exclude) {
176
- const Acc = {}
177
- for (const key of Object.getOwnPropertyNames(value)) {
178
- Acc[key] = Clone(value[key], exclude)
179
- }
180
- for (const key of Object.getOwnPropertySymbols(value)) {
181
- Acc[key] = Clone(value[key], exclude)
182
- }
183
- return Acc
184
- }
185
- function FromArray(value, exclude) {
186
- return value.map((element) => Clone(element, exclude))
187
- }
188
- function FromTypedArray(value) {
189
- return value.slice()
190
- }
191
- function FromMap(value, exclude) {
192
- return new Map(Clone([...value.entries()], exclude))
193
- }
194
- function FromSet(value, exclude) {
195
- return new Set(Clone([...value.entries()], exclude))
196
- }
197
- function FromDate(value) {
198
- return new Date(value.toISOString())
199
- }
200
- function FromValue(value) {
201
- return value
202
- }
203
- // ------------------------------------------------------------------
204
- // Clone
205
- // ------------------------------------------------------------------
206
- /** Returns a clone of the given value */
207
- export function Clone(value, exclude?: Set<any>) {
208
- if (IsArray(value)) return FromArray(value, exclude)
209
- if (IsDate(value)) return FromDate(value)
210
- if (IsTypedArray(value)) return FromTypedArray(value)
211
- if (IsMap(value)) return FromMap(value, exclude)
212
- if (IsSet(value)) return FromSet(value, exclude)
213
- if (IsObject(value)) return FromObject(value, exclude)
214
- if (IsValueType(value)) return FromValue(value)
215
- if (exclude?.has(value.constructor)) return value
216
- throw new Error('Cannot clone value')
217
- }
package/src/runtime.ts DELETED
@@ -1,137 +0,0 @@
1
- import type { ClassConstructor } from '@nmtjs/common'
2
- import type { TSchema } from '@sinclair/typebox'
3
- import type { ValueErrorIterator } from '@sinclair/typebox/compiler'
4
- import {
5
- TransformDecode,
6
- TransformEncode,
7
- Value,
8
- } from '@sinclair/typebox/value'
9
- import { register } from './formats.ts'
10
- import type {
11
- StaticInputEncode,
12
- StaticOutputDecode,
13
- StaticOutputEncode,
14
- } from './inference.ts'
15
- import { Clone } from './parse.ts'
16
- import { IsDiscriminatedUnion } from './schemas/discriminated-union.ts'
17
- import type { BaseType } from './types/base.ts'
18
-
19
- // register ajv formats
20
- register()
21
-
22
- export type CloneOptions = {
23
- clone?: boolean
24
- exclude?: Set<any>
25
- }
26
-
27
- export type ValidationError = {
28
- path: string
29
- message: string
30
- value: unknown
31
- }
32
-
33
- // TODO: this one is very slow
34
- export function _applyDefaults(schema: TSchema, value: any) {
35
- return Value.Default(schema, value)
36
- }
37
-
38
- // TODO: this one is very slow
39
- // Clone -> Clean -> Convert
40
- export function _parse(
41
- schema: TSchema,
42
- value: any,
43
- cloneOptions?: CloneOptions,
44
- ) {
45
- if (cloneOptions?.clone !== false) {
46
- value = Clone(value, cloneOptions?.exclude)
47
- }
48
- return Value.Clean(schema, Value.Convert(schema, value))
49
- }
50
-
51
- export function _traversErrors(errors: ValueErrorIterator) {
52
- const result: ValidationError[] = []
53
-
54
- for (const error of errors) {
55
- if (IsDiscriminatedUnion(error.schema)) {
56
- const discriminator = error.schema.discriminator
57
- const discriminatorValue = error.value?.[discriminator]
58
- if (discriminatorValue !== undefined) {
59
- const variantSchema = error.schema.anyOf.find(
60
- (schema) =>
61
- schema.properties[discriminator].const === discriminatorValue,
62
- )
63
- if (variantSchema) {
64
- const propertiesSchemas: TSchema[] = []
65
- for (const element in variantSchema.properties) {
66
- const propertySchema = variantSchema.properties[element]
67
- if (propertySchema !== variantSchema.properties[discriminator]) {
68
- propertiesSchemas.push(propertySchema)
69
- }
70
- }
71
-
72
- for (const iter of error.errors) {
73
- for (const err of iter) {
74
- if (!propertiesSchemas.includes(err.schema)) continue
75
- result.push({
76
- path: err.path,
77
- message: err.message,
78
- value: err.value,
79
- })
80
- }
81
- }
82
-
83
- continue
84
- }
85
- }
86
- }
87
-
88
- result.push({
89
- path: error.path,
90
- message: error.message,
91
- value: error.value,
92
- })
93
-
94
- for (const nestedError of error.errors) {
95
- result.push(..._traversErrors(nestedError))
96
- }
97
- }
98
-
99
- return result
100
- }
101
-
102
- export function applyDefaults(type: BaseType, value: unknown) {
103
- return _applyDefaults(type.schema, value)
104
- }
105
-
106
- export function parse(
107
- type: BaseType,
108
- value: unknown,
109
- cloneOptions?: CloneOptions,
110
- ) {
111
- return _parse(type.schema, value, cloneOptions)
112
- }
113
-
114
- export function errors(type: BaseType, value: unknown): ValidationError[] {
115
- return _traversErrors(Value.Errors(type.schema, value))
116
- }
117
-
118
- export function check<T extends BaseType>(
119
- type: T,
120
- value: unknown,
121
- ): value is StaticInputEncode<T['schema']> {
122
- return Value.Check(type.schema, value)
123
- }
124
-
125
- export function decode<T extends BaseType>(
126
- type: T,
127
- value: unknown,
128
- ): StaticOutputDecode<T['schema']> {
129
- return TransformDecode(type.schema, [], value)
130
- }
131
-
132
- export function encode<T extends BaseType>(
133
- type: T,
134
- value: unknown,
135
- ): StaticOutputEncode<T['schema']> {
136
- return TransformEncode(type.schema, [], value)
137
- }
@@ -1,12 +0,0 @@
1
- import type { TSchema } from '@sinclair/typebox'
2
-
3
- export type TDefault<Type extends TSchema, Default = unknown> = Type & {
4
- default: Default
5
- }
6
-
7
- export function Default<Type extends TSchema, const Default>(
8
- type: Type,
9
- default_: Default,
10
- ): TDefault<Type, Default> {
11
- return { ...type, default: default_ } as never
12
- }
@@ -1,49 +0,0 @@
1
- import {
2
- KindGuard,
3
- type TLiteral,
4
- type TObject,
5
- type TPropertyKey,
6
- type TSchema,
7
- type TUnion,
8
- Type,
9
- TypeGuard,
10
- ValueGuard,
11
- } from '@sinclair/typebox'
12
-
13
- export function IsDiscriminatedUnion(
14
- schema: TSchema,
15
- ): schema is TDiscriminatedUnion {
16
- return (
17
- TypeGuard.IsUnion(schema) &&
18
- 'discriminator' in schema &&
19
- ValueGuard.IsString(schema.discriminator) &&
20
- schema.anyOf.every(
21
- (variant) =>
22
- KindGuard.IsObject(variant) &&
23
- KindGuard.IsLiteralString(variant.properties[schema.discriminator]),
24
- )
25
- )
26
- }
27
-
28
- export type DiscriminatedUnionProperties<K extends string = string> = {
29
- [OK in K]: TLiteral<any>
30
- } & {
31
- [OK in TPropertyKey]: any
32
- }
33
-
34
- export interface TDiscriminatedUnion<
35
- K extends string = string,
36
- T extends TObject<DiscriminatedUnionProperties<K>>[] = TObject<
37
- DiscriminatedUnionProperties<K>
38
- >[],
39
- > extends TUnion<T> {
40
- discriminator: K
41
- anyOf: T
42
- }
43
-
44
- export function DiscriminatedUnion<
45
- K extends string,
46
- T extends TObject<DiscriminatedUnionProperties<K>>[],
47
- >(key: K, types: T): TDiscriminatedUnion<K, T> {
48
- return Type.Union(types, { discriminator: key }) as any
49
- }
@@ -1,20 +0,0 @@
1
- import {
2
- type SchemaOptions,
3
- type TNull,
4
- type TSchema,
5
- type TUnion,
6
- Type,
7
- } from '@sinclair/typebox/type'
8
-
9
- export type TNullable<T extends TSchema> = TUnion<[T, TNull]>
10
- export const Nullable = <T extends TSchema>(
11
- schema: T,
12
- options: SchemaOptions = {},
13
- ) => {
14
- const { default: _default } = schema
15
-
16
- return Type.Union([schema, Type.Null()], {
17
- default: _default,
18
- ...options,
19
- })
20
- }