@sinclair/typebox 0.24.33 → 0.24.36
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/package.json +1 -1
- package/readme.md +319 -186
- package/typebox.d.ts +4 -1
- package/value/cast.js +6 -3
- package/value/clone.d.ts +3 -0
- package/value/clone.js +65 -0
- package/value/delta.d.ts +22 -0
- package/value/delta.js +169 -0
- package/value/equal.d.ts +3 -0
- package/value/equal.js +74 -0
- package/value/is.d.ts +10 -0
- package/value/is.js +49 -0
- package/value/pointer.d.ts +11 -0
- package/value/pointer.js +108 -0
- package/value/value.d.ts +15 -5
- package/value/value.js +29 -6
package/typebox.d.ts
CHANGED
|
@@ -103,9 +103,12 @@ export declare type IntersectEvaluate<T extends readonly TSchema[], P extends un
|
|
|
103
103
|
[K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
|
|
104
104
|
};
|
|
105
105
|
export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I extends object ? I : {};
|
|
106
|
+
export declare type IntersectProperties<T extends TObject[]> = UnionToIntersect<{
|
|
107
|
+
[I in keyof T]: T[I]['properties'];
|
|
108
|
+
}[number]> extends infer P ? (P extends TProperties ? P : TProperties) : never;
|
|
106
109
|
export interface TIntersect<T extends TObject[] = TObject[]> extends TObject {
|
|
107
110
|
static: IntersectReduce<unknown, IntersectEvaluate<T, this['params']>>;
|
|
108
|
-
properties:
|
|
111
|
+
properties: IntersectProperties<T>;
|
|
109
112
|
}
|
|
110
113
|
export declare type UnionToIntersect<U> = (U extends unknown ? (arg: U) => 0 : never) extends (arg: infer I) => 0 ? I : never;
|
|
111
114
|
export declare type UnionLast<U> = UnionToIntersect<U extends unknown ? (x: U) => 0 : never> extends (x: infer L) => 0 ? L : never;
|
package/value/cast.js
CHANGED
|
@@ -81,7 +81,7 @@ exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
|
81
81
|
var ValueCast;
|
|
82
82
|
(function (ValueCast) {
|
|
83
83
|
// -----------------------------------------------------------
|
|
84
|
-
//
|
|
84
|
+
// Convert
|
|
85
85
|
// -----------------------------------------------------------
|
|
86
86
|
function IsString(value) {
|
|
87
87
|
return typeof value === 'string';
|
|
@@ -104,6 +104,9 @@ var ValueCast;
|
|
|
104
104
|
function IsValueTrue(value) {
|
|
105
105
|
return value === true || (IsNumber(value) && value === 1) || (IsBigInt(value) && value === 1n) || (IsString(value) && (value.toLowerCase() === 'true' || value === '1'));
|
|
106
106
|
}
|
|
107
|
+
function IsValueFalse(value) {
|
|
108
|
+
return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === 0n) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
|
|
109
|
+
}
|
|
107
110
|
function TryConvertString(value) {
|
|
108
111
|
return IsValueToString(value) ? value.toString() : value;
|
|
109
112
|
}
|
|
@@ -114,10 +117,10 @@ var ValueCast;
|
|
|
114
117
|
return IsStringNumeric(value) ? parseInt(value) : IsValueTrue(value) ? 1 : value;
|
|
115
118
|
}
|
|
116
119
|
function TryConvertBoolean(value) {
|
|
117
|
-
return IsValueTrue(value) ? true : value;
|
|
120
|
+
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
118
121
|
}
|
|
119
122
|
// -----------------------------------------------------------
|
|
120
|
-
//
|
|
123
|
+
// Cast
|
|
121
124
|
// -----------------------------------------------------------
|
|
122
125
|
function Any(schema, references, value) {
|
|
123
126
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
package/value/clone.d.ts
ADDED
package/value/clone.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValueClone = void 0;
|
|
31
|
+
const is_1 = require("./is");
|
|
32
|
+
var ValueClone;
|
|
33
|
+
(function (ValueClone) {
|
|
34
|
+
function Object(value) {
|
|
35
|
+
const keys = [...globalThis.Object.keys(value), ...globalThis.Object.getOwnPropertySymbols(value)];
|
|
36
|
+
return keys.reduce((acc, key) => ({ ...acc, [key]: Clone(value[key]) }), {});
|
|
37
|
+
}
|
|
38
|
+
function Array(value) {
|
|
39
|
+
return value.map((element) => Clone(element));
|
|
40
|
+
}
|
|
41
|
+
function TypedArray(value) {
|
|
42
|
+
return value.slice();
|
|
43
|
+
}
|
|
44
|
+
function Value(value) {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
function Clone(value) {
|
|
48
|
+
if (is_1.Is.Object(value)) {
|
|
49
|
+
return Object(value);
|
|
50
|
+
}
|
|
51
|
+
else if (is_1.Is.Array(value)) {
|
|
52
|
+
return Array(value);
|
|
53
|
+
}
|
|
54
|
+
else if (is_1.Is.TypedArray(value)) {
|
|
55
|
+
return TypedArray(value);
|
|
56
|
+
}
|
|
57
|
+
else if (is_1.Is.Value(value)) {
|
|
58
|
+
return Value(value);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
throw new Error('ValueClone: Unable to clone value');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
ValueClone.Clone = Clone;
|
|
65
|
+
})(ValueClone = exports.ValueClone || (exports.ValueClone = {}));
|
package/value/delta.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export declare type Edit<T = unknown> = Insert<T> | Update<T> | Delete<T>;
|
|
2
|
+
export interface Insert<T> {
|
|
3
|
+
brand: T;
|
|
4
|
+
type: 'insert';
|
|
5
|
+
path: string;
|
|
6
|
+
value: any;
|
|
7
|
+
}
|
|
8
|
+
export interface Update<T> {
|
|
9
|
+
brand: T;
|
|
10
|
+
type: 'update';
|
|
11
|
+
path: string;
|
|
12
|
+
value: any;
|
|
13
|
+
}
|
|
14
|
+
export interface Delete<T> {
|
|
15
|
+
brand: T;
|
|
16
|
+
type: 'delete';
|
|
17
|
+
path: string;
|
|
18
|
+
}
|
|
19
|
+
export declare namespace ValueDelta {
|
|
20
|
+
function Diff<T>(current: T, next: T): Edit<T>[];
|
|
21
|
+
function Patch<T>(current: T, edits: Edit<T>[]): T;
|
|
22
|
+
}
|
package/value/delta.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValueDelta = void 0;
|
|
31
|
+
const is_1 = require("./is");
|
|
32
|
+
const clone_1 = require("./clone");
|
|
33
|
+
const pointer_1 = require("./pointer");
|
|
34
|
+
var ValueDelta;
|
|
35
|
+
(function (ValueDelta) {
|
|
36
|
+
// ---------------------------------------------------------------------
|
|
37
|
+
// Edits
|
|
38
|
+
// ---------------------------------------------------------------------
|
|
39
|
+
function Update(path, value) {
|
|
40
|
+
return { type: 'update', path, value };
|
|
41
|
+
}
|
|
42
|
+
function Insert(path, value) {
|
|
43
|
+
return { type: 'insert', path, value };
|
|
44
|
+
}
|
|
45
|
+
function Delete(path) {
|
|
46
|
+
return { type: 'delete', path };
|
|
47
|
+
}
|
|
48
|
+
// ---------------------------------------------------------------------
|
|
49
|
+
// Diff
|
|
50
|
+
// ---------------------------------------------------------------------
|
|
51
|
+
function* Object(path, current, next) {
|
|
52
|
+
if (!is_1.Is.Object(next))
|
|
53
|
+
return yield Update(path, next);
|
|
54
|
+
const currentKeys = [...globalThis.Object.keys(current), ...globalThis.Object.getOwnPropertySymbols(current)];
|
|
55
|
+
const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
|
|
56
|
+
for (const key of currentKeys) {
|
|
57
|
+
if (typeof key === 'symbol')
|
|
58
|
+
throw Error('ValueDelta: Cannot produce diff symbol keys');
|
|
59
|
+
if (next[key] === undefined && nextKeys.includes(key))
|
|
60
|
+
yield Update(`${path}/${String(key)}`, undefined);
|
|
61
|
+
}
|
|
62
|
+
for (const key of nextKeys) {
|
|
63
|
+
if (current[key] === undefined || next[key] === undefined)
|
|
64
|
+
continue;
|
|
65
|
+
if (typeof key === 'symbol')
|
|
66
|
+
throw Error('ValueDelta: Cannot produce diff symbol keys');
|
|
67
|
+
yield* Visit(`${path}/${String(key)}`, current[key], next[key]);
|
|
68
|
+
}
|
|
69
|
+
for (const key of nextKeys) {
|
|
70
|
+
if (typeof key === 'symbol')
|
|
71
|
+
throw Error('ValueDelta: Cannot produce diff symbol keys');
|
|
72
|
+
if (current[key] === undefined)
|
|
73
|
+
yield Insert(`${path}/${String(key)}`, next[key]);
|
|
74
|
+
}
|
|
75
|
+
for (const key of currentKeys.reverse()) {
|
|
76
|
+
if (typeof key === 'symbol')
|
|
77
|
+
throw Error('ValueDelta: Cannot produce diff symbol keys');
|
|
78
|
+
if (next[key] === undefined && !nextKeys.includes(key))
|
|
79
|
+
yield Delete(`${path}/${String(key)}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function* Array(path, current, next) {
|
|
83
|
+
if (!is_1.Is.Array(next))
|
|
84
|
+
return yield Update(path, next);
|
|
85
|
+
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
86
|
+
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
87
|
+
}
|
|
88
|
+
for (let i = 0; i < next.length; i++) {
|
|
89
|
+
if (i < current.length)
|
|
90
|
+
continue;
|
|
91
|
+
yield Insert(`${path}/${i}`, next[i]);
|
|
92
|
+
}
|
|
93
|
+
for (let i = current.length - 1; i >= 0; i--) {
|
|
94
|
+
if (i < next.length)
|
|
95
|
+
continue;
|
|
96
|
+
yield Delete(`${path}/${i}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function* TypedArray(path, current, next) {
|
|
100
|
+
if (!is_1.Is.TypedArray(next) || current.length !== next.length || globalThis.Object.getPrototypeOf(current).constructor.name !== globalThis.Object.getPrototypeOf(next).constructor.name)
|
|
101
|
+
return yield Update(path, next);
|
|
102
|
+
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
103
|
+
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function* Value(path, current, next) {
|
|
107
|
+
if (current === next)
|
|
108
|
+
return;
|
|
109
|
+
yield Update(path, next);
|
|
110
|
+
}
|
|
111
|
+
function* Visit(path, current, next) {
|
|
112
|
+
if (is_1.Is.Object(current)) {
|
|
113
|
+
return yield* Object(path, current, next);
|
|
114
|
+
}
|
|
115
|
+
else if (is_1.Is.Array(current)) {
|
|
116
|
+
return yield* Array(path, current, next);
|
|
117
|
+
}
|
|
118
|
+
else if (is_1.Is.TypedArray(current)) {
|
|
119
|
+
return yield* TypedArray(path, current, next);
|
|
120
|
+
}
|
|
121
|
+
else if (is_1.Is.Value(current)) {
|
|
122
|
+
return yield* Value(path, current, next);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
console.log('left', current);
|
|
126
|
+
throw new Error('ValueDelta: Cannot produce edits for value');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function Diff(current, next) {
|
|
130
|
+
return [...Visit('', current, next)];
|
|
131
|
+
}
|
|
132
|
+
ValueDelta.Diff = Diff;
|
|
133
|
+
// ---------------------------------------------------------------------
|
|
134
|
+
// Patch
|
|
135
|
+
// ---------------------------------------------------------------------
|
|
136
|
+
function IsRootUpdate(edits) {
|
|
137
|
+
return edits.length > 0 && edits[0].path === '' && edits[0].type === 'update';
|
|
138
|
+
}
|
|
139
|
+
function IsIdentity(edits) {
|
|
140
|
+
return edits.length === 0;
|
|
141
|
+
}
|
|
142
|
+
function Patch(current, edits) {
|
|
143
|
+
if (IsRootUpdate(edits)) {
|
|
144
|
+
return clone_1.ValueClone.Clone(edits[0].value);
|
|
145
|
+
}
|
|
146
|
+
if (IsIdentity(edits)) {
|
|
147
|
+
return clone_1.ValueClone.Clone(current);
|
|
148
|
+
}
|
|
149
|
+
const clone = clone_1.ValueClone.Clone(current);
|
|
150
|
+
for (const edit of edits) {
|
|
151
|
+
switch (edit.type) {
|
|
152
|
+
case 'insert': {
|
|
153
|
+
pointer_1.ValuePointer.Set(clone, edit.path, edit.value);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case 'update': {
|
|
157
|
+
pointer_1.ValuePointer.Set(clone, edit.path, edit.value);
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
case 'delete': {
|
|
161
|
+
pointer_1.ValuePointer.Delete(clone, edit.path);
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return clone;
|
|
167
|
+
}
|
|
168
|
+
ValueDelta.Patch = Patch;
|
|
169
|
+
})(ValueDelta = exports.ValueDelta || (exports.ValueDelta = {}));
|
package/value/equal.d.ts
ADDED
package/value/equal.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValueEqual = void 0;
|
|
31
|
+
const is_1 = require("./is");
|
|
32
|
+
var ValueEqual;
|
|
33
|
+
(function (ValueEqual) {
|
|
34
|
+
function Object(left, right) {
|
|
35
|
+
if (!is_1.Is.Object(right))
|
|
36
|
+
return false;
|
|
37
|
+
const leftKeys = [...globalThis.Object.keys(left), ...globalThis.Object.getOwnPropertySymbols(left)];
|
|
38
|
+
const rightKeys = [...globalThis.Object.keys(right), ...globalThis.Object.getOwnPropertySymbols(right)];
|
|
39
|
+
if (leftKeys.length !== rightKeys.length)
|
|
40
|
+
return false;
|
|
41
|
+
return leftKeys.every((key) => Equal(left[key], right[key]));
|
|
42
|
+
}
|
|
43
|
+
function Array(left, right) {
|
|
44
|
+
if (!is_1.Is.Array(right) || left.length !== right.length)
|
|
45
|
+
return false;
|
|
46
|
+
return left.every((value, index) => Equal(value, right[index]));
|
|
47
|
+
}
|
|
48
|
+
function TypedArray(left, right) {
|
|
49
|
+
if (!is_1.Is.TypedArray(right) || left.length !== right.length || globalThis.Object.getPrototypeOf(left).constructor.name !== globalThis.Object.getPrototypeOf(right).constructor.name)
|
|
50
|
+
return false;
|
|
51
|
+
return left.every((value, index) => Equal(value, right[index]));
|
|
52
|
+
}
|
|
53
|
+
function Value(left, right) {
|
|
54
|
+
return left === right;
|
|
55
|
+
}
|
|
56
|
+
function Equal(left, right) {
|
|
57
|
+
if (is_1.Is.Object(left)) {
|
|
58
|
+
return Object(left, right);
|
|
59
|
+
}
|
|
60
|
+
else if (is_1.Is.TypedArray(left)) {
|
|
61
|
+
return TypedArray(left, right);
|
|
62
|
+
}
|
|
63
|
+
else if (is_1.Is.Array(left)) {
|
|
64
|
+
return Array(left, right);
|
|
65
|
+
}
|
|
66
|
+
else if (is_1.Is.Value(left)) {
|
|
67
|
+
return Value(left, right);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw new Error('ValueEquals: Unable to compare value');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
ValueEqual.Equal = Equal;
|
|
74
|
+
})(ValueEqual = exports.ValueEqual || (exports.ValueEqual = {}));
|
package/value/is.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare type ValueType = null | undefined | Function | symbol | bigint | number | boolean | string;
|
|
2
|
+
export declare type ObjectType = Record<string | number | symbol, unknown>;
|
|
3
|
+
export declare type TypedArrayType = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
|
|
4
|
+
export declare type ArrayType = unknown[];
|
|
5
|
+
export declare namespace Is {
|
|
6
|
+
function Object(value: unknown): value is ObjectType;
|
|
7
|
+
function Array(value: unknown): value is ArrayType;
|
|
8
|
+
function Value(value: unknown): value is ValueType;
|
|
9
|
+
function TypedArray(value: unknown): value is TypedArrayType;
|
|
10
|
+
}
|
package/value/is.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.Is = void 0;
|
|
31
|
+
var Is;
|
|
32
|
+
(function (Is) {
|
|
33
|
+
function Object(value) {
|
|
34
|
+
return value !== null && typeof value === 'object' && !globalThis.Array.isArray(value) && !ArrayBuffer.isView(value);
|
|
35
|
+
}
|
|
36
|
+
Is.Object = Object;
|
|
37
|
+
function Array(value) {
|
|
38
|
+
return globalThis.Array.isArray(value) && !ArrayBuffer.isView(value);
|
|
39
|
+
}
|
|
40
|
+
Is.Array = Array;
|
|
41
|
+
function Value(value) {
|
|
42
|
+
return value === null || value === undefined || typeof value === 'function' || typeof value === 'symbol' || typeof value === 'bigint' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'string';
|
|
43
|
+
}
|
|
44
|
+
Is.Value = Value;
|
|
45
|
+
function TypedArray(value) {
|
|
46
|
+
return ArrayBuffer.isView(value);
|
|
47
|
+
}
|
|
48
|
+
Is.TypedArray = TypedArray;
|
|
49
|
+
})(Is = exports.Is || (exports.Is = {}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** RFC6901 JsonPointer */
|
|
2
|
+
export declare namespace ValuePointer {
|
|
3
|
+
/** Sets the value at the given pointer. If the value at the pointer does not exist it is created. */
|
|
4
|
+
function Set(value: any, pointer: string, update: any): void;
|
|
5
|
+
/** Deletes a value at the given pointer. */
|
|
6
|
+
function Delete(value: any, pointer: string): any[] | undefined;
|
|
7
|
+
/** True if a value exists at the given pointer */
|
|
8
|
+
function Has(value: any, pointer: string): boolean;
|
|
9
|
+
/** Gets the value at the given pointer */
|
|
10
|
+
function Get(value: any, pointer: string): any;
|
|
11
|
+
}
|
package/value/pointer.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in
|
|
18
|
+
all copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
THE SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---------------------------------------------------------------------------*/
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.ValuePointer = void 0;
|
|
31
|
+
/** RFC6901 JsonPointer */
|
|
32
|
+
var ValuePointer;
|
|
33
|
+
(function (ValuePointer) {
|
|
34
|
+
function Format(pointer) {
|
|
35
|
+
if (pointer === '/')
|
|
36
|
+
return [''];
|
|
37
|
+
const split = pointer.split('/');
|
|
38
|
+
const filter = split.filter((part) => part.length > 0);
|
|
39
|
+
return filter.map((part) => part.replace(/~0/g, `~`).replace(/~1/g, `/`));
|
|
40
|
+
}
|
|
41
|
+
/** Sets the value at the given pointer. If the value at the pointer does not exist it is created. */
|
|
42
|
+
function Set(value, pointer, update) {
|
|
43
|
+
if (pointer === '')
|
|
44
|
+
throw Error('Cannot set root value');
|
|
45
|
+
const path = Format(pointer);
|
|
46
|
+
let current = value;
|
|
47
|
+
while (path.length > 1) {
|
|
48
|
+
const next = path.shift();
|
|
49
|
+
if (current[next] === undefined)
|
|
50
|
+
current[next] = {};
|
|
51
|
+
current = current[next];
|
|
52
|
+
}
|
|
53
|
+
current[path.shift()] = update;
|
|
54
|
+
}
|
|
55
|
+
ValuePointer.Set = Set;
|
|
56
|
+
/** Deletes a value at the given pointer. */
|
|
57
|
+
function Delete(value, pointer) {
|
|
58
|
+
if (pointer === '')
|
|
59
|
+
throw Error('Cannot delete root value');
|
|
60
|
+
let current = value;
|
|
61
|
+
const path = Format(pointer);
|
|
62
|
+
while (path.length > 1) {
|
|
63
|
+
const next = path.shift();
|
|
64
|
+
if (current[next] === undefined)
|
|
65
|
+
return;
|
|
66
|
+
current = current[next];
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(current)) {
|
|
69
|
+
const index = parseInt(path.shift());
|
|
70
|
+
return current.splice(index, 1);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
const key = path.shift();
|
|
74
|
+
delete current[key];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
ValuePointer.Delete = Delete;
|
|
78
|
+
/** True if a value exists at the given pointer */
|
|
79
|
+
function Has(value, pointer) {
|
|
80
|
+
if (pointer === '')
|
|
81
|
+
return true;
|
|
82
|
+
let current = value;
|
|
83
|
+
const path = Format(pointer);
|
|
84
|
+
while (path.length > 1) {
|
|
85
|
+
const next = path.shift();
|
|
86
|
+
if (current[next] === undefined)
|
|
87
|
+
return false;
|
|
88
|
+
current = current[next];
|
|
89
|
+
}
|
|
90
|
+
return current[path.shift()] !== undefined;
|
|
91
|
+
}
|
|
92
|
+
ValuePointer.Has = Has;
|
|
93
|
+
/** Gets the value at the given pointer */
|
|
94
|
+
function Get(value, pointer) {
|
|
95
|
+
if (pointer === '')
|
|
96
|
+
return value;
|
|
97
|
+
let current = value;
|
|
98
|
+
const path = Format(pointer);
|
|
99
|
+
while (path.length > 1) {
|
|
100
|
+
const next = path.shift();
|
|
101
|
+
if (current[next] === undefined)
|
|
102
|
+
return undefined;
|
|
103
|
+
current = current[next];
|
|
104
|
+
}
|
|
105
|
+
return current[path.shift()];
|
|
106
|
+
}
|
|
107
|
+
ValuePointer.Get = Get;
|
|
108
|
+
})(ValuePointer = exports.ValuePointer || (exports.ValuePointer = {}));
|
package/value/value.d.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
2
|
import { ValueError } from '../errors/index';
|
|
3
|
-
|
|
3
|
+
import { Edit } from './delta';
|
|
4
|
+
export type { Edit } from './delta';
|
|
5
|
+
/** The Value namespace provides type operations on values */
|
|
4
6
|
export declare namespace Value {
|
|
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
|
+
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|
|
9
|
+
/** 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. */
|
|
10
|
+
function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
|
5
11
|
/** Creates a value from the given type */
|
|
6
12
|
function Create<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R]): Types.Static<T>;
|
|
7
13
|
/** Creates a value from the given type */
|
|
@@ -10,12 +16,16 @@ export declare namespace Value {
|
|
|
10
16
|
function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): value is Types.Static<T>;
|
|
11
17
|
/** Returns true if the value matches the given type. */
|
|
12
18
|
function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>;
|
|
13
|
-
/** 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. */
|
|
14
|
-
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
|
|
15
|
-
/** 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. */
|
|
16
|
-
function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
|
17
19
|
/** Returns an iterator for each error in this value. */
|
|
18
20
|
function Errors<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): IterableIterator<ValueError>;
|
|
19
21
|
/** Returns an iterator for each error in this value. */
|
|
20
22
|
function Errors<T extends Types.TSchema>(schema: T, value: unknown): IterableIterator<ValueError>;
|
|
23
|
+
/** Returns true if left and right values are structurally equal */
|
|
24
|
+
function Equal<T>(left: T, right: unknown): right is T;
|
|
25
|
+
/** Returns a structural clone of the given value */
|
|
26
|
+
function Clone<T>(value: T): T;
|
|
27
|
+
/** Returns edits to transform the current value into the next value */
|
|
28
|
+
function Diff<T>(current: T, next: T): Edit<T>[];
|
|
29
|
+
/** Returns a new value with edits applied to the given value */
|
|
30
|
+
function Patch<T>(current: T, edits: Edit<T>[]): T;
|
|
21
31
|
}
|
package/value/value.js
CHANGED
|
@@ -29,12 +29,20 @@ THE SOFTWARE.
|
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.Value = void 0;
|
|
31
31
|
const index_1 = require("../errors/index");
|
|
32
|
+
const equal_1 = require("./equal");
|
|
32
33
|
const cast_1 = require("./cast");
|
|
34
|
+
const clone_1 = require("./clone");
|
|
33
35
|
const create_1 = require("./create");
|
|
34
36
|
const check_1 = require("./check");
|
|
35
|
-
|
|
37
|
+
const delta_1 = require("./delta");
|
|
38
|
+
/** The Value namespace provides type operations on values */
|
|
36
39
|
var Value;
|
|
37
40
|
(function (Value) {
|
|
41
|
+
function Cast(...args) {
|
|
42
|
+
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
|
|
43
|
+
return cast_1.ValueCast.Cast(schema, references, value);
|
|
44
|
+
}
|
|
45
|
+
Value.Cast = Cast;
|
|
38
46
|
function Create(...args) {
|
|
39
47
|
const [schema, references] = args.length === 2 ? [args[0], args[1]] : [args[0], []];
|
|
40
48
|
return create_1.ValueCreate.Create(schema, references);
|
|
@@ -45,14 +53,29 @@ var Value;
|
|
|
45
53
|
return check_1.ValueCheck.Check(schema, references, value);
|
|
46
54
|
}
|
|
47
55
|
Value.Check = Check;
|
|
48
|
-
function Cast(...args) {
|
|
49
|
-
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
|
|
50
|
-
return cast_1.ValueCast.Cast(schema, references, value);
|
|
51
|
-
}
|
|
52
|
-
Value.Cast = Cast;
|
|
53
56
|
function* Errors(...args) {
|
|
54
57
|
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
|
|
55
58
|
yield* index_1.ValueErrors.Errors(schema, references, value);
|
|
56
59
|
}
|
|
57
60
|
Value.Errors = Errors;
|
|
61
|
+
/** Returns true if left and right values are structurally equal */
|
|
62
|
+
function Equal(left, right) {
|
|
63
|
+
return equal_1.ValueEqual.Equal(left, right);
|
|
64
|
+
}
|
|
65
|
+
Value.Equal = Equal;
|
|
66
|
+
/** Returns a structural clone of the given value */
|
|
67
|
+
function Clone(value) {
|
|
68
|
+
return clone_1.ValueClone.Clone(value);
|
|
69
|
+
}
|
|
70
|
+
Value.Clone = Clone;
|
|
71
|
+
/** Returns edits to transform the current value into the next value */
|
|
72
|
+
function Diff(current, next) {
|
|
73
|
+
return delta_1.ValueDelta.Diff(current, next);
|
|
74
|
+
}
|
|
75
|
+
Value.Diff = Diff;
|
|
76
|
+
/** Returns a new value with edits applied to the given value */
|
|
77
|
+
function Patch(current, edits) {
|
|
78
|
+
return delta_1.ValueDelta.Patch(current, edits);
|
|
79
|
+
}
|
|
80
|
+
Value.Patch = Patch;
|
|
58
81
|
})(Value = exports.Value || (exports.Value = {}));
|