@twin.org/core 0.0.1-next.28 → 0.0.1-next.29

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.
@@ -2101,76 +2101,6 @@ class ArrayHelper {
2101
2101
  }
2102
2102
  }
2103
2103
 
2104
- // Copyright 2024 IOTA Stiftung.
2105
- // SPDX-License-Identifier: Apache-2.0.
2106
- /**
2107
- * Helpers methods for JSON objects.
2108
- */
2109
- class JsonHelper {
2110
- /**
2111
- * Serializes in canonical format.
2112
- * Based on https://www.rfc-editor.org/rfc/rfc8785.
2113
- * @param object The object to be serialized.
2114
- * @returns The serialized object.
2115
- */
2116
- static canonicalize(object) {
2117
- const buffer = [];
2118
- if (object === null ||
2119
- typeof object !== "object" ||
2120
- ("toJSON" in object && object.toJSON instanceof Function)) {
2121
- // Primitive data type
2122
- buffer.push(JSON.stringify(object));
2123
- }
2124
- else if (Array.isArray(object)) {
2125
- // Array maintain element order
2126
- const parts = [];
2127
- for (const element of object) {
2128
- if (element === undefined) {
2129
- parts.push("null");
2130
- }
2131
- else {
2132
- parts.push(JsonHelper.canonicalize(element));
2133
- }
2134
- }
2135
- buffer.push(`[${parts.join(",")}]`);
2136
- }
2137
- else {
2138
- // Object sort properties
2139
- const props = [];
2140
- const keys = Object.keys(object).sort();
2141
- const o = object;
2142
- for (const key of keys) {
2143
- if (o[key] !== undefined) {
2144
- props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
2145
- }
2146
- }
2147
- buffer.push(`{${props.join(",")}}`);
2148
- }
2149
- return buffer.join("");
2150
- }
2151
- /**
2152
- * Creates a RFC 6902 diff set.
2153
- * Based on https://www.rfc-editor.org/rfc/rfc6902.
2154
- * @param object1 The first object.
2155
- * @param object2 The second object.
2156
- * @returns The list of patches.
2157
- */
2158
- static diff(object1, object2) {
2159
- const operations = rfc6902.createPatch(object1, object2);
2160
- return operations;
2161
- }
2162
- /**
2163
- * Applies a RFC 6902 diff set to an object.
2164
- * Based on https://www.rfc-editor.org/rfc/rfc6902.
2165
- * @param object The object to patch.
2166
- * @param patches The second object.
2167
- * @returns The updated object.
2168
- */
2169
- static patch(object, patches) {
2170
- return rfc6902.applyPatch(object, patches);
2171
- }
2172
- }
2173
-
2174
2104
  // Copyright 2024 IOTA Stiftung.
2175
2105
  // SPDX-License-Identifier: Apache-2.0.
2176
2106
  /* eslint-disable no-bitwise */
@@ -2423,6 +2353,149 @@ class Converter {
2423
2353
  }
2424
2354
  }
2425
2355
 
