@sinclair/typebox 0.25.3 → 0.25.5
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/conditional/conditional.d.ts +1 -1
- package/conditional/conditional.js +1 -1
- package/errors/errors.d.ts +1 -0
- package/errors/errors.js +1 -0
- package/format/format.d.ts +1 -1
- package/format/format.js +1 -1
- package/guard/guard.d.ts +1 -1
- package/guard/guard.js +1 -1
- package/package.json +1 -1
- package/readme.md +3 -3
- package/value/delta.d.ts +39 -18
- package/value/delta.js +42 -14
- package/value/pointer.d.ts +1 -1
- package/value/pointer.js +1 -1
- package/value/value.d.ts +4 -4
- package/value/value.js +7 -2
|
@@ -6,7 +6,7 @@ export interface TExclude<T extends Types.TUnion, U extends Types.TUnion> extend
|
|
|
6
6
|
export interface TExtract<T extends Types.TSchema, U extends Types.TUnion> extends Types.TUnion<any[]> {
|
|
7
7
|
static: Extract<Types.Static<T, this['params']>, Types.Static<U, this['params']>>;
|
|
8
8
|
}
|
|
9
|
-
/** Conditional
|
|
9
|
+
/** Conditional type mapping for TypeBox types */
|
|
10
10
|
export declare namespace Conditional {
|
|
11
11
|
/** (Experimental) Creates a conditional expression type */
|
|
12
12
|
function Extends<L extends Types.TSchema, R extends Types.TSchema, T extends Types.TSchema, U extends Types.TSchema>(left: L, right: R, ok: T, fail: U): TExtends<L, R, T, U>;
|
|
@@ -31,7 +31,7 @@ exports.Conditional = void 0;
|
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const structural_1 = require("./structural");
|
|
33
33
|
const index_1 = require("../guard/index");
|
|
34
|
-
/** Conditional
|
|
34
|
+
/** Conditional type mapping for TypeBox types */
|
|
35
35
|
var Conditional;
|
|
36
36
|
(function (Conditional) {
|
|
37
37
|
/** (Experimental) Creates a conditional expression type */
|
package/errors/errors.d.ts
CHANGED
|
@@ -60,6 +60,7 @@ export declare class ValueErrorsUnknownTypeError extends Error {
|
|
|
60
60
|
readonly schema: Types.TSchema;
|
|
61
61
|
constructor(schema: Types.TSchema);
|
|
62
62
|
}
|
|
63
|
+
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
63
64
|
export declare namespace ValueErrors {
|
|
64
65
|
function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): IterableIterator<ValueError>;
|
|
65
66
|
}
|
package/errors/errors.js
CHANGED
|
@@ -94,6 +94,7 @@ class ValueErrorsUnknownTypeError extends Error {
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
exports.ValueErrorsUnknownTypeError = ValueErrorsUnknownTypeError;
|
|
97
|
+
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
97
98
|
var ValueErrors;
|
|
98
99
|
(function (ValueErrors) {
|
|
99
100
|
function* Any(schema, references, path, value) { }
|
package/format/format.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare type FormatValidationFunction = (value: string) => boolean;
|
|
2
|
-
/**
|
|
2
|
+
/** Provides functions to create custom string formats */
|
|
3
3
|
export declare namespace Format {
|
|
4
4
|
/** Clears all formats */
|
|
5
5
|
function Clear(): void;
|
package/format/format.js
CHANGED
|
@@ -28,7 +28,7 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.Format = void 0;
|
|
31
|
-
/**
|
|
31
|
+
/** Provides functions to create custom string formats */
|
|
32
32
|
var Format;
|
|
33
33
|
(function (Format) {
|
|
34
34
|
const formats = new Map();
|
package/guard/guard.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare class TypeGuardInvalidTypeError extends Error {
|
|
|
3
3
|
readonly schema: unknown;
|
|
4
4
|
constructor(schema: unknown);
|
|
5
5
|
}
|
|
6
|
-
/**
|
|
6
|
+
/** Provides functionality to test if values are TypeBox types */
|
|
7
7
|
export declare namespace TypeGuard {
|
|
8
8
|
/** Returns true if the given schema is TAny */
|
|
9
9
|
function TAny(schema: unknown): schema is Types.TAny;
|
package/guard/guard.js
CHANGED
|
@@ -36,7 +36,7 @@ class TypeGuardInvalidTypeError extends Error {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
exports.TypeGuardInvalidTypeError = TypeGuardInvalidTypeError;
|
|
39
|
-
/**
|
|
39
|
+
/** Provides functionality to test if values are TypeBox types */
|
|
40
40
|
var TypeGuard;
|
|
41
41
|
(function (TypeGuard) {
|
|
42
42
|
function IsObject(value) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -832,7 +832,7 @@ const R = Value.Equal( // const R = true
|
|
|
832
832
|
Use the Diff function to produce a sequence of edits to transform one value into another.
|
|
833
833
|
|
|
834
834
|
```typescript
|
|
835
|
-
const E = Value.Diff
|
|
835
|
+
const E = Value.Diff( // const E = [
|
|
836
836
|
{ x: 1, y: 2, z: 3 }, // { type: 'update', path: '/y', value: 4 },
|
|
837
837
|
{ y: 4, z: 5, w: 6 } // { type: 'update', path: '/z', value: 5 },
|
|
838
838
|
) // { type: 'insert', path: '/w', value: 6 },
|
|
@@ -851,12 +851,12 @@ const A = { x: 1, y: 2 }
|
|
|
851
851
|
|
|
852
852
|
const B = { x: 3 }
|
|
853
853
|
|
|
854
|
-
const E = Value.Diff
|
|
854
|
+
const E = Value.Diff(A, B) // const E = [
|
|
855
855
|
// { type: 'update', path: '/x', value: 3 },
|
|
856
856
|
// { type: 'delete', path: '/y' }
|
|
857
857
|
// ]
|
|
858
858
|
|
|
859
|
-
const C = Value.Patch<
|
|
859
|
+
const C = Value.Patch<typeof B>(A, E) // const C = { x: 3 }
|
|
860
860
|
```
|
|
861
861
|
|
|
862
862
|
|
package/value/delta.d.ts
CHANGED
|
@@ -1,22 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
type:
|
|
5
|
-
path: string
|
|
6
|
-
value:
|
|
1
|
+
import { Static } from '../typebox';
|
|
2
|
+
export declare type Insert = Static<typeof Insert>;
|
|
3
|
+
export declare const Insert: import("../typebox").TObject<{
|
|
4
|
+
type: import("../typebox").TLiteral<"insert">;
|
|
5
|
+
path: import("../typebox").TString<string>;
|
|
6
|
+
value: import("../typebox").TUnknown;
|
|
7
|
+
}>;
|
|
8
|
+
export declare type Update = Static<typeof Update>;
|
|
9
|
+
export declare const Update: import("../typebox").TObject<{
|
|
10
|
+
type: import("../typebox").TLiteral<"update">;
|
|
11
|
+
path: import("../typebox").TString<string>;
|
|
12
|
+
value: import("../typebox").TUnknown;
|
|
13
|
+
}>;
|
|
14
|
+
export declare type Delete = Static<typeof Delete>;
|
|
15
|
+
export declare const Delete: import("../typebox").TObject<{
|
|
16
|
+
type: import("../typebox").TLiteral<"delete">;
|
|
17
|
+
path: import("../typebox").TString<string>;
|
|
18
|
+
}>;
|
|
19
|
+
export declare type Edit = Static<typeof Edit>;
|
|
20
|
+
export declare const Edit: import("../typebox").TUnion<[import("../typebox").TObject<{
|
|
21
|
+
type: import("../typebox").TLiteral<"insert">;
|
|
22
|
+
path: import("../typebox").TString<string>;
|
|
23
|
+
value: import("../typebox").TUnknown;
|
|
24
|
+
}>, import("../typebox").TObject<{
|
|
25
|
+
type: import("../typebox").TLiteral<"update">;
|
|
26
|
+
path: import("../typebox").TString<string>;
|
|
27
|
+
value: import("../typebox").TUnknown;
|
|
28
|
+
}>, import("../typebox").TObject<{
|
|
29
|
+
type: import("../typebox").TLiteral<"delete">;
|
|
30
|
+
path: import("../typebox").TString<string>;
|
|
31
|
+
}>]>;
|
|
32
|
+
export declare class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
33
|
+
readonly key: unknown;
|
|
34
|
+
constructor(key: unknown);
|
|
7
35
|
}
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
path: string;
|
|
12
|
-
value: any;
|
|
13
|
-
}
|
|
14
|
-
export interface Delete<T> {
|
|
15
|
-
brand: T;
|
|
16
|
-
type: 'delete';
|
|
17
|
-
path: string;
|
|
36
|
+
export declare class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
37
|
+
readonly value: unknown;
|
|
38
|
+
constructor(value: unknown);
|
|
18
39
|
}
|
|
19
40
|
export declare namespace ValueDelta {
|
|
20
|
-
function Diff
|
|
21
|
-
function Patch<T>(current:
|
|
41
|
+
function Diff(current: unknown, next: unknown): Edit[];
|
|
42
|
+
function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
22
43
|
}
|
package/value/delta.js
CHANGED
|
@@ -27,10 +27,46 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueDelta = void 0;
|
|
30
|
+
exports.ValueDelta = exports.ValueDeltaUnableToDiffUnknownValue = exports.ValueDeltaObjectWithSymbolKeyError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
|
|
31
|
+
const typebox_1 = require("../typebox");
|
|
31
32
|
const is_1 = require("./is");
|
|
32
33
|
const clone_1 = require("./clone");
|
|
33
34
|
const pointer_1 = require("./pointer");
|
|
35
|
+
exports.Insert = typebox_1.Type.Object({
|
|
36
|
+
type: typebox_1.Type.Literal('insert'),
|
|
37
|
+
path: typebox_1.Type.String(),
|
|
38
|
+
value: typebox_1.Type.Unknown(),
|
|
39
|
+
});
|
|
40
|
+
exports.Update = typebox_1.Type.Object({
|
|
41
|
+
type: typebox_1.Type.Literal('update'),
|
|
42
|
+
path: typebox_1.Type.String(),
|
|
43
|
+
value: typebox_1.Type.Unknown(),
|
|
44
|
+
});
|
|
45
|
+
exports.Delete = typebox_1.Type.Object({
|
|
46
|
+
type: typebox_1.Type.Literal('delete'),
|
|
47
|
+
path: typebox_1.Type.String(),
|
|
48
|
+
});
|
|
49
|
+
exports.Edit = typebox_1.Type.Union([exports.Insert, exports.Update, exports.Delete]);
|
|
50
|
+
// ---------------------------------------------------------------------
|
|
51
|
+
// Errors
|
|
52
|
+
// ---------------------------------------------------------------------
|
|
53
|
+
class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
54
|
+
constructor(key) {
|
|
55
|
+
super('ValueDelta: Cannot diff objects with symbol keys');
|
|
56
|
+
this.key = key;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.ValueDeltaObjectWithSymbolKeyError = ValueDeltaObjectWithSymbolKeyError;
|
|
60
|
+
class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
61
|
+
constructor(value) {
|
|
62
|
+
super('ValueDelta: Unable to create diff edits for unknown value');
|
|
63
|
+
this.value = value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.ValueDeltaUnableToDiffUnknownValue = ValueDeltaUnableToDiffUnknownValue;
|
|
67
|
+
// ---------------------------------------------------------------------
|
|
68
|
+
// ValueDelta
|
|
69
|
+
// ---------------------------------------------------------------------
|
|
34
70
|
var ValueDelta;
|
|
35
71
|
(function (ValueDelta) {
|
|
36
72
|
// ---------------------------------------------------------------------
|
|
@@ -55,7 +91,7 @@ var ValueDelta;
|
|
|
55
91
|
const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
|
|
56
92
|
for (const key of currentKeys) {
|
|
57
93
|
if (typeof key === 'symbol')
|
|
58
|
-
throw
|
|
94
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
59
95
|
if (next[key] === undefined && nextKeys.includes(key))
|
|
60
96
|
yield Update(`${path}/${String(key)}`, undefined);
|
|
61
97
|
}
|
|
@@ -63,27 +99,22 @@ var ValueDelta;
|
|
|
63
99
|
if (current[key] === undefined || next[key] === undefined)
|
|
64
100
|
continue;
|
|
65
101
|
if (typeof key === 'symbol')
|
|
66
|
-
throw
|
|
102
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
67
103
|
yield* Visit(`${path}/${String(key)}`, current[key], next[key]);
|
|
68
104
|
}
|
|
69
105
|
for (const key of nextKeys) {
|
|
70
106
|
if (typeof key === 'symbol')
|
|
71
|
-
throw
|
|
107
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
72
108
|
if (current[key] === undefined)
|
|
73
109
|
yield Insert(`${path}/${String(key)}`, next[key]);
|
|
74
110
|
}
|
|
75
111
|
for (const key of currentKeys.reverse()) {
|
|
76
112
|
if (typeof key === 'symbol')
|
|
77
|
-
throw
|
|
113
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
78
114
|
if (next[key] === undefined && !nextKeys.includes(key))
|
|
79
115
|
yield Delete(`${path}/${String(key)}`);
|
|
80
116
|
}
|
|
81
117
|
}
|
|
82
|
-
function* Date(path, current, next) {
|
|
83
|
-
if (is_1.Is.Date(next) && current.getTime() === next.getTime())
|
|
84
|
-
return;
|
|
85
|
-
yield Update(path, next);
|
|
86
|
-
}
|
|
87
118
|
function* Array(path, current, next) {
|
|
88
119
|
if (!is_1.Is.Array(next))
|
|
89
120
|
return yield Update(path, next);
|
|
@@ -117,9 +148,6 @@ var ValueDelta;
|
|
|
117
148
|
if (is_1.Is.Object(current)) {
|
|
118
149
|
return yield* Object(path, current, next);
|
|
119
150
|
}
|
|
120
|
-
else if (is_1.Is.Date(current)) {
|
|
121
|
-
return yield* Date(path, current, next);
|
|
122
|
-
}
|
|
123
151
|
else if (is_1.Is.Array(current)) {
|
|
124
152
|
return yield* Array(path, current, next);
|
|
125
153
|
}
|
|
@@ -130,7 +158,7 @@ var ValueDelta;
|
|
|
130
158
|
return yield* Value(path, current, next);
|
|
131
159
|
}
|
|
132
160
|
else {
|
|
133
|
-
throw new
|
|
161
|
+
throw new ValueDeltaUnableToDiffUnknownValue(current);
|
|
134
162
|
}
|
|
135
163
|
}
|
|
136
164
|
function Diff(current, next) {
|
package/value/pointer.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare class ValuePointerRootDeleteError extends Error {
|
|
|
9
9
|
readonly path: string;
|
|
10
10
|
constructor(value: unknown, path: string);
|
|
11
11
|
}
|
|
12
|
-
/**
|
|
12
|
+
/** Provides functionality to update values through RFC6901 string pointers */
|
|
13
13
|
export declare namespace ValuePointer {
|
|
14
14
|
/** Formats the given pointer into navigable key components */
|
|
15
15
|
function Format(pointer: string): IterableIterator<string>;
|
package/value/pointer.js
CHANGED
|
@@ -45,7 +45,7 @@ class ValuePointerRootDeleteError extends Error {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
exports.ValuePointerRootDeleteError = ValuePointerRootDeleteError;
|
|
48
|
-
/**
|
|
48
|
+
/** Provides functionality to update values through RFC6901 string pointers */
|
|
49
49
|
var ValuePointer;
|
|
50
50
|
(function (ValuePointer) {
|
|
51
51
|
function Escape(component) {
|
package/value/value.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
2
|
import { ValueError } from '../errors/index';
|
|
3
3
|
import { Edit } from './delta';
|
|
4
|
-
export
|
|
5
|
-
/**
|
|
4
|
+
export { Edit, Insert, Update, Delete } from './delta';
|
|
5
|
+
/** Provides functions to perform structural updates to JavaScript values */
|
|
6
6
|
export declare namespace Value {
|
|
7
7
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
|
|
8
8
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|
|
@@ -25,7 +25,7 @@ export declare namespace Value {
|
|
|
25
25
|
/** Returns a structural clone of the given value */
|
|
26
26
|
function Clone<T>(value: T): T;
|
|
27
27
|
/** Returns edits to transform the current value into the next value */
|
|
28
|
-
function Diff
|
|
28
|
+
function Diff(current: unknown, next: unknown): Edit[];
|
|
29
29
|
/** Returns a new value with edits applied to the given value */
|
|
30
|
-
function Patch<T>(current:
|
|
30
|
+
function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
31
31
|
}
|
package/value/value.js
CHANGED
|
@@ -27,7 +27,7 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Value = void 0;
|
|
30
|
+
exports.Value = exports.Delete = exports.Update = exports.Insert = exports.Edit = void 0;
|
|
31
31
|
const index_1 = require("../errors/index");
|
|
32
32
|
const equal_1 = require("./equal");
|
|
33
33
|
const cast_1 = require("./cast");
|
|
@@ -35,7 +35,12 @@ const clone_1 = require("./clone");
|
|
|
35
35
|
const create_1 = require("./create");
|
|
36
36
|
const check_1 = require("./check");
|
|
37
37
|
const delta_1 = require("./delta");
|
|
38
|
-
|
|
38
|
+
var delta_2 = require("./delta");
|
|
39
|
+
Object.defineProperty(exports, "Edit", { enumerable: true, get: function () { return delta_2.Edit; } });
|
|
40
|
+
Object.defineProperty(exports, "Insert", { enumerable: true, get: function () { return delta_2.Insert; } });
|
|
41
|
+
Object.defineProperty(exports, "Update", { enumerable: true, get: function () { return delta_2.Update; } });
|
|
42
|
+
Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return delta_2.Delete; } });
|
|
43
|
+
/** Provides functions to perform structural updates to JavaScript values */
|
|
39
44
|
var Value;
|
|
40
45
|
(function (Value) {
|
|
41
46
|
function Cast(...args) {
|