@twin.org/core 0.0.1-next.47 → 0.0.1-next.49

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.
@@ -2353,12 +2353,15 @@ class Converter {
2353
2353
  }
2354
2354
  }
2355
2355
 
2356
- // Copyright 2024 IOTA Stiftung.
2357
- // SPDX-License-Identifier: Apache-2.0.
2358
2356
  /**
2359
2357
  * Helpers methods for JSON objects.
2360
2358
  */
2361
2359
  class JsonHelper {
2360
+ /**
2361
+ * Runtime name for the class.
2362
+ * @internal
2363
+ */
2364
+ static _CLASS_NAME = "JsonHelper";
2362
2365
  /**
2363
2366
  * Serializes in canonical format.
2364
2367
  * Based on https://www.rfc-editor.org/rfc/rfc8785.
@@ -2417,9 +2420,17 @@ class JsonHelper {
2417
2420
  * @param object The object to patch.
2418
2421
  * @param patches The second object.
2419
2422
  * @returns The updated object.
2423
+ * @throws GeneralError if the patch fails.
2420
2424
  */
2421
2425
  static patch(object, patches) {
2422
- return rfc6902.applyPatch(object, patches);
2426
+ const clone = ObjectHelper.clone(object);
2427
+ const result = rfc6902.applyPatch(clone, patches);
2428
+ for (let i = 0; i < result.length; i++) {
2429
+ if (!Is.empty(result[i])) {
2430
+ throw new GeneralError(JsonHelper._CLASS_NAME, "failedPatch", { index: i }, result[i]);
2431
+ }
2432
+ }
2433
+ return clone;
2423
2434
  }
2424
2435
  /**
2425
2436
  * Stringify the JSON with support for extended data types date/bigint/uint8array.
@@ -2351,12 +2351,15 @@ class Converter {
2351
2351
  }
2352
2352
  }
2353
2353
 
2354
- // Copyright 2024 IOTA Stiftung.
2355
- // SPDX-License-Identifier: Apache-2.0.
2356
2354
  /**
2357
2355
  * Helpers methods for JSON objects.
2358
2356
  */
2359
2357
  class JsonHelper {
2358
+ /**
2359
+ * Runtime name for the class.
2360
+ * @internal
2361
+ */
2362
+ static _CLASS_NAME = "JsonHelper";
2360
2363
  /**
2361
2364
  * Serializes in canonical format.
2362
2365
  * Based on https://www.rfc-editor.org/rfc/rfc8785.
@@ -2415,9 +2418,17 @@ class JsonHelper {
2415
2418
  * @param object The object to patch.
2416
2419
  * @param patches The second object.
2417
2420
  * @returns The updated object.
2421
+ * @throws GeneralError if the patch fails.
2418
2422
  */
2419
2423
  static patch(object, patches) {
2420
- return applyPatch(object, patches);
2424
+ const clone = ObjectHelper.clone(object);
2425
+ const result = applyPatch(clone, patches);
2426
+ for (let i = 0; i < result.length; i++) {
2427
+ if (!Is.empty(result[i])) {
2428
+ throw new GeneralError(JsonHelper._CLASS_NAME, "failedPatch", { index: i }, result[i]);
2429
+ }
2430
+ }
2431
+ return clone;
2421
2432
  }
2422
2433
  /**
2423
2434
  * Stringify the JSON with support for extended data types date/bigint/uint8array.
@@ -24,6 +24,7 @@ export declare class JsonHelper {
24
24
  * @param object The object to patch.
25
25
  * @param patches The second object.
26
26
  * @returns The updated object.
27
+ * @throws GeneralError if the patch fails.
27
28
  */
28
29
  static patch<T = unknown>(object: T, patches: IPatchOperation[]): T;
29
30
  /**
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.47
3
+ ## 0.0.1-next.49
4
4
 
5
5
  - Initial Release
@@ -254,7 +254,7 @@ TypeError If the value can not be coerced.
254
254
 
255
255
  ### uint8Array()
256
256
 
257
- > `static` **uint8Array**(`value`): `undefined` \| `Uint8Array`
257
+ > `static` **uint8Array**(`value`): `undefined` \| `Uint8Array`\<`ArrayBufferLike`\>
258
258
 
259
259
  Coerce the value to a Uint8Array.
260
260
 
@@ -268,7 +268,7 @@ The value to coerce.
268
268
 
269
269
  #### Returns
270
270
 
271
- `undefined` \| `Uint8Array`
271
+ `undefined` \| `Uint8Array`\<`ArrayBufferLike`\>
272
272
 
273
273
  The value if it can be coerced.
274
274
 
@@ -16,7 +16,7 @@ A class to handle compression.
16
16
 
17
17
  ### compress()
18
18
 
19
- > `static` **compress**(`bytes`, `type`): `Promise`\<`Uint8Array`\>
19
+ > `static` **compress**(`bytes`, `type`): `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
20
20
 
21
21
  Compress bytes using GZIP.
22
22
 
@@ -36,7 +36,7 @@ The type of compression to use.
36
36
 
37
37
  #### Returns
38
38
 
39
- `Promise`\<`Uint8Array`\>
39
+ `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
40
40
 
41
41
  The compressed bytes.
42
42
 
@@ -44,7 +44,7 @@ The compressed bytes.
44
44
 
45
45
  ### decompress()
46
46
 
47
- > `static` **decompress**(`compressedBytes`, `type`): `Promise`\<`Uint8Array`\>
47
+ > `static` **decompress**(`compressedBytes`, `type`): `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
48
48
 
49
49
  Decompress a gzipped compressed byte array.
50
50
 
@@ -64,6 +64,6 @@ The type of compression to use.
64
64
 
65
65
  #### Returns
66
66
 
67
- `Promise`\<`Uint8Array`\>
67
+ `Promise`\<`Uint8Array`\<`ArrayBufferLike`\>\>
68
68
 
69
69
  The decompressed bytes.
@@ -816,7 +816,7 @@ GuardError If the value does not match the assertion.
816
816
 
817
817
  ### uint8Array()
818
818
 
819
- > `static` **uint8Array**(`source`, `property`, `value`): `asserts value is Uint8Array`
819
+ > `static` **uint8Array**(`source`, `property`, `value`): `asserts value is Uint8Array<ArrayBufferLike>`
820
820
 
821
821
  Is the property a Uint8Array.
822
822
 
@@ -842,7 +842,7 @@ The value to test.
842
842
 
843
843
  #### Returns
844
844
 
845
- `asserts value is Uint8Array`
845
+ `asserts value is Uint8Array<ArrayBufferLike>`
846
846
 
847
847
  #### Throws
848
848
 
@@ -676,7 +676,7 @@ True if the value is an element from the options array.
676
676
 
677
677
  ### uint8Array()
678
678
 
679
- > `static` **uint8Array**(`value`): `value is Uint8Array`
679
+ > `static` **uint8Array**(`value`): `value is Uint8Array<ArrayBufferLike>`
680
680
 
681
681
  Is the value a Uint8Array.
682
682
 
@@ -690,7 +690,7 @@ The value to test.
690
690
 
691
691
  #### Returns
692
692
 
693
- `value is Uint8Array`
693
+ `value is Uint8Array<ArrayBufferLike>`
694
694
 
695
695
  True if the value is a Uint8Array.
696
696
 
@@ -698,7 +698,7 @@ True if the value is a Uint8Array.
698
698
 
699
699
  ### typedArray()
700
700
 
701
- > `static` **typedArray**(`value`): value is Int8Array \| Uint8Array \| Int16Array \| Uint16Array \| Int32Array \| Uint32Array \| Float32Array \| Float64Array
701
+ > `static` **typedArray**(`value`): value is Int8Array\<ArrayBufferLike\> \| Uint8Array\<ArrayBufferLike\> \| Int16Array\<ArrayBufferLike\> \| Uint16Array\<ArrayBufferLike\> \| Int32Array\<ArrayBufferLike\> \| Uint32Array\<ArrayBufferLike\> \| Float32Array\<ArrayBufferLike\> \| Float64Array\<ArrayBufferLike\>
702
702
 
703
703
  Is the value a TypedArray.
704
704
 
@@ -712,7 +712,7 @@ The value to test.
712
712
 
713
713
  #### Returns
714
714
 
715
- value is Int8Array \| Uint8Array \| Int16Array \| Uint16Array \| Int32Array \| Uint32Array \| Float32Array \| Float64Array
715
+ value is Int8Array\<ArrayBufferLike\> \| Uint8Array\<ArrayBufferLike\> \| Int16Array\<ArrayBufferLike\> \| Uint16Array\<ArrayBufferLike\> \| Int32Array\<ArrayBufferLike\> \| Uint32Array\<ArrayBufferLike\> \| Float32Array\<ArrayBufferLike\> \| Float64Array\<ArrayBufferLike\>
716
716
 
717
717
  True if the value is a TypedArray.
718
718
 
@@ -101,6 +101,10 @@ The second object.
101
101
 
102
102
  The updated object.
103
103
 
104
+ #### Throws
105
+
106
+ GeneralError if the patch fails.
107
+
104
108
  ***
105
109
 
106
110
  ### stringifyEx()
@@ -62,7 +62,7 @@ Convert a bytes to an object.
62
62
 
63
63
  The bytes to convert to an object.
64
64
 
65
- `undefined` | `null` | `Uint8Array`
65
+ `undefined` | `null` | `Uint8Array`\<`ArrayBufferLike`\>
66
66
 
67
67
  #### Returns
68
68
 
@@ -436,13 +436,13 @@ The object to remove the empty properties from.
436
436
 
437
437
  The options for the removal.
438
438
 
439
- ###### removeUndefined
439
+ ###### removeUndefined?
440
440
 
441
441
  `boolean`
442
442
 
443
443
  Remove undefined properties, defaults to true.
444
444
 
445
- ###### removeNull
445
+ ###### removeNull?
446
446
 
447
447
  `boolean`
448
448
 
@@ -24,7 +24,7 @@ Concatenate multiple arrays.
24
24
 
25
25
  ##### arrays
26
26
 
27
- `Uint8Array`[]
27
+ `Uint8Array`\<`ArrayBufferLike`\>[]
28
28
 
29
29
  The array to concatenate.
30
30
 
@@ -130,19 +130,19 @@ Optional i18n resource of the field name to display in the message.
130
130
 
131
131
  Additional options for the validation.
132
132
 
133
- ###### format
133
+ ###### format?
134
134
 
135
135
  `RegExp` \| `"base64"` \| `"base58"` \| `"hex"`
136
136
 
137
137
  Specific format to check.
138
138
 
139
- ###### minLength
139
+ ###### minLength?
140
140
 
141
141
  `number`
142
142
 
143
143
  The minimum length of the string.
144
144
 
145
- ###### maxLength
145
+ ###### maxLength?
146
146
 
147
147
  `number`
148
148
 
@@ -192,13 +192,13 @@ Optional i18n resource of the field name to display in the message.
192
192
 
193
193
  Additional options for the validation.
194
194
 
195
- ###### minLength
195
+ ###### minLength?
196
196
 
197
197
  `number`
198
198
 
199
199
  The minimum length of the string.
200
200
 
201
- ###### maxLength
201
+ ###### maxLength?
202
202
 
203
203
  `number`
204
204
 
@@ -248,13 +248,13 @@ Optional i18n resource of the field name to display in the message.
248
248
 
249
249
  Additional options for the validation.
250
250
 
251
- ###### minValue
251
+ ###### minValue?
252
252
 
253
253
  `number`
254
254
 
255
255
  The minimum value of the number.
256
256
 
257
- ###### maxValue
257
+ ###### maxValue?
258
258
 
259
259
  `number`
260
260
 
@@ -304,13 +304,13 @@ Optional i18n resource of the field name to display in the message.
304
304
 
305
305
  Additional options for the validation.
306
306
 
307
- ###### minValue
307
+ ###### minValue?
308
308
 
309
309
  `number`
310
310
 
311
311
  The minimum value of the integer.
312
312
 
313
- ###### maxValue
313
+ ###### maxValue?
314
314
 
315
315
  `number`
316
316
 
@@ -360,13 +360,13 @@ Optional i18n resource of the field name to display in the message.
360
360
 
361
361
  Additional options for the validation.
362
362
 
363
- ###### minValue
363
+ ###### minValue?
364
364
 
365
365
  `bigint`
366
366
 
367
367
  The minimum value of the bigint.
368
368
 
369
- ###### maxValue
369
+ ###### maxValue?
370
370
 
371
371
  `bigint`
372
372
 
@@ -844,7 +844,7 @@ True if the value is one of the items in the options.
844
844
 
845
845
  ### uint8Array()
846
846
 
847
- > `static` **uint8Array**(`property`, `value`, `failures`, `fieldNameResource`?): `value is Uint8Array`
847
+ > `static` **uint8Array**(`property`, `value`, `failures`, `fieldNameResource`?): `value is Uint8Array<ArrayBufferLike>`
848
848
 
849
849
  Is the property a Uint8Array.
850
850
 
@@ -876,7 +876,7 @@ Optional i18n resource of the field name to display in the message.
876
876
 
877
877
  #### Returns
878
878
 
879
- `value is Uint8Array`
879
+ `value is Uint8Array<ArrayBufferLike>`
880
880
 
881
881
  True if the value is a Uint8Array.
882
882
 
package/locales/en.json CHANGED
@@ -95,6 +95,9 @@
95
95
  },
96
96
  "base58": {
97
97
  "invalidCharacter": "Data contains a character \"{invalidCharacter}\" which is not in the charset"
98
+ },
99
+ "jsonHelper": {
100
+ "failedPatch": "Failed to patch the JSON object, patch index \"{index}\" failed"
98
101
  }
99
102
  },
100
103
  "errorNames": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.47",
3
+ "version": "0.0.1-next.49",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@twin.org/nameof": "next",
18
- "intl-messageformat": "10.7.14",
18
+ "intl-messageformat": "10.7.15",
19
19
  "rfc6902": "5.1.2"
20
20
  },
21
21
  "main": "./dist/cjs/index.cjs",