@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/delta.d.ts
CHANGED
|
@@ -37,7 +37,5 @@ export declare class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
|
37
37
|
readonly value: unknown;
|
|
38
38
|
constructor(value: unknown);
|
|
39
39
|
}
|
|
40
|
-
export declare
|
|
41
|
-
|
|
42
|
-
function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
43
|
-
}
|
|
40
|
+
export declare function Diff(current: unknown, next: unknown): Edit[];
|
|
41
|
+
export declare function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
package/value/delta.js
CHANGED
|
@@ -27,11 +27,11 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
30
|
+
exports.Patch = exports.Diff = exports.ValueDeltaUnableToDiffUnknownValue = exports.ValueDeltaObjectWithSymbolKeyError = exports.Edit = exports.Delete = exports.Update = exports.Insert = void 0;
|
|
31
31
|
const typebox_1 = require("../typebox");
|
|
32
|
-
const is_1 = require("./is");
|
|
33
|
-
const clone_1 = require("./clone");
|
|
34
32
|
const pointer_1 = require("./pointer");
|
|
33
|
+
const ValueGuard = require("./guard");
|
|
34
|
+
const ValueClone = require("./clone");
|
|
35
35
|
exports.Insert = typebox_1.Type.Object({
|
|
36
36
|
type: typebox_1.Type.Literal('insert'),
|
|
37
37
|
path: typebox_1.Type.String(),
|
|
@@ -47,9 +47,9 @@ exports.Delete = typebox_1.Type.Object({
|
|
|
47
47
|
path: typebox_1.Type.String(),
|
|
48
48
|
});
|
|
49
49
|
exports.Edit = typebox_1.Type.Union([exports.Insert, exports.Update, exports.Delete]);
|
|
50
|
-
//
|
|
50
|
+
// --------------------------------------------------------------------------
|
|
51
51
|
// Errors
|
|
52
|
-
//
|
|
52
|
+
// --------------------------------------------------------------------------
|
|
53
53
|
class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
54
54
|
constructor(key) {
|
|
55
55
|
super('ValueDelta: Cannot diff objects with symbol keys');
|
|
@@ -64,141 +64,132 @@ class ValueDeltaUnableToDiffUnknownValue extends Error {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
exports.ValueDeltaUnableToDiffUnknownValue = ValueDeltaUnableToDiffUnknownValue;
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
// --------------------------------------------------------------------------
|
|
68
|
+
// Command Factory
|
|
69
|
+
// --------------------------------------------------------------------------
|
|
70
|
+
function CreateUpdate(path, value) {
|
|
71
|
+
return { type: 'update', path, value };
|
|
72
|
+
}
|
|
73
|
+
function CreateInsert(path, value) {
|
|
74
|
+
return { type: 'insert', path, value };
|
|
75
|
+
}
|
|
76
|
+
function CreateDelete(path) {
|
|
77
|
+
return { type: 'delete', path };
|
|
78
|
+
}
|
|
79
|
+
// --------------------------------------------------------------------------
|
|
80
|
+
// Diffing Generators
|
|
81
|
+
// --------------------------------------------------------------------------
|
|
82
|
+
function* ObjectType(path, current, next) {
|
|
83
|
+
if (!ValueGuard.IsPlainObject(next))
|
|
84
|
+
return yield CreateUpdate(path, next);
|
|
85
|
+
const currentKeys = [...Object.keys(current), ...Object.getOwnPropertySymbols(current)];
|
|
86
|
+
const nextKeys = [...Object.keys(next), ...Object.getOwnPropertySymbols(next)];
|
|
87
|
+
for (const key of currentKeys) {
|
|
88
|
+
if (ValueGuard.IsSymbol(key))
|
|
89
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
90
|
+
if (ValueGuard.IsUndefined(next[key]) && nextKeys.includes(key))
|
|
91
|
+
yield CreateUpdate(`${path}/${String(key)}`, undefined);
|
|
77
92
|
}
|
|
78
|
-
|
|
79
|
-
|
|
93
|
+
for (const key of nextKeys) {
|
|
94
|
+
if (ValueGuard.IsUndefined(current[key]) || ValueGuard.IsUndefined(next[key]))
|
|
95
|
+
continue;
|
|
96
|
+
if (ValueGuard.IsSymbol(key))
|
|
97
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
98
|
+
yield* Visit(`${path}/${String(key)}`, current[key], next[key]);
|
|
80
99
|
}
|
|
81
|
-
|
|
82
|
-
|
|
100
|
+
for (const key of nextKeys) {
|
|
101
|
+
if (ValueGuard.IsSymbol(key))
|
|
102
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
103
|
+
if (ValueGuard.IsUndefined(current[key]))
|
|
104
|
+
yield CreateInsert(`${path}/${String(key)}`, next[key]);
|
|
83
105
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return yield Update(path, next);
|
|
90
|
-
const currentKeys = [...globalThis.Object.keys(current), ...globalThis.Object.getOwnPropertySymbols(current)];
|
|
91
|
-
const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
|
|
92
|
-
for (const key of currentKeys) {
|
|
93
|
-
if (typeof key === 'symbol')
|
|
94
|
-
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
95
|
-
if (next[key] === undefined && nextKeys.includes(key))
|
|
96
|
-
yield Update(`${path}/${String(key)}`, undefined);
|
|
97
|
-
}
|
|
98
|
-
for (const key of nextKeys) {
|
|
99
|
-
if (current[key] === undefined || next[key] === undefined)
|
|
100
|
-
continue;
|
|
101
|
-
if (typeof key === 'symbol')
|
|
102
|
-
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
103
|
-
yield* Visit(`${path}/${String(key)}`, current[key], next[key]);
|
|
104
|
-
}
|
|
105
|
-
for (const key of nextKeys) {
|
|
106
|
-
if (typeof key === 'symbol')
|
|
107
|
-
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
108
|
-
if (current[key] === undefined)
|
|
109
|
-
yield Insert(`${path}/${String(key)}`, next[key]);
|
|
110
|
-
}
|
|
111
|
-
for (const key of currentKeys.reverse()) {
|
|
112
|
-
if (typeof key === 'symbol')
|
|
113
|
-
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
114
|
-
if (next[key] === undefined && !nextKeys.includes(key))
|
|
115
|
-
yield Delete(`${path}/${String(key)}`);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function* Array(path, current, next) {
|
|
119
|
-
if (!is_1.Is.Array(next))
|
|
120
|
-
return yield Update(path, next);
|
|
121
|
-
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
122
|
-
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
123
|
-
}
|
|
124
|
-
for (let i = 0; i < next.length; i++) {
|
|
125
|
-
if (i < current.length)
|
|
126
|
-
continue;
|
|
127
|
-
yield Insert(`${path}/${i}`, next[i]);
|
|
128
|
-
}
|
|
129
|
-
for (let i = current.length - 1; i >= 0; i--) {
|
|
130
|
-
if (i < next.length)
|
|
131
|
-
continue;
|
|
132
|
-
yield Delete(`${path}/${i}`);
|
|
133
|
-
}
|
|
106
|
+
for (const key of currentKeys.reverse()) {
|
|
107
|
+
if (ValueGuard.IsSymbol(key))
|
|
108
|
+
throw new ValueDeltaObjectWithSymbolKeyError(key);
|
|
109
|
+
if (ValueGuard.IsUndefined(next[key]) && !nextKeys.includes(key))
|
|
110
|
+
yield CreateDelete(`${path}/${String(key)}`);
|
|
134
111
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
112
|
+
}
|
|
113
|
+
function* ArrayType(path, current, next) {
|
|
114
|
+
if (!ValueGuard.IsArray(next))
|
|
115
|
+
return yield CreateUpdate(path, next);
|
|
116
|
+
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
117
|
+
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
141
118
|
}
|
|
142
|
-
|
|
143
|
-
if (
|
|
144
|
-
|
|
145
|
-
yield
|
|
119
|
+
for (let i = 0; i < next.length; i++) {
|
|
120
|
+
if (i < current.length)
|
|
121
|
+
continue;
|
|
122
|
+
yield CreateInsert(`${path}/${i}`, next[i]);
|
|
146
123
|
}
|
|
147
|
-
|
|
148
|
-
if (
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
else if (is_1.Is.Array(current)) {
|
|
152
|
-
return yield* Array(path, current, next);
|
|
153
|
-
}
|
|
154
|
-
else if (is_1.Is.TypedArray(current)) {
|
|
155
|
-
return yield* TypedArray(path, current, next);
|
|
156
|
-
}
|
|
157
|
-
else if (is_1.Is.Value(current)) {
|
|
158
|
-
return yield* Value(path, current, next);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
throw new ValueDeltaUnableToDiffUnknownValue(current);
|
|
162
|
-
}
|
|
124
|
+
for (let i = current.length - 1; i >= 0; i--) {
|
|
125
|
+
if (i < next.length)
|
|
126
|
+
continue;
|
|
127
|
+
yield CreateDelete(`${path}/${i}`);
|
|
163
128
|
}
|
|
164
|
-
|
|
165
|
-
|
|
129
|
+
}
|
|
130
|
+
function* TypedArrayType(path, current, next) {
|
|
131
|
+
if (!ValueGuard.IsTypedArray(next) || current.length !== next.length || Object.getPrototypeOf(current).constructor.name !== Object.getPrototypeOf(next).constructor.name)
|
|
132
|
+
return yield CreateUpdate(path, next);
|
|
133
|
+
for (let i = 0; i < Math.min(current.length, next.length); i++) {
|
|
134
|
+
yield* Visit(`${path}/${i}`, current[i], next[i]);
|
|
166
135
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
136
|
+
}
|
|
137
|
+
function* ValueType(path, current, next) {
|
|
138
|
+
if (current === next)
|
|
139
|
+
return;
|
|
140
|
+
yield CreateUpdate(path, next);
|
|
141
|
+
}
|
|
142
|
+
function* Visit(path, current, next) {
|
|
143
|
+
if (ValueGuard.IsPlainObject(current))
|
|
144
|
+
return yield* ObjectType(path, current, next);
|
|
145
|
+
if (ValueGuard.IsArray(current))
|
|
146
|
+
return yield* ArrayType(path, current, next);
|
|
147
|
+
if (ValueGuard.IsTypedArray(current))
|
|
148
|
+
return yield* TypedArrayType(path, current, next);
|
|
149
|
+
if (ValueGuard.IsValueType(current))
|
|
150
|
+
return yield* ValueType(path, current, next);
|
|
151
|
+
throw new ValueDeltaUnableToDiffUnknownValue(current);
|
|
152
|
+
}
|
|
153
|
+
// ---------------------------------------------------------------------
|
|
154
|
+
// Diff
|
|
155
|
+
// ---------------------------------------------------------------------
|
|
156
|
+
function Diff(current, next) {
|
|
157
|
+
return [...Visit('', current, next)];
|
|
158
|
+
}
|
|
159
|
+
exports.Diff = Diff;
|
|
160
|
+
// ---------------------------------------------------------------------
|
|
161
|
+
// Patch
|
|
162
|
+
// ---------------------------------------------------------------------
|
|
163
|
+
function IsRootUpdate(edits) {
|
|
164
|
+
return edits.length > 0 && edits[0].path === '' && edits[0].type === 'update';
|
|
165
|
+
}
|
|
166
|
+
function IsIdentity(edits) {
|
|
167
|
+
return edits.length === 0;
|
|
168
|
+
}
|
|
169
|
+
function Patch(current, edits) {
|
|
170
|
+
if (IsRootUpdate(edits)) {
|
|
171
|
+
return ValueClone.Clone(edits[0].value);
|
|
173
172
|
}
|
|
174
|
-
|
|
175
|
-
return
|
|
173
|
+
if (IsIdentity(edits)) {
|
|
174
|
+
return ValueClone.Clone(current);
|
|
176
175
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
case 'update': {
|
|
192
|
-
pointer_1.ValuePointer.Set(clone, edit.path, edit.value);
|
|
193
|
-
break;
|
|
194
|
-
}
|
|
195
|
-
case 'delete': {
|
|
196
|
-
pointer_1.ValuePointer.Delete(clone, edit.path);
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
176
|
+
const clone = ValueClone.Clone(current);
|
|
177
|
+
for (const edit of edits) {
|
|
178
|
+
switch (edit.type) {
|
|
179
|
+
case 'insert': {
|
|
180
|
+
pointer_1.ValuePointer.Set(clone, edit.path, edit.value);
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
case 'update': {
|
|
184
|
+
pointer_1.ValuePointer.Set(clone, edit.path, edit.value);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
case 'delete': {
|
|
188
|
+
pointer_1.ValuePointer.Delete(clone, edit.path);
|
|
189
|
+
break;
|
|
199
190
|
}
|
|
200
191
|
}
|
|
201
|
-
return clone;
|
|
202
192
|
}
|
|
203
|
-
|
|
204
|
-
}
|
|
193
|
+
return clone;
|
|
194
|
+
}
|
|
195
|
+
exports.Patch = Patch;
|
package/value/equal.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
1
|
+
/** Returns true if the left value deep-equals the right */
|
|
2
|
+
export declare function Equal<T>(left: T, right: unknown): right is T;
|
package/value/equal.js
CHANGED
|
@@ -27,54 +27,51 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
ValueEqual.Equal = Equal;
|
|
80
|
-
})(ValueEqual || (exports.ValueEqual = ValueEqual = {}));
|
|
30
|
+
exports.Equal = void 0;
|
|
31
|
+
const ValueGuard = require("./guard");
|
|
32
|
+
// --------------------------------------------------------------------------
|
|
33
|
+
// Equality Checks
|
|
34
|
+
// --------------------------------------------------------------------------
|
|
35
|
+
function ObjectType(left, right) {
|
|
36
|
+
if (!ValueGuard.IsPlainObject(right))
|
|
37
|
+
return false;
|
|
38
|
+
const leftKeys = [...Object.keys(left), ...Object.getOwnPropertySymbols(left)];
|
|
39
|
+
const rightKeys = [...Object.keys(right), ...Object.getOwnPropertySymbols(right)];
|
|
40
|
+
if (leftKeys.length !== rightKeys.length)
|
|
41
|
+
return false;
|
|
42
|
+
return leftKeys.every((key) => Equal(left[key], right[key]));
|
|
43
|
+
}
|
|
44
|
+
function DateType(left, right) {
|
|
45
|
+
return ValueGuard.IsDate(right) && left.getTime() === right.getTime();
|
|
46
|
+
}
|
|
47
|
+
function ArrayType(left, right) {
|
|
48
|
+
if (!ValueGuard.IsArray(right) || left.length !== right.length)
|
|
49
|
+
return false;
|
|
50
|
+
return left.every((value, index) => Equal(value, right[index]));
|
|
51
|
+
}
|
|
52
|
+
function TypedArrayType(left, right) {
|
|
53
|
+
if (!ValueGuard.IsTypedArray(right) || left.length !== right.length || Object.getPrototypeOf(left).constructor.name !== Object.getPrototypeOf(right).constructor.name)
|
|
54
|
+
return false;
|
|
55
|
+
return left.every((value, index) => Equal(value, right[index]));
|
|
56
|
+
}
|
|
57
|
+
function ValueType(left, right) {
|
|
58
|
+
return left === right;
|
|
59
|
+
}
|
|
60
|
+
// --------------------------------------------------------------------------
|
|
61
|
+
// Equal
|
|
62
|
+
// --------------------------------------------------------------------------
|
|
63
|
+
/** Returns true if the left value deep-equals the right */
|
|
64
|
+
function Equal(left, right) {
|
|
65
|
+
if (ValueGuard.IsPlainObject(left))
|
|
66
|
+
return ObjectType(left, right);
|
|
67
|
+
if (ValueGuard.IsDate(left))
|
|
68
|
+
return DateType(left, right);
|
|
69
|
+
if (ValueGuard.IsTypedArray(left))
|
|
70
|
+
return TypedArrayType(left, right);
|
|
71
|
+
if (ValueGuard.IsArray(left))
|
|
72
|
+
return ArrayType(left, right);
|
|
73
|
+
if (ValueGuard.IsValueType(left))
|
|
74
|
+
return ValueType(left, right);
|
|
75
|
+
throw new Error('ValueEquals: Unable to compare value');
|
|
76
|
+
}
|
|
77
|
+
exports.Equal = Equal;
|
package/value/guard.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type ObjectType = Record<PropertyKey, unknown>;
|
|
2
|
+
export type ArrayType = unknown[];
|
|
3
|
+
export type ValueType = null | undefined | symbol | bigint | number | boolean | string;
|
|
4
|
+
export type TypedArrayType = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
|
|
5
|
+
/** Returns true if this value is an async iterator */
|
|
6
|
+
export declare function IsAsyncIterator(value: unknown): value is AsyncIterableIterator<any>;
|
|
7
|
+
/** Returns true if this value is an iterator */
|
|
8
|
+
export declare function IsIterator(value: unknown): value is IterableIterator<any>;
|
|
9
|
+
/** Returns true if this value is a typed array */
|
|
10
|
+
export declare function IsTypedArray(value: unknown): value is TypedArrayType;
|
|
11
|
+
/** Returns true if this value is a Promise */
|
|
12
|
+
export declare function IsPromise(value: unknown): value is Promise<unknown>;
|
|
13
|
+
/** Returns true if the value is a Uint8Array */
|
|
14
|
+
export declare function IsUint8Array(value: unknown): value is Uint8Array;
|
|
15
|
+
/** Returns true if this value is a Date */
|
|
16
|
+
export declare function IsDate(value: unknown): value is Date;
|
|
17
|
+
/** Returns true if this value has this property key */
|
|
18
|
+
export declare function HasPropertyKey<K extends PropertyKey>(value: Record<any, unknown>, key: K): value is ObjectType & Record<K, unknown>;
|
|
19
|
+
/** Returns true if this object is not an instance of any other type */
|
|
20
|
+
export declare function IsPlainObject(value: unknown): value is ObjectType;
|
|
21
|
+
/** Returns true of this value is an object type */
|
|
22
|
+
export declare function IsObject(value: unknown): value is ObjectType;
|
|
23
|
+
/** Returns true if this value is an array, but not a typed array */
|
|
24
|
+
export declare function IsArray(value: unknown): value is ArrayType;
|
|
25
|
+
/** Returns true if this value is an undefined */
|
|
26
|
+
export declare function IsUndefined(value: unknown): value is undefined;
|
|
27
|
+
/** Returns true if this value is an null */
|
|
28
|
+
export declare function IsNull(value: unknown): value is null;
|
|
29
|
+
/** Returns true if this value is an boolean */
|
|
30
|
+
export declare function IsBoolean(value: unknown): value is boolean;
|
|
31
|
+
/** Returns true if this value is an number */
|
|
32
|
+
export declare function IsNumber(value: unknown): value is number;
|
|
33
|
+
/** Returns true if this value is an integer */
|
|
34
|
+
export declare function IsInteger(value: unknown): value is number;
|
|
35
|
+
/** Returns true if this value is bigint */
|
|
36
|
+
export declare function IsBigInt(value: unknown): value is bigint;
|
|
37
|
+
/** Returns true if this value is string */
|
|
38
|
+
export declare function IsString(value: unknown): value is string;
|
|
39
|
+
/** Returns true if this value is a function */
|
|
40
|
+
export declare function IsFunction(value: unknown): value is Function;
|
|
41
|
+
/** Returns true if this value is a symbol */
|
|
42
|
+
export declare function IsSymbol(value: unknown): value is symbol;
|
|
43
|
+
/** Returns true if this value is a value type such as number, string, boolean */
|
|
44
|
+
export declare function IsValueType(value: unknown): value is ValueType;
|
package/value/guard.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*--------------------------------------------------------------------------
|
|
3
|
+
|
|
4
|
+
@sinclair/typebox/value
|
|
5
|
+
|
|
6
|
+
The MIT License (MIT)
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2017-2023 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.IsValueType = exports.IsSymbol = exports.IsFunction = exports.IsString = exports.IsBigInt = exports.IsInteger = exports.IsNumber = exports.IsBoolean = exports.IsNull = exports.IsUndefined = exports.IsArray = exports.IsObject = exports.IsPlainObject = exports.HasPropertyKey = exports.IsDate = exports.IsUint8Array = exports.IsPromise = exports.IsTypedArray = exports.IsIterator = exports.IsAsyncIterator = void 0;
|
|
31
|
+
// --------------------------------------------------------------------------
|
|
32
|
+
// Iterators
|
|
33
|
+
// --------------------------------------------------------------------------
|
|
34
|
+
/** Returns true if this value is an async iterator */
|
|
35
|
+
function IsAsyncIterator(value) {
|
|
36
|
+
return IsObject(value) && Symbol.asyncIterator in value;
|
|
37
|
+
}
|
|
38
|
+
exports.IsAsyncIterator = IsAsyncIterator;
|
|
39
|
+
/** Returns true if this value is an iterator */
|
|
40
|
+
function IsIterator(value) {
|
|
41
|
+
return IsObject(value) && Symbol.iterator in value;
|
|
42
|
+
}
|
|
43
|
+
exports.IsIterator = IsIterator;
|
|
44
|
+
// --------------------------------------------------------------------------
|
|
45
|
+
// Nominal
|
|
46
|
+
// --------------------------------------------------------------------------
|
|
47
|
+
/** Returns true if this value is a typed array */
|
|
48
|
+
function IsTypedArray(value) {
|
|
49
|
+
return ArrayBuffer.isView(value);
|
|
50
|
+
}
|
|
51
|
+
exports.IsTypedArray = IsTypedArray;
|
|
52
|
+
/** Returns true if this value is a Promise */
|
|
53
|
+
function IsPromise(value) {
|
|
54
|
+
return value instanceof Promise;
|
|
55
|
+
}
|
|
56
|
+
exports.IsPromise = IsPromise;
|
|
57
|
+
/** Returns true if the value is a Uint8Array */
|
|
58
|
+
function IsUint8Array(value) {
|
|
59
|
+
return value instanceof Uint8Array;
|
|
60
|
+
}
|
|
61
|
+
exports.IsUint8Array = IsUint8Array;
|
|
62
|
+
/** Returns true if this value is a Date */
|
|
63
|
+
function IsDate(value) {
|
|
64
|
+
return value instanceof Date;
|
|
65
|
+
}
|
|
66
|
+
exports.IsDate = IsDate;
|
|
67
|
+
// --------------------------------------------------------------------------
|
|
68
|
+
// Standard
|
|
69
|
+
// --------------------------------------------------------------------------
|
|
70
|
+
/** Returns true if this value has this property key */
|
|
71
|
+
function HasPropertyKey(value, key) {
|
|
72
|
+
return key in value;
|
|
73
|
+
}
|
|
74
|
+
exports.HasPropertyKey = HasPropertyKey;
|
|
75
|
+
/** Returns true if this object is not an instance of any other type */
|
|
76
|
+
function IsPlainObject(value) {
|
|
77
|
+
return IsObject(value) && IsFunction(value.constructor) && value.constructor.name === 'Object';
|
|
78
|
+
}
|
|
79
|
+
exports.IsPlainObject = IsPlainObject;
|
|
80
|
+
/** Returns true of this value is an object type */
|
|
81
|
+
function IsObject(value) {
|
|
82
|
+
return value !== null && typeof value === 'object';
|
|
83
|
+
}
|
|
84
|
+
exports.IsObject = IsObject;
|
|
85
|
+
/** Returns true if this value is an array, but not a typed array */
|
|
86
|
+
function IsArray(value) {
|
|
87
|
+
return Array.isArray(value) && !ArrayBuffer.isView(value);
|
|
88
|
+
}
|
|
89
|
+
exports.IsArray = IsArray;
|
|
90
|
+
/** Returns true if this value is an undefined */
|
|
91
|
+
function IsUndefined(value) {
|
|
92
|
+
return value === undefined;
|
|
93
|
+
}
|
|
94
|
+
exports.IsUndefined = IsUndefined;
|
|
95
|
+
/** Returns true if this value is an null */
|
|
96
|
+
function IsNull(value) {
|
|
97
|
+
return value === null;
|
|
98
|
+
}
|
|
99
|
+
exports.IsNull = IsNull;
|
|
100
|
+
/** Returns true if this value is an boolean */
|
|
101
|
+
function IsBoolean(value) {
|
|
102
|
+
return typeof value === 'boolean';
|
|
103
|
+
}
|
|
104
|
+
exports.IsBoolean = IsBoolean;
|
|
105
|
+
/** Returns true if this value is an number */
|
|
106
|
+
function IsNumber(value) {
|
|
107
|
+
return typeof value === 'number';
|
|
108
|
+
}
|
|
109
|
+
exports.IsNumber = IsNumber;
|
|
110
|
+
/** Returns true if this value is an integer */
|
|
111
|
+
function IsInteger(value) {
|
|
112
|
+
return IsNumber(value) && Number.isInteger(value);
|
|
113
|
+
}
|
|
114
|
+
exports.IsInteger = IsInteger;
|
|
115
|
+
/** Returns true if this value is bigint */
|
|
116
|
+
function IsBigInt(value) {
|
|
117
|
+
return typeof value === 'bigint';
|
|
118
|
+
}
|
|
119
|
+
exports.IsBigInt = IsBigInt;
|
|
120
|
+
/** Returns true if this value is string */
|
|
121
|
+
function IsString(value) {
|
|
122
|
+
return typeof value === 'string';
|
|
123
|
+
}
|
|
124
|
+
exports.IsString = IsString;
|
|
125
|
+
/** Returns true if this value is a function */
|
|
126
|
+
function IsFunction(value) {
|
|
127
|
+
return typeof value === 'function';
|
|
128
|
+
}
|
|
129
|
+
exports.IsFunction = IsFunction;
|
|
130
|
+
/** Returns true if this value is a symbol */
|
|
131
|
+
function IsSymbol(value) {
|
|
132
|
+
return typeof value === 'symbol';
|
|
133
|
+
}
|
|
134
|
+
exports.IsSymbol = IsSymbol;
|
|
135
|
+
/** Returns true if this value is a value type such as number, string, boolean */
|
|
136
|
+
function IsValueType(value) {
|
|
137
|
+
// prettier-ignore
|
|
138
|
+
return (IsBigInt(value) ||
|
|
139
|
+
IsBoolean(value) ||
|
|
140
|
+
IsNull(value) ||
|
|
141
|
+
IsNumber(value) ||
|
|
142
|
+
IsString(value) ||
|
|
143
|
+
IsSymbol(value) ||
|
|
144
|
+
IsUndefined(value));
|
|
145
|
+
}
|
|
146
|
+
exports.IsValueType = IsValueType;
|
package/value/hash.d.ts
CHANGED
|
@@ -2,7 +2,18 @@ export declare class ValueHashError extends Error {
|
|
|
2
2
|
readonly value: unknown;
|
|
3
3
|
constructor(value: unknown);
|
|
4
4
|
}
|
|
5
|
-
export declare
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export declare enum ByteMarker {
|
|
6
|
+
Undefined = 0,
|
|
7
|
+
Null = 1,
|
|
8
|
+
Boolean = 2,
|
|
9
|
+
Number = 3,
|
|
10
|
+
String = 4,
|
|
11
|
+
Object = 5,
|
|
12
|
+
Array = 6,
|
|
13
|
+
Date = 7,
|
|
14
|
+
Uint8Array = 8,
|
|
15
|
+
Symbol = 9,
|
|
16
|
+
BigInt = 10
|
|
8
17
|
}
|
|
18
|
+
/** Creates a FNV1A-64 non cryptographic hash of the given value */
|
|
19
|
+
export declare function Hash(value: unknown): bigint;
|