2356
+ // Copyright 2024 IOTA Stiftung.
2357
+ // SPDX-License-Identifier: Apache-2.0.
2358
+ /**
2359
+ * Helpers methods for JSON objects.
2360
+ */
2361
+ class JsonHelper {
2362
+ /**
2363
+ * Serializes in canonical format.
2364
+ * Based on https://www.rfc-editor.org/rfc/rfc8785.
2365
+ * @param object The object to be serialized.
2366
+ * @returns The serialized object.
2367
+ */
2368
+ static canonicalize(object) {
2369
+ const buffer = [];
2370
+ if (object === null ||
2371
+ typeof object !== "object" ||
2372
+ ("toJSON" in object && object.toJSON instanceof Function)) {
2373
+ // Primitive data type
2374
+ buffer.push(JSON.stringify(object));
2375
+ }
2376
+ else if (Array.isArray(object)) {
2377
+ // Array maintain element order
2378
+ const parts = [];
2379
+ for (const element of object) {
2380
+ if (element === undefined) {
2381
+ parts.push("null");
2382
+ }
2383
+ else {
2384
+ parts.push(JsonHelper.canonicalize(element));
2385
+ }
2386
+ }
2387
+ buffer.push(`[${parts.join(",")}]`);
2388
+ }
2389
+ else {
2390
+ // Object sort properties
2391
+ const props = [];
2392
+ const keys = Object.keys(object).sort();
2393
+ const o = object;
2394
+ for (const key of keys) {
2395
+ if (o[key] !== undefined) {
2396
+ props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
2397
+ }
2398
+ }
2399
+ buffer.push(`{${props.join(",")}}`);
2400
+ }
2401
+ return buffer.join("");
2402
+ }
2403
+ /**
2404
+ * Creates a RFC 6902 diff set.
2405
+ * Based on https://www.rfc-editor.org/rfc/rfc6902.
2406
+ * @param object1 The first object.
2407
+ * @param object2 The second object.
2408
+ * @returns The list of patches.
2409
+ */
2410
+ static diff(object1, object2) {
2411
+ const operations = rfc6902.createPatch(object1, object2);
2412
+ return operations;
2413
+ }
2414
+ /**
2415
+ * Applies a RFC 6902 diff set to an object.
2416
+ * Based on https://www.rfc-editor.org/rfc/rfc6902.
2417
+ * @param object The object to patch.
2418
+ * @param patches The second object.
2419
+ * @returns The updated object.
2420
+ */
2421
+ static patch(object, patches) {
2422
+ return rfc6902.applyPatch(object, patches);
2423
+ }
2424
+ /**
2425
+ * Stringify the JSON with support for extended data types date/bigint/uint8array.
2426
+ * @param object The object to stringify.
2427
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
2428
+ * @returns The stringified object.
2429
+ */
2430
+ static stringifyEx(object, space) {
2431
+ // We want to keep the 'this' intact for the replacer
2432
+ // eslint-disable-next-line @typescript-eslint/unbound-method
2433
+ return JSON.stringify(object, JsonHelper.stringifyExReplacer, space);
2434
+ }
2435
+ /**
2436
+ * Parse the JSON string with support for extended data types date/bigint/uint8array.
2437
+ * @param json The object to pause.
2438
+ * @returns The object.
2439
+ */
2440
+ static parseEx(json) {
2441
+ // We want to keep the 'this' intact for the reviver
2442
+ // eslint-disable-next-line @typescript-eslint/unbound-method
2443
+ return JSON.parse(json, JsonHelper.parseExReviver);
2444
+ }
2445
+ /**
2446
+ * Replacer function to handle extended data types.
2447
+ * @param this The object.
2448
+ * @param key The key.
2449
+ * @param value The value.
2450
+ * @returns The value.
2451
+ */
2452
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2453
+ static stringifyExReplacer(key, value) {
2454
+ const rawValue = this[key];
2455
+ if (Is.bigint(rawValue)) {
2456
+ return {
2457
+ "@ext": "bigint",
2458
+ value: rawValue.toString()
2459
+ };
2460
+ }
2461
+ else if (Is.date(rawValue)) {
2462
+ return {
2463
+ "@ext": "date",
2464
+ value: rawValue.getTime()
2465
+ };
2466
+ }
2467
+ else if (Is.uint8Array(rawValue)) {
2468
+ return {
2469
+ "@ext": "uint8array",
2470
+ value: Converter.bytesToBase64(rawValue)
2471
+ };
2472
+ }
2473
+ return value;
2474
+ }
2475
+ /**
2476
+ * Reviver function to handle extended data types.
2477
+ * @param this The object.
2478
+ * @param key The key.
2479
+ * @param value The value.
2480
+ * @returns The value.
2481
+ */
2482
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2483
+ static parseExReviver(key, value) {
2484
+ if (Is.object(value)) {
2485
+ if (value["@ext"] === "bigint") {
2486
+ return BigInt(value.value);
2487
+ }
2488
+ else if (value["@ext"] === "date") {
2489
+ return new Date(value.value);
2490
+ }
2491
+ else if (value["@ext"] === "uint8array") {
2492
+ return Converter.base64ToBytes(value.value);
2493
+ }
2494
+ }
2495
+ return value;
2496
+ }
2497
+ }
2498
+
2426
2499
  /**
2427
2500
  * Class to help with objects.
2428
2501
  */
