@sinclair/typebox 0.29.6 → 0.30.0-dev-1
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/compiler/compiler.d.ts +7 -4
- package/compiler/compiler.js +208 -194
- package/errors/errors.d.ts +65 -60
- package/errors/errors.js +515 -492
- package/package.json +14 -2
- package/readme.md +161 -169
- package/system/system.js +0 -6
- package/typebox.d.ts +99 -52
- package/typebox.js +496 -573
- package/value/cast.d.ts +5 -4
- package/value/cast.js +255 -260
- package/value/check.d.ts +4 -3
- package/value/check.js +412 -397
- package/value/clone.d.ts +2 -3
- package/value/clone.js +62 -42
- package/value/convert.d.ts +5 -4
- package/value/convert.js +305 -318
- package/value/create.d.ts +6 -6
- package/value/create.js +388 -376
- package/value/delta.d.ts +2 -4
- package/value/delta.js +121 -130
- package/value/equal.d.ts +2 -3
- package/value/equal.js +48 -51
- package/value/guard.d.ts +44 -0
- package/value/guard.js +146 -0
- package/value/hash.d.ts +14 -3
- package/value/hash.js +124 -166
- package/value/index.d.ts +3 -4
- package/value/index.js +6 -20
- package/value/mutate.d.ts +2 -4
- package/value/mutate.js +75 -67
- package/value/pointer.js +8 -2
- package/value/value.d.ts +15 -15
- package/value/value.js +27 -27
- package/value/is.d.ts +0 -11
- package/value/is.js +0 -53
package/value/hash.js
CHANGED
|
@@ -27,7 +27,11 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
30
|
+
exports.Hash = exports.ByteMarker = exports.ValueHashError = void 0;
|
|
31
|
+
const ValueGuard = require("./guard");
|
|
32
|
+
// --------------------------------------------------------------------------
|
|
33
|
+
// Errors
|
|
34
|
+
// --------------------------------------------------------------------------
|
|
31
35
|
class ValueHashError extends Error {
|
|
32
36
|
constructor(value) {
|
|
33
37
|
super(`Hash: Unable to hash value`);
|
|
@@ -35,174 +39,128 @@ class ValueHashError extends Error {
|
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
exports.ValueHashError = ValueHashError;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
42
|
+
// --------------------------------------------------------------------------
|
|
43
|
+
// ByteMarker
|
|
44
|
+
// --------------------------------------------------------------------------
|
|
45
|
+
var ByteMarker;
|
|
46
|
+
(function (ByteMarker) {
|
|
47
|
+
ByteMarker[ByteMarker["Undefined"] = 0] = "Undefined";
|
|
48
|
+
ByteMarker[ByteMarker["Null"] = 1] = "Null";
|
|
49
|
+
ByteMarker[ByteMarker["Boolean"] = 2] = "Boolean";
|
|
50
|
+
ByteMarker[ByteMarker["Number"] = 3] = "Number";
|
|
51
|
+
ByteMarker[ByteMarker["String"] = 4] = "String";
|
|
52
|
+
ByteMarker[ByteMarker["Object"] = 5] = "Object";
|
|
53
|
+
ByteMarker[ByteMarker["Array"] = 6] = "Array";
|
|
54
|
+
ByteMarker[ByteMarker["Date"] = 7] = "Date";
|
|
55
|
+
ByteMarker[ByteMarker["Uint8Array"] = 8] = "Uint8Array";
|
|
56
|
+
ByteMarker[ByteMarker["Symbol"] = 9] = "Symbol";
|
|
57
|
+
ByteMarker[ByteMarker["BigInt"] = 10] = "BigInt";
|
|
58
|
+
})(ByteMarker || (exports.ByteMarker = ByteMarker = {}));
|
|
59
|
+
// --------------------------------------------------------------------------
|
|
60
|
+
// State
|
|
61
|
+
// --------------------------------------------------------------------------
|
|
62
|
+
let Accumulator = BigInt('14695981039346656037');
|
|
63
|
+
const [Prime, Size] = [BigInt('1099511628211'), BigInt('2') ** BigInt('64')];
|
|
64
|
+
const Bytes = Array.from({ length: 256 }).map((_, i) => BigInt(i));
|
|
65
|
+
const F64 = new Float64Array(1);
|
|
66
|
+
const F64In = new DataView(F64.buffer);
|
|
67
|
+
const F64Out = new Uint8Array(F64.buffer);
|
|
68
|
+
// --------------------------------------------------------------------------
|
|
69
|
+
// Hashing Functions
|
|
70
|
+
// --------------------------------------------------------------------------
|
|
71
|
+
function ArrayType(value) {
|
|
72
|
+
FNV1A64(ByteMarker.Array);
|
|
73
|
+
for (const item of value) {
|
|
74
|
+
Visit(item);
|
|
68
75
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return value === null;
|
|
80
|
-
}
|
|
81
|
-
function IsNumber(value) {
|
|
82
|
-
return typeof value === 'number';
|
|
83
|
-
}
|
|
84
|
-
function IsSymbol(value) {
|
|
85
|
-
return typeof value === 'symbol';
|
|
86
|
-
}
|
|
87
|
-
function IsBigInt(value) {
|
|
88
|
-
return typeof value === 'bigint';
|
|
89
|
-
}
|
|
90
|
-
function IsObject(value) {
|
|
91
|
-
return typeof value === 'object' && value !== null && !IsArray(value) && !IsDate(value) && !IsUint8Array(value);
|
|
92
|
-
}
|
|
93
|
-
function IsString(value) {
|
|
94
|
-
return typeof value === 'string';
|
|
95
|
-
}
|
|
96
|
-
function IsUndefined(value) {
|
|
97
|
-
return value === undefined;
|
|
98
|
-
}
|
|
99
|
-
// ----------------------------------------------------
|
|
100
|
-
// Encoding
|
|
101
|
-
// ----------------------------------------------------
|
|
102
|
-
function Array(value) {
|
|
103
|
-
FNV1A64(ByteMarker.Array);
|
|
104
|
-
for (const item of value) {
|
|
105
|
-
Visit(item);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
function Boolean(value) {
|
|
109
|
-
FNV1A64(ByteMarker.Boolean);
|
|
110
|
-
FNV1A64(value ? 1 : 0);
|
|
111
|
-
}
|
|
112
|
-
function BigInt(value) {
|
|
113
|
-
FNV1A64(ByteMarker.BigInt);
|
|
114
|
-
F64In.setBigInt64(0, value);
|
|
115
|
-
for (const byte of F64Out) {
|
|
116
|
-
FNV1A64(byte);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
function Date(value) {
|
|
120
|
-
FNV1A64(ByteMarker.Date);
|
|
121
|
-
Visit(value.getTime());
|
|
122
|
-
}
|
|
123
|
-
function Null(value) {
|
|
124
|
-
FNV1A64(ByteMarker.Null);
|
|
125
|
-
}
|
|
126
|
-
function Number(value) {
|
|
127
|
-
FNV1A64(ByteMarker.Number);
|
|
128
|
-
F64In.setFloat64(0, value);
|
|
129
|
-
for (const byte of F64Out) {
|
|
130
|
-
FNV1A64(byte);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
function Object(value) {
|
|
134
|
-
FNV1A64(ByteMarker.Object);
|
|
135
|
-
for (const key of globalThis.Object.keys(value).sort()) {
|
|
136
|
-
Visit(key);
|
|
137
|
-
Visit(value[key]);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function String(value) {
|
|
141
|
-
FNV1A64(ByteMarker.String);
|
|
142
|
-
for (let i = 0; i < value.length; i++) {
|
|
143
|
-
FNV1A64(value.charCodeAt(i));
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
function Symbol(value) {
|
|
147
|
-
FNV1A64(ByteMarker.Symbol);
|
|
148
|
-
Visit(value.description);
|
|
149
|
-
}
|
|
150
|
-
function Uint8Array(value) {
|
|
151
|
-
FNV1A64(ByteMarker.Uint8Array);
|
|
152
|
-
for (let i = 0; i < value.length; i++) {
|
|
153
|
-
FNV1A64(value[i]);
|
|
154
|
-
}
|
|
76
|
+
}
|
|
77
|
+
function BooleanType(value) {
|
|
78
|
+
FNV1A64(ByteMarker.Boolean);
|
|
79
|
+
FNV1A64(value ? 1 : 0);
|
|
80
|
+
}
|
|
81
|
+
function BigIntType(value) {
|
|
82
|
+
FNV1A64(ByteMarker.BigInt);
|
|
83
|
+
F64In.setBigInt64(0, value);
|
|
84
|
+
for (const byte of F64Out) {
|
|
85
|
+
FNV1A64(byte);
|
|
155
86
|
}
|
|
156
|
-
|
|
157
|
-
|
|
87
|
+
}
|
|
88
|
+
function DateType(value) {
|
|
89
|
+
FNV1A64(ByteMarker.Date);
|
|
90
|
+
Visit(value.getTime());
|
|
91
|
+
}
|
|
92
|
+
function NullType(value) {
|
|
93
|
+
FNV1A64(ByteMarker.Null);
|
|
94
|
+
}
|
|
95
|
+
function NumberType(value) {
|
|
96
|
+
FNV1A64(ByteMarker.Number);
|
|
97
|
+
F64In.setFloat64(0, value);
|
|
98
|
+
for (const byte of F64Out) {
|
|
99
|
+
FNV1A64(byte);
|
|
158
100
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
else if (IsBigInt(value)) {
|
|
167
|
-
BigInt(value);
|
|
168
|
-
}
|
|
169
|
-
else if (IsDate(value)) {
|
|
170
|
-
Date(value);
|
|
171
|
-
}
|
|
172
|
-
else if (IsNull(value)) {
|
|
173
|
-
Null(value);
|
|
174
|
-
}
|
|
175
|
-
else if (IsNumber(value)) {
|
|
176
|
-
Number(value);
|
|
177
|
-
}
|
|
178
|
-
else if (IsObject(value)) {
|
|
179
|
-
Object(value);
|
|
180
|
-
}
|
|
181
|
-
else if (IsString(value)) {
|
|
182
|
-
String(value);
|
|
183
|
-
}
|
|
184
|
-
else if (IsSymbol(value)) {
|
|
185
|
-
Symbol(value);
|
|
186
|
-
}
|
|
187
|
-
else if (IsUint8Array(value)) {
|
|
188
|
-
Uint8Array(value);
|
|
189
|
-
}
|
|
190
|
-
else if (IsUndefined(value)) {
|
|
191
|
-
Undefined(value);
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
throw new ValueHashError(value);
|
|
195
|
-
}
|
|
101
|
+
}
|
|
102
|
+
function ObjectType(value) {
|
|
103
|
+
FNV1A64(ByteMarker.Object);
|
|
104
|
+
for (const key of globalThis.Object.keys(value).sort()) {
|
|
105
|
+
Visit(key);
|
|
106
|
+
Visit(value[key]);
|
|
196
107
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
108
|
+
}
|
|
109
|
+
function StringType(value) {
|
|
110
|
+
FNV1A64(ByteMarker.String);
|
|
111
|
+
for (let i = 0; i < value.length; i++) {
|
|
112
|
+
FNV1A64(value.charCodeAt(i));
|
|
200
113
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
114
|
+
}
|
|
115
|
+
function SymbolType(value) {
|
|
116
|
+
FNV1A64(ByteMarker.Symbol);
|
|
117
|
+
Visit(value.description);
|
|
118
|
+
}
|
|
119
|
+
function Uint8ArrayType(value) {
|
|
120
|
+
FNV1A64(ByteMarker.Uint8Array);
|
|
121
|
+
for (let i = 0; i < value.length; i++) {
|
|
122
|
+
FNV1A64(value[i]);
|
|
206
123
|
}
|
|
207
|
-
|
|
208
|
-
|
|
124
|
+
}
|
|
125
|
+
function UndefinedType(value) {
|
|
126
|
+
return FNV1A64(ByteMarker.Undefined);
|
|
127
|
+
}
|
|
128
|
+
function Visit(value) {
|
|
129
|
+
if (ValueGuard.IsArray(value))
|
|
130
|
+
return ArrayType(value);
|
|
131
|
+
if (ValueGuard.IsBoolean(value))
|
|
132
|
+
return BooleanType(value);
|
|
133
|
+
if (ValueGuard.IsBigInt(value))
|
|
134
|
+
return BigIntType(value);
|
|
135
|
+
if (ValueGuard.IsDate(value))
|
|
136
|
+
return DateType(value);
|
|
137
|
+
if (ValueGuard.IsNull(value))
|
|
138
|
+
return NullType(value);
|
|
139
|
+
if (ValueGuard.IsNumber(value))
|
|
140
|
+
return NumberType(value);
|
|
141
|
+
if (ValueGuard.IsPlainObject(value))
|
|
142
|
+
return ObjectType(value);
|
|
143
|
+
if (ValueGuard.IsString(value))
|
|
144
|
+
return StringType(value);
|
|
145
|
+
if (ValueGuard.IsSymbol(value))
|
|
146
|
+
return SymbolType(value);
|
|
147
|
+
if (ValueGuard.IsUint8Array(value))
|
|
148
|
+
return Uint8ArrayType(value);
|
|
149
|
+
if (ValueGuard.IsUndefined(value))
|
|
150
|
+
return UndefinedType(value);
|
|
151
|
+
throw new ValueHashError(value);
|
|
152
|
+
}
|
|
153
|
+
function FNV1A64(byte) {
|
|
154
|
+
Accumulator = Accumulator ^ Bytes[byte];
|
|
155
|
+
Accumulator = (Accumulator * Prime) % Size;
|
|
156
|
+
}
|
|
157
|
+
// --------------------------------------------------------------------------
|
|
158
|
+
// Hash
|
|
159
|
+
// --------------------------------------------------------------------------
|
|
160
|
+
/** Creates a FNV1A-64 non cryptographic hash of the given value */
|
|
161
|
+
function Hash(value) {
|
|
162
|
+
Accumulator = BigInt('14695981039346656037');
|
|
163
|
+
Visit(value);
|
|
164
|
+
return Accumulator;
|
|
165
|
+
}
|
|
166
|
+
exports.Hash = Hash;
|
package/value/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { ValueError,
|
|
2
|
-
export { ValueHash } from './hash';
|
|
1
|
+
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index';
|
|
3
2
|
export { Edit, Insert, Update, Delete } from './delta';
|
|
4
3
|
export { Mutable } from './mutate';
|
|
5
|
-
export
|
|
6
|
-
export
|
|
4
|
+
export { ValuePointer } from './pointer';
|
|
5
|
+
export { Value } from './value';
|
package/value/index.js
CHANGED
|
@@ -26,31 +26,17 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
26
26
|
THE SOFTWARE.
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
30
|
-
if (k2 === undefined) k2 = k;
|
|
31
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
32
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
33
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
34
|
-
}
|
|
35
|
-
Object.defineProperty(o, k2, desc);
|
|
36
|
-
}) : (function(o, m, k, k2) {
|
|
37
|
-
if (k2 === undefined) k2 = k;
|
|
38
|
-
o[k2] = m[k];
|
|
39
|
-
}));
|
|
40
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
41
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
|
-
};
|
|
43
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.
|
|
30
|
+
exports.Value = exports.ValuePointer = exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
45
31
|
var index_1 = require("../errors/index");
|
|
46
|
-
Object.defineProperty(exports, "ValueErrorIterator", { enumerable: true, get: function () { return index_1.ValueErrorIterator; } });
|
|
47
32
|
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } });
|
|
48
|
-
|
|
49
|
-
Object.defineProperty(exports, "ValueHash", { enumerable: true, get: function () { return hash_1.ValueHash; } });
|
|
33
|
+
Object.defineProperty(exports, "ValueErrorIterator", { enumerable: true, get: function () { return index_1.ValueErrorIterator; } });
|
|
50
34
|
var delta_1 = require("./delta");
|
|
51
35
|
Object.defineProperty(exports, "Edit", { enumerable: true, get: function () { return delta_1.Edit; } });
|
|
52
36
|
Object.defineProperty(exports, "Insert", { enumerable: true, get: function () { return delta_1.Insert; } });
|
|
53
37
|
Object.defineProperty(exports, "Update", { enumerable: true, get: function () { return delta_1.Update; } });
|
|
54
38
|
Object.defineProperty(exports, "Delete", { enumerable: true, get: function () { return delta_1.Delete; } });
|
|
55
|
-
|
|
56
|
-
|
|
39
|
+
var pointer_1 = require("./pointer");
|
|
40
|
+
Object.defineProperty(exports, "ValuePointer", { enumerable: true, get: function () { return pointer_1.ValuePointer; } });
|
|
41
|
+
var value_1 = require("./value");
|
|
42
|
+
Object.defineProperty(exports, "Value", { enumerable: true, get: function () { return value_1.Value; } });
|
package/value/mutate.d.ts
CHANGED
|
@@ -7,7 +7,5 @@ export declare class ValueMutateInvalidRootMutationError extends Error {
|
|
|
7
7
|
export type Mutable = {
|
|
8
8
|
[key: string]: unknown;
|
|
9
9
|
} | unknown[];
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function Mutate(current: Mutable, next: Mutable): void;
|
|
13
|
-
}
|
|
10
|
+
/** Performs a deep mutable value assignment while retaining internal references. */
|
|
11
|
+
export declare function Mutate(current: Mutable, next: Mutable): void;
|
package/value/mutate.js
CHANGED
|
@@ -27,10 +27,13 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
31
|
-
const is_1 = require("./is");
|
|
30
|
+
exports.Mutate = exports.ValueMutateInvalidRootMutationError = exports.ValueMutateTypeMismatchError = void 0;
|
|
32
31
|
const pointer_1 = require("./pointer");
|
|
33
|
-
const
|
|
32
|
+
const ValueClone = require("./clone");
|
|
33
|
+
const ValueGuard = require("./guard");
|
|
34
|
+
// --------------------------------------------------------------------------
|
|
35
|
+
// Errors
|
|
36
|
+
// --------------------------------------------------------------------------
|
|
34
37
|
class ValueMutateTypeMismatchError extends Error {
|
|
35
38
|
constructor() {
|
|
36
39
|
super('ValueMutate: Cannot assign due type mismatch of assignable values');
|
|
@@ -43,79 +46,84 @@ class ValueMutateInvalidRootMutationError extends Error {
|
|
|
43
46
|
}
|
|
44
47
|
}
|
|
45
48
|
exports.ValueMutateInvalidRootMutationError = ValueMutateInvalidRootMutationError;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (!is_1.Is.Object(current)) {
|
|
50
|
-
pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next));
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
const currentKeys = globalThis.Object.keys(current);
|
|
54
|
-
const nextKeys = globalThis.Object.keys(next);
|
|
55
|
-
for (const currentKey of currentKeys) {
|
|
56
|
-
if (!nextKeys.includes(currentKey)) {
|
|
57
|
-
delete current[currentKey];
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
for (const nextKey of nextKeys) {
|
|
61
|
-
if (!currentKeys.includes(nextKey)) {
|
|
62
|
-
current[nextKey] = null;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
for (const nextKey of nextKeys) {
|
|
66
|
-
Visit(root, `${path}/${nextKey}`, current[nextKey], next[nextKey]);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
49
|
+
function ObjectType(root, path, current, next) {
|
|
50
|
+
if (!ValueGuard.IsPlainObject(current)) {
|
|
51
|
+
pointer_1.ValuePointer.Set(root, path, ValueClone.Clone(next));
|
|
69
52
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
Visit(root, `${path}/${index}`, current[index], next[index]);
|
|
53
|
+
else {
|
|
54
|
+
const currentKeys = Object.keys(current);
|
|
55
|
+
const nextKeys = Object.keys(next);
|
|
56
|
+
for (const currentKey of currentKeys) {
|
|
57
|
+
if (!nextKeys.includes(currentKey)) {
|
|
58
|
+
delete current[currentKey];
|
|
77
59
|
}
|
|
78
|
-
current.splice(next.length);
|
|
79
60
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
for (let i = 0; i < current.length; i++) {
|
|
84
|
-
current[i] = next[i];
|
|
61
|
+
for (const nextKey of nextKeys) {
|
|
62
|
+
if (!currentKeys.includes(nextKey)) {
|
|
63
|
+
current[nextKey] = null;
|
|
85
64
|
}
|
|
86
65
|
}
|
|
87
|
-
|
|
88
|
-
|
|
66
|
+
for (const nextKey of nextKeys) {
|
|
67
|
+
Visit(root, `${path}/${nextKey}`, current[nextKey], next[nextKey]);
|
|
89
68
|
}
|
|
90
69
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
pointer_1.ValuePointer.Set(root, path, next);
|
|
70
|
+
}
|
|
71
|
+
function ArrayType(root, path, current, next) {
|
|
72
|
+
if (!ValueGuard.IsArray(current)) {
|
|
73
|
+
pointer_1.ValuePointer.Set(root, path, ValueClone.Clone(next));
|
|
95
74
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
else if (is_1.Is.TypedArray(next)) {
|
|
101
|
-
return TypedArray(root, path, current, next);
|
|
102
|
-
}
|
|
103
|
-
else if (is_1.Is.Object(next)) {
|
|
104
|
-
return Object(root, path, current, next);
|
|
105
|
-
}
|
|
106
|
-
else if (is_1.Is.Value(next)) {
|
|
107
|
-
return Value(root, path, current, next);
|
|
75
|
+
else {
|
|
76
|
+
for (let index = 0; index < next.length; index++) {
|
|
77
|
+
Visit(root, `${path}/${index}`, current[index], next[index]);
|
|
108
78
|
}
|
|
79
|
+
current.splice(next.length);
|
|
109
80
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if ((is_1.Is.Object(current) && is_1.Is.Array(next)) || (is_1.Is.Array(current) && is_1.Is.Object(next))) {
|
|
116
|
-
throw new ValueMutateTypeMismatchError();
|
|
81
|
+
}
|
|
82
|
+
function TypedArrayType(root, path, current, next) {
|
|
83
|
+
if (ValueGuard.IsTypedArray(current) && current.length === next.length) {
|
|
84
|
+
for (let i = 0; i < current.length; i++) {
|
|
85
|
+
current[i] = next[i];
|
|
117
86
|
}
|
|
118
|
-
Visit(current, '', current, next);
|
|
119
87
|
}
|
|
120
|
-
|
|
121
|
-
|
|
88
|
+
else {
|
|
89
|
+
pointer_1.ValuePointer.Set(root, path, ValueClone.Clone(next));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function ValueType(root, path, current, next) {
|
|
93
|
+
if (current === next)
|
|
94
|
+
return;
|
|
95
|
+
pointer_1.ValuePointer.Set(root, path, next);
|
|
96
|
+
}
|
|
97
|
+
function Visit(root, path, current, next) {
|
|
98
|
+
if (ValueGuard.IsArray(next))
|
|
99
|
+
return ArrayType(root, path, current, next);
|
|
100
|
+
if (ValueGuard.IsTypedArray(next))
|
|
101
|
+
return TypedArrayType(root, path, current, next);
|
|
102
|
+
if (ValueGuard.IsPlainObject(next))
|
|
103
|
+
return ObjectType(root, path, current, next);
|
|
104
|
+
if (ValueGuard.IsValueType(next))
|
|
105
|
+
return ValueType(root, path, current, next);
|
|
106
|
+
}
|
|
107
|
+
// --------------------------------------------------------------------------
|
|
108
|
+
// Mutate
|
|
109
|
+
// --------------------------------------------------------------------------
|
|
110
|
+
function IsNonMutableValue(value) {
|
|
111
|
+
return ValueGuard.IsTypedArray(value) || ValueGuard.IsValueType(value);
|
|
112
|
+
}
|
|
113
|
+
function IsMismatchedValue(current, next) {
|
|
114
|
+
// prettier-ignore
|
|
115
|
+
return ((ValueGuard.IsPlainObject(current) && ValueGuard.IsArray(next)) ||
|
|
116
|
+
(ValueGuard.IsArray(current) && ValueGuard.IsPlainObject(next)));
|
|
117
|
+
}
|
|
118
|
+
// --------------------------------------------------------------------------
|
|
119
|
+
// Mutate
|
|
120
|
+
// --------------------------------------------------------------------------
|
|
121
|
+
/** Performs a deep mutable value assignment while retaining internal references. */
|
|
122
|
+
function Mutate(current, next) {
|
|
123
|
+
if (IsNonMutableValue(current) || IsNonMutableValue(next))
|
|
124
|
+
throw new ValueMutateInvalidRootMutationError();
|
|
125
|
+
if (IsMismatchedValue(current, next))
|
|
126
|
+
throw new ValueMutateTypeMismatchError();
|
|
127
|
+
Visit(current, '', current, next);
|
|
128
|
+
}
|
|
129
|
+
exports.Mutate = Mutate;
|
package/value/pointer.js
CHANGED
|
@@ -28,6 +28,9 @@ THE SOFTWARE.
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.ValuePointer = exports.ValuePointerRootDeleteError = exports.ValuePointerRootSetError = void 0;
|
|
31
|
+
// --------------------------------------------------------------------------
|
|
32
|
+
// Errors
|
|
33
|
+
// --------------------------------------------------------------------------
|
|
31
34
|
class ValuePointerRootSetError extends Error {
|
|
32
35
|
constructor(value, path, update) {
|
|
33
36
|
super('ValuePointer: Cannot set root value');
|
|
@@ -45,6 +48,9 @@ class ValuePointerRootDeleteError extends Error {
|
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
exports.ValuePointerRootDeleteError = ValuePointerRootDeleteError;
|
|
51
|
+
// --------------------------------------------------------------------------
|
|
52
|
+
// ValuePointer
|
|
53
|
+
// --------------------------------------------------------------------------
|
|
48
54
|
/** Provides functionality to update values through RFC6901 string pointers */
|
|
49
55
|
var ValuePointer;
|
|
50
56
|
(function (ValuePointer) {
|
|
@@ -102,7 +108,7 @@ var ValuePointer;
|
|
|
102
108
|
next = next[component];
|
|
103
109
|
key = component;
|
|
104
110
|
}
|
|
105
|
-
if (
|
|
111
|
+
if (Array.isArray(owner)) {
|
|
106
112
|
const index = parseInt(key);
|
|
107
113
|
owner.splice(index, 1);
|
|
108
114
|
}
|
|
@@ -123,7 +129,7 @@ var ValuePointer;
|
|
|
123
129
|
next = next[component];
|
|
124
130
|
key = component;
|
|
125
131
|
}
|
|
126
|
-
return
|
|
132
|
+
return Object.getOwnPropertyNames(owner).includes(key);
|
|
127
133
|
}
|
|
128
134
|
ValuePointer.Has = Has;
|
|
129
135
|
/** Gets the value at the given pointer */
|
package/value/value.d.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
+
import * as ValueErrors from '../errors/index';
|
|
2
|
+
import * as ValueMutate from './mutate';
|
|
3
|
+
import * as ValueDelta from './delta';
|
|
1
4
|
import * as Types from '../typebox';
|
|
2
|
-
|
|
3
|
-
import { Mutable } from './mutate';
|
|
4
|
-
import { Edit } from './delta';
|
|
5
|
-
/** Provides functions to perform structural updates to JavaScript values */
|
|
5
|
+
/** Functions to perform structural operations on JavaScript values */
|
|
6
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.
|
|
8
|
-
function Cast<T extends Types.TSchema
|
|
9
|
-
/** Casts a value into a given type. The return value will retain as much information of the original value as possible.
|
|
7
|
+
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
8
|
+
function Cast<T extends Types.TSchema>(schema: T, references: Types.TSchema[], 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. */
|
|
10
10
|
function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
|
|
11
11
|
/** Creates a value from the given type */
|
|
12
|
-
function Create<T extends Types.TSchema
|
|
12
|
+
function Create<T extends Types.TSchema>(schema: T, references: Types.TSchema[]): Types.Static<T>;
|
|
13
13
|
/** Creates a value from the given type */
|
|
14
14
|
function Create<T extends Types.TSchema>(schema: T): Types.Static<T>;
|
|
15
15
|
/** Returns true if the value matches the given type. */
|
|
16
|
-
function Check<T extends Types.TSchema
|
|
16
|
+
function Check<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): value is Types.Static<T>;
|
|
17
17
|
/** Returns true if the value matches the given type. */
|
|
18
18
|
function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>;
|
|
19
19
|
/** Converts any type mismatched values to their target type if a conversion is possible. */
|
|
20
|
-
function Convert<T extends Types.TSchema
|
|
20
|
+
function Convert<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): unknown;
|
|
21
21
|
/** Converts any type mismatched values to their target type if a conversion is possible. */
|
|
22
22
|
function Convert<T extends Types.TSchema>(schema: T, value: unknown): unknown;
|
|
23
23
|
/** Returns a structural clone of the given value */
|
|
24
24
|
function Clone<T>(value: T): T;
|
|
25
25
|
/** Returns an iterator for each error in this value. */
|
|
26
|
-
function Errors<T extends Types.TSchema
|
|
26
|
+
function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): ValueErrors.ValueErrorIterator;
|
|
27
27
|
/** Returns an iterator for each error in this value. */
|
|
28
|
-
function Errors<T extends Types.TSchema>(schema: T, value: unknown): ValueErrorIterator;
|
|
28
|
+
function Errors<T extends Types.TSchema>(schema: T, value: unknown): ValueErrors.ValueErrorIterator;
|
|
29
29
|
/** Returns true if left and right values are structurally equal */
|
|
30
30
|
function Equal<T>(left: T, right: unknown): right is T;
|
|
31
31
|
/** Returns edits to transform the current value into the next value */
|
|
32
|
-
function Diff(current: unknown, next: unknown): Edit[];
|
|
32
|
+
function Diff(current: unknown, next: unknown): ValueDelta.Edit[];
|
|
33
33
|
/** Returns a FNV1A-64 non cryptographic hash of the given value */
|
|
34
34
|
function Hash(value: unknown): bigint;
|
|
35
35
|
/** Returns a new value with edits applied to the given value */
|
|
36
|
-
function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
36
|
+
function Patch<T = any>(current: unknown, edits: ValueDelta.Edit[]): T;
|
|
37
37
|
/** Performs a deep mutable value assignment while retaining internal references. */
|
|
38
|
-
function Mutate(current: Mutable, next: Mutable): void;
|
|
38
|
+
function Mutate(current: ValueMutate.Mutable, next: ValueMutate.Mutable): void;
|
|
39
39
|
}
|