@sinclair/typebox 0.27.0 → 0.27.2

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/errors/errors.js CHANGED
@@ -505,12 +505,12 @@ var ValueErrors;
505
505
  return;
506
506
  errors.push(...variantErrors);
507
507
  }
508
- for (const error of errors) {
509
- yield error;
510
- }
511
508
  if (errors.length > 0) {
512
509
  yield { type: ValueErrorType.Union, schema, path, value, message: 'Expected value of union' };
513
510
  }
511
+ for (const error of errors) {
512
+ yield error;
513
+ }
514
514
  }
515
515
  function* Uint8Array(schema, references, path, value) {
516
516
  if (!(value instanceof globalThis.Uint8Array)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.27.0",
3
+ "version": "0.27.2",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -96,6 +96,7 @@ License MIT
96
96
  - [Diff](#values-diff)
97
97
  - [Patch](#values-patch)
98
98
  - [Errors](#values-errors)
99
+ - [Mutate](#values-mutate)
99
100
  - [Pointer](#values-pointer)
100
101
  - [TypeCheck](#typecheck)
101
102
  - [Ajv](#typecheck-ajv)
@@ -1053,7 +1054,6 @@ const E = Value.Diff(A, B) // const E = [
1053
1054
  const C = Value.Patch<typeof B>(A, E) // const C = { x: 3 }
1054
1055
  ```
1055
1056
 
1056
-
1057
1057
  <a name='values-errors'></a>
1058
1058
 
1059
1059
  ### Errors
@@ -1076,6 +1076,29 @@ const R = [...Value.Errors(T, { x: '42' })] // const R = [{
1076
1076
  // }]
1077
1077
  ```
1078
1078
 
1079
+ <a name='values-mutate'></a>
1080
+
1081
+ ### Mutate
1082
+
1083
+ Use the Mutate function to perform a deep mutable value assignment while retaining internal references.
1084
+
1085
+ ```typescript
1086
+ const Y = { z: 1 } // const Y = { z: 1 }
1087
+
1088
+ const X = { y: Y } // const X = { y: { z: 1 } }
1089
+
1090
+ const A = { x: X } // const A = { x: { y: { z: 1 } } }
1091
+
1092
+
1093
+ Value.Mutate(A, { x: { y: { z: 2 } } }) // const A' = { x: { y: { z: 2 } } }
1094
+
1095
+ const R0 = A.x.y.z === 2 // const R0 = 2
1096
+
1097
+ const R1 = A.x.y === Y // const R1 = true
1098
+
1099
+ const R2 = A.x === X // const R2 = true
1100
+ ```
1101
+
1079
1102
  <a name='values-pointer'></a>
1080
1103
 
1081
1104
  ### Pointer
@@ -1087,10 +1110,13 @@ import { ValuePointer } from '@sinclair/typebox/value'
1087
1110
 
1088
1111
  const A = { x: 0, y: 0, z: 0 }
1089
1112
 
1090
- ValuePointer.Set(A, '/x', 1) // const A = { x: 1, y: 0, z: 0 }
1091
- ValuePointer.Set(A, '/y', 1) // const A = { x: 1, y: 1, z: 0 }
1092
- ValuePointer.Set(A, '/z', 1) // const A = { x: 1, y: 1, z: 1 }
1113
+ ValuePointer.Set(A, '/x', 1) // const A' = { x: 1, y: 0, z: 0 }
1114
+
1115
+ ValuePointer.Set(A, '/y', 1) // const A' = { x: 1, y: 1, z: 0 }
1116
+
1117
+ ValuePointer.Set(A, '/z', 1) // const A' = { x: 1, y: 1, z: 1 }
1093
1118
  ```
1119
+
1094
1120
  <a name='typecheck'></a>
1095
1121
 
1096
1122
  ## TypeCheck
package/value/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { ValueError, ValueErrorIterator, ValueErrorType } from '../errors/index';
2
2
  export { ValueHash } from './hash';
3
3
  export { Edit, Insert, Update, Delete } from './delta';
4
+ export { Mutable } from './mutate';
4
5
  export * from './pointer';
5
6
  export * from './value';
@@ -0,0 +1,13 @@
1
+ export declare class ValueMutateTypeMismatchError extends Error {
2
+ constructor();
3
+ }
4
+ export declare class ValueMutateInvalidRootMutationError extends Error {
5
+ constructor();
6
+ }
7
+ export type Mutable = {
8
+ [key: string]: unknown;
9
+ } | unknown[];
10
+ export declare namespace ValueMutate {
11
+ /** Performs a deep mutable value assignment while retaining internal references. */
12
+ function Mutate(current: Mutable, next: Mutable): void;
13
+ }
@@ -0,0 +1,121 @@
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.ValueMutate = exports.ValueMutateInvalidRootMutationError = exports.ValueMutateTypeMismatchError = void 0;
31
+ const is_1 = require("./is");
32
+ const pointer_1 = require("./pointer");
33
+ const clone_1 = require("./clone");
34
+ class ValueMutateTypeMismatchError extends Error {
35
+ constructor() {
36
+ super('ValueMutate: Cannot assign due type mismatch of assignable values');
37
+ }
38
+ }
39
+ exports.ValueMutateTypeMismatchError = ValueMutateTypeMismatchError;
40
+ class ValueMutateInvalidRootMutationError extends Error {
41
+ constructor() {
42
+ super('ValueMutate: Only object and array types can be mutated at the root level');
43
+ }
44
+ }
45
+ exports.ValueMutateInvalidRootMutationError = ValueMutateInvalidRootMutationError;
46
+ var ValueMutate;
47
+ (function (ValueMutate) {
48
+ function Object(root, path, current, next) {
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
+ }
69
+ }
70
+ function Array(root, path, current, next) {
71
+ if (!is_1.Is.Array(current)) {
72
+ pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next));
73
+ }
74
+ else {
75
+ for (let index = 0; index < next.length; index++) {
76
+ Visit(root, `${path}/${index}`, current[index], next[index]);
77
+ }
78
+ current.splice(next.length);
79
+ }
80
+ }
81
+ function TypedArray(root, path, current, next) {
82
+ if (is_1.Is.TypedArray(current) && current.length === next.length) {
83
+ for (let i = 0; i < current.length; i++) {
84
+ current[i] = next[i];
85
+ }
86
+ }
87
+ else {
88
+ pointer_1.ValuePointer.Set(root, path, clone_1.ValueClone.Clone(next));
89
+ }
90
+ }
91
+ function Value(root, path, current, next) {
92
+ if (current === next)
93
+ return;
94
+ pointer_1.ValuePointer.Set(root, path, next);
95
+ }
96
+ function Visit(root, path, current, next) {
97
+ if (is_1.Is.Array(next)) {
98
+ return Array(root, path, current, next);
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);
108
+ }
109
+ }
110
+ /** Performs a deep mutable value assignment while retaining internal references. */
111
+ function Mutate(current, next) {
112
+ if (is_1.Is.TypedArray(current) || is_1.Is.Value(current) || is_1.Is.TypedArray(next) || is_1.Is.Value(next)) {
113
+ throw new ValueMutateInvalidRootMutationError();
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();
117
+ }
118
+ Visit(current, '', current, next);
119
+ }
120
+ ValueMutate.Mutate = Mutate;
121
+ })(ValueMutate = exports.ValueMutate || (exports.ValueMutate = {}));
package/value/value.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as Types from '../typebox';
2
2
  import { ValueErrorIterator } from '../errors/index';
3
+ import { Mutable } from './mutate';
3
4
  import { Edit } from './delta';
4
5
  /** Provides functions to perform structural updates to JavaScript values */
5
6
  export declare namespace Value {
@@ -33,4 +34,6 @@ export declare namespace Value {
33
34
  function Hash(value: unknown): bigint;
34
35
  /** Returns a new value with edits applied to the given value */
35
36
  function Patch<T = any>(current: unknown, edits: Edit[]): T;
37
+ /** Performs a deep mutable value assignment while retaining internal references. */
38
+ function Mutate(current: Mutable, next: Mutable): void;
36
39
  }
package/value/value.js CHANGED
@@ -29,6 +29,7 @@ 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 mutate_1 = require("./mutate");
32
33
  const hash_1 = require("./hash");
33
34
  const equal_1 = require("./equal");
34
35
  const cast_1 = require("./cast");
@@ -90,4 +91,9 @@ var Value;
90
91
  return delta_1.ValueDelta.Patch(current, edits);
91
92
  }
92
93
  Value.Patch = Patch;
94
+ /** Performs a deep mutable value assignment while retaining internal references. */
95
+ function Mutate(current, next) {
96
+ mutate_1.ValueMutate.Mutate(current, next);
97
+ }
98
+ Value.Mutate = Mutate;
93
99
  })(Value = exports.Value || (exports.Value = {}));