@@ -2099,76 +2099,6 @@ class ArrayHelper {
2099
2099
  }
2100
2100
  }
2101
2101
 
2102
- // Copyright 2024 IOTA Stiftung.
2103
- // SPDX-License-Identifier: Apache-2.0.
2104
- /**
2105
- * Helpers methods for JSON objects.
2106
- */
2107
- class JsonHelper {
2108
- /**
2109
- * Serializes in canonical format.
2110
- * Based on https://www.rfc-editor.org/rfc/rfc8785.
2111
- * @param object The object to be serialized.
2112
- * @returns The serialized object.
2113
- */
2114
- static canonicalize(object) {
2115
- const buffer = [];
2116
- if (object === null ||
2117
- typeof object !== "object" ||
2118
- ("toJSON" in object && object.toJSON instanceof Function)) {
2119
- // Primitive data type
2120
- buffer.push(JSON.stringify(object));
2121
- }
2122
- else if (Array.isArray(object)) {
2123
- // Array maintain element order
2124
- const parts = [];
2125
- for (const element of object) {
2126
- if (element === undefined) {
2127
- parts.push("null");
2128
- }
2129
- else {
2130
- parts.push(JsonHelper.canonicalize(element));
2131
- }
2132
- }
2133
- buffer.push(`[${parts.join(",")}]`);
2134
- }
2135
- else {
2136
- // Object sort properties
2137
- const props = [];
2138
- const keys = Object.keys(object).sort();
2139
- const o = object;
2140
- for (const key of keys) {
2141
- if (o[key] !== undefined) {
2142
- props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
2143
- }
2144
- }
2145
- buffer.push(`{${props.join(",")}}`);
2146
- }
2147
- return buffer.join("");
2148
- }
2149
- /**
2150
- * Creates a RFC 6902 diff set.
2151
- * Based on https://www.rfc-editor.org/rfc/rfc6902.
2152
- * @param object1 The first object.
2153
- * @param object2 The second object.
2154
- * @returns The list of patches.
2155
- */
2156
- static diff(object1, object2) {
2157
- const operations = createPatch(object1, object2);
2158
- return operations;
2159
- }
2160
- /**
2161
- * Applies a RFC 6902 diff set to an object.
2162
- * Based on https://www.rfc-editor.org/rfc/rfc6902.
2163
- * @param object The object to patch.
2164
- * @param patches The second object.
2165
- * @returns The updated object.
2166
- */
2167
- static patch(object, patches) {
2168
- return applyPatch(object, patches);
2169
- }
2170
- }
2171
-
2172
2102
  // Copyright 2024 IOTA Stiftung.
2173
2103
  // SPDX-License-Identifier: Apache-2.0.
2174
2104
  /* eslint-disable no-bitwise */
@@ -2421,6 +2351,149 @@ class Converter {
2421
2351
  }
2422
2352
  }
2423
2353
 
2354
+ // Copyright 2024 IOTA Stiftung.
2355
+ // SPDX-License-Identifier: Apache-2.0.
2356
+ /**
2357
+ * Helpers methods for JSON objects.
2358
+ */
2359
+ class JsonHelper {
2360
+ /**
2361
+ * Serializes in canonical format.
2362
+ * Based on https://www.rfc-editor.org/rfc/rfc8785.
2363
+ * @param object The object to be serialized.
2364
+ * @returns The serialized object.
2365
+ */
2366
+ static canonicalize(object) {
2367
+ const buffer = [];
2368
+ if (object === null ||
2369
+ typeof object !== "object" ||
2370
+ ("toJSON" in object && object.toJSON instanceof Function)) {
2371
+ // Primitive data type
2372
+ buffer.push(JSON.stringify(object));
2373
+ }
2374
+ else if (Array.isArray(object)) {
2375
+ // Array maintain element order
2376
+ const parts = [];
2377
+ for (const element of object) {
2378
+ if (element === undefined) {
2379
+ parts.push("null");
2380
+ }
2381
+ else {
2382
+ parts.push(JsonHelper.canonicalize(element));
2383
+ }
2384
+ }
2385
+ buffer.push(`[${parts.join(",")}]`);
2386
+ }
2387
+ else {
2388
+ // Object sort properties
2389
+ const props = [];
2390
+ const keys = Object.keys(object).sort();
2391
+ const o = object;
2392
+ for (const key of keys) {
2393
+ if (o[key] !== undefined) {
2394
+ props.push(`${JSON.stringify(key)}:${JsonHelper.canonicalize(o[key])}`);
2395
+ }
2396
+ }
2397
+ buffer.push(`{${props.join(",")}}`);
2398
+ }
2399
+ return buffer.join("");
2400
+ }
2401
+ /**
2402
+ * Creates a RFC 6902 diff set.
2403
+ * Based on https://www.rfc-editor.org/rfc/rfc6902.
2404
+ * @param object1 The first object.
2405
+ * @param object2 The second object.
2406
+ * @returns The list of patches.
2407
+ */
2408
+ static diff(object1, object2) {
2409
+ const operations = createPatch(object1, object2);
2410
+ return operations;
2411
+ }
2412
+ /**
2413
+ * Applies a RFC 6902 diff set to an object.
2414
+ * Based on https://www.rfc-editor.org/rfc/rfc6902.
2415
+ * @param object The object to patch.
2416
+ * @param patches The second object.
2417
+ * @returns The updated object.
2418
+ */
2419
+ static patch(object, patches) {
2420
+ return applyPatch(object, patches);
2421
+ }
2422
+ /**
2423
+ * Stringify the JSON with support for extended data types date/bigint/uint8array.
2424
+ * @param object The object to stringify.
2425
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
2426
+ * @returns The stringified object.
2427
+ */
2428
+ static stringifyEx(object, space) {
2429
+ // We want to keep the 'this' intact for the replacer
2430
+ // eslint-disable-next-line @typescript-eslint/unbound-method
2431
+ return JSON.stringify(object, JsonHelper.stringifyExReplacer, space);
2432
+ }
2433
+ /**
2434
+ * Parse the JSON string with support for extended data types date/bigint/uint8array.
2435
+ * @param json The object to pause.
2436
+ * @returns The object.
2437
+ */
2438
+ static parseEx(json) {
2439
+ // We want to keep the 'this' intact for the reviver
2440
+ // eslint-disable-next-line @typescript-eslint/unbound-method
2441
+ return JSON.parse(json, JsonHelper.parseExReviver);
2442
+ }
2443
+ /**
2444
+ * Replacer function to handle extended data types.
2445
+ * @param this The object.
2446
+ * @param key The key.
2447
+ * @param value The value.
2448
+ * @returns The value.
2449
+ */
2450
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2451
+ static stringifyExReplacer(key, value) {
2452
+ const rawValue = this[key];
2453
+ if (Is.bigint(rawValue)) {
2454
+ return {
2455
+ "@ext": "bigint",
2456
+ value: rawValue.toString()
2457
+ };
2458
+ }
2459
+ else if (Is.date(rawValue)) {
2460
+ return {
2461
+ "@ext": "date",
2462
+ value: rawValue.getTime()
2463
+ };
2464
+ }
2465
+ else if (Is.uint8Array(rawValue)) {
2466
+ return {
2467
+ "@ext": "uint8array",
2468
+ value: Converter.bytesToBase64(rawValue)
2469
+ };
2470
+ }
2471
+ return value;
2472
+ }
2473
+ /**
2474
+ * Reviver function to handle extended data types.
2475
+ * @param this The object.
2476
+ * @param key The key.
2477
+ * @param value The value.
2478
+ * @returns The value.
2479
+ */
2480
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2481
+ static parseExReviver(key, value) {
2482
+ if (Is.object(value)) {
2483
+ if (value["@ext"] === "bigint") {
2484
+ return BigInt(value.value);
2485
+ }
2486
+ else if (value["@ext"] === "date") {
2487
+ return new Date(value.value);
2488
+ }
2489
+ else if (value["@ext"] === "uint8array") {
2490
+ return Converter.base64ToBytes(value.value);
2491
+ }
2492
+ }
2493
+ return value;
2494
+ }
2495
+ }
2496
+
2424
2497
  /**
2425
2498
  * Class to help with objects.
2426
2499
  */
@@ -26,4 +26,33 @@ export declare class JsonHelper {
26
26
  * @returns The updated object.
27
27
  */
28
28
  static patch<T = unknown>(object: T, patches: IPatchOperation[]): T;
29
+ /**
30
+ * Stringify the JSON with support for extended data types date/bigint/uint8array.
31
+ * @param object The object to stringify.
32
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
33
+ * @returns The stringified object.
34
+ */
35
+ static stringifyEx(object: unknown, space?: string | number): string;
36
+ /**
37
+ * Parse the JSON string with support for extended data types date/bigint/uint8array.
38
+ * @param json The object to pause.
39
+ * @returns The object.
40
+ */
41
+ static parseEx(json: string): unknown;
42
+ /**
43
+ * Replacer function to handle extended data types.
44
+ * @param this The object.
45
+ * @param key The key.
46
+ * @param value The value.
47
+ * @returns The value.
48
+ */
49
+ static stringifyExReplacer(this: any, key: string, value: unknown): unknown;
50
+ /**
51
+ * Reviver function to handle extended data types.
52
+ * @param this The object.
53
+ * @param key The key.
54
+ * @param value The value.
55
+ * @returns The value.
56
+ */
57
+ static parseExReviver(this: any, key: string, value: unknown): unknown;
29
58
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.28
3
+ ## 0.0.1-next.29
4
4
 
5
5
  - Initial Release
@@ -100,3 +100,121 @@ The second object.
100
100
  `T`
101
101
 
102
102
  The updated object.
103
+
104
+ ***
105
+
106
+ ### stringifyEx()
107
+
108
+ > `static` **stringifyEx**(`object`, `space`?): `string`
109
+
110
+ Stringify the JSON with support for extended data types date/bigint/uint8array.
111
+
112
+ #### Parameters
113
+
114
+ ##### object
115
+
116
+ `unknown`
117
+
118
+ The object to stringify.
119
+
120
+ ##### space?
121
+
122
+ Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
123
+
124
+ `string` | `number`
125
+
126
+ #### Returns
127
+
128
+ `string`
129
+
130
+ The stringified object.
131
+
132
+ ***
133
+
134
+ ### parseEx()
135
+
136
+ > `static` **parseEx**(`json`): `unknown`
137
+
138
+ Parse the JSON string with support for extended data types date/bigint/uint8array.
139
+
140
+ #### Parameters
141
+
142
+ ##### json
143
+
144
+ `string`
145
+
146
+ The object to pause.
147
+
148
+ #### Returns
149
+
150
+ `unknown`
151
+
152
+ The object.
153
+
154
+ ***
155
+
156
+ ### stringifyExReplacer()
157
+
158
+ > `static` **stringifyExReplacer**(`this`, `key`, `value`): `unknown`
159
+
160
+ Replacer function to handle extended data types.
161
+
162
+ #### Parameters
163
+
164
+ ##### this
165
+
166
+ `any`
167
+
168
+ The object.
169
+
170
+ ##### key
171
+
172
+ `string`
173
+
174
+ The key.
175
+
176
+ ##### value
177
+
178
+ `unknown`
179
+
180
+ The value.
181
+
182
+ #### Returns
183
+
184
+ `unknown`
185
+
186
+ The value.
187
+
188
+ ***
189
+
190
+ ### parseExReviver()
191
+
192
+ > `static` **parseExReviver**(`this`, `key`, `value`): `unknown`
193
+
194
+ Reviver function to handle extended data types.
195
+
196
+ #### Parameters
197
+
198
+ ##### this
199
+
200
+ `any`
201
+
202
+ The object.
203
+
204
+ ##### key
205
+
206
+ `string`
207
+
208
+ The key.
209
+
210
+ ##### value
211
+
212
+ `unknown`
213
+
214
+ The value.
215
+
216
+ #### Returns
217
+
218
+ `unknown`
219
+
220
+ The value.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.28",
3
+ "version": "0.0.1-next.29",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",