@twin.org/core 0.0.1-next.27 → 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.
- package/dist/cjs/index.cjs +225 -70
- package/dist/esm/index.mjs +225 -71
- package/dist/types/helpers/jsonHelper.d.ts +29 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/models/coerceType.d.ts +49 -0
- package/dist/types/utils/coerce.d.ts +8 -0
- package/docs/changelog.md +1 -1
- package/docs/reference/classes/Coerce.md +28 -0
- package/docs/reference/classes/JsonHelper.md +118 -0
- package/docs/reference/index.md +2 -0
- package/docs/reference/type-aliases/CoerceType.md +5 -0
- package/docs/reference/variables/CoerceType.md +67 -0
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -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
|
*/
|
|
@@ -2954,6 +3027,55 @@ class ErrorHelper {
|
|
|
2954
3027
|
}
|
|
2955
3028
|
}
|
|
2956
3029
|
|
|
3030
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3031
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3032
|
+
/**
|
|
3033
|
+
* The types the extracted data can be coerced to.
|
|
3034
|
+
*/
|
|
3035
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3036
|
+
const CoerceType = {
|
|
3037
|
+
/**
|
|
3038
|
+
* String.
|
|
3039
|
+
*/
|
|
3040
|
+
String: "string",
|
|
3041
|
+
/**
|
|
3042
|
+
* Number.
|
|
3043
|
+
*/
|
|
3044
|
+
Number: "number",
|
|
3045
|
+
/**
|
|
3046
|
+
* Integer.
|
|
3047
|
+
*/
|
|
3048
|
+
Integer: "integer",
|
|
3049
|
+
/**
|
|
3050
|
+
* Boolean.
|
|
3051
|
+
*/
|
|
3052
|
+
Boolean: "boolean",
|
|
3053
|
+
/**
|
|
3054
|
+
* Big Integer.
|
|
3055
|
+
*/
|
|
3056
|
+
BigInt: "bigint",
|
|
3057
|
+
/**
|
|
3058
|
+
* Date.
|
|
3059
|
+
*/
|
|
3060
|
+
Date: "date",
|
|
3061
|
+
/**
|
|
3062
|
+
* Date Time.
|
|
3063
|
+
*/
|
|
3064
|
+
DateTime: "datetime",
|
|
3065
|
+
/**
|
|
3066
|
+
* Time.
|
|
3067
|
+
*/
|
|
3068
|
+
Time: "time",
|
|
3069
|
+
/**
|
|
3070
|
+
* Object.
|
|
3071
|
+
*/
|
|
3072
|
+
Object: "object",
|
|
3073
|
+
/**
|
|
3074
|
+
* Uint8Array.
|
|
3075
|
+
*/
|
|
3076
|
+
Uint8Array: "uint8array"
|
|
3077
|
+
};
|
|
3078
|
+
|
|
2957
3079
|
// Copyright 2024 IOTA Stiftung.
|
|
2958
3080
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2959
3081
|
/**
|
|
@@ -3186,6 +3308,38 @@ class Coerce {
|
|
|
3186
3308
|
}
|
|
3187
3309
|
}
|
|
3188
3310
|
}
|
|
3311
|
+
/**
|
|
3312
|
+
* Coerces a value based on the coercion type.
|
|
3313
|
+
* @param value The value to coerce.
|
|
3314
|
+
* @param type The coercion type to perform.
|
|
3315
|
+
* @returns The coerced value.
|
|
3316
|
+
*/
|
|
3317
|
+
static byType(value, type) {
|
|
3318
|
+
switch (type) {
|
|
3319
|
+
case CoerceType.String:
|
|
3320
|
+
return Coerce.string(value);
|
|
3321
|
+
case CoerceType.Number:
|
|
3322
|
+
return Coerce.number(value);
|
|
3323
|
+
case CoerceType.Integer:
|
|
3324
|
+
return Coerce.integer(value);
|
|
3325
|
+
case CoerceType.BigInt:
|
|
3326
|
+
return Coerce.bigint(value);
|
|
3327
|
+
case CoerceType.Boolean:
|
|
3328
|
+
return Coerce.boolean(value);
|
|
3329
|
+
case CoerceType.Date:
|
|
3330
|
+
return Coerce.date(value);
|
|
3331
|
+
case CoerceType.DateTime:
|
|
3332
|
+
return Coerce.dateTime(value);
|
|
3333
|
+
case CoerceType.Time:
|
|
3334
|
+
return Coerce.time(value);
|
|
3335
|
+
case CoerceType.Object:
|
|
3336
|
+
return Coerce.object(value);
|
|
3337
|
+
case CoerceType.Uint8Array:
|
|
3338
|
+
return Coerce.uint8Array(value);
|
|
3339
|
+
default:
|
|
3340
|
+
return value;
|
|
3341
|
+
}
|
|
3342
|
+
}
|
|
3189
3343
|
}
|
|
3190
3344
|
|
|
3191
3345
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -4600,6 +4754,7 @@ exports.Base64Url = Base64Url;
|
|
|
4600
4754
|
exports.BaseError = BaseError;
|
|
4601
4755
|
exports.BitString = BitString;
|
|
4602
4756
|
exports.Coerce = Coerce;
|
|
4757
|
+
exports.CoerceType = CoerceType;
|
|
4603
4758
|
exports.ComponentFactory = ComponentFactory;
|
|
4604
4759
|
exports.Compression = Compression;
|
|
4605
4760
|
exports.CompressionType = CompressionType;
|
package/dist/esm/index.mjs
CHANGED
|
@@ -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
|
*/
|
|
@@ -2952,6 +3025,55 @@ class ErrorHelper {
|
|
|
2952
3025
|
}
|
|
2953
3026
|
}
|
|
2954
3027
|
|
|
3028
|
+
// Copyright 2024 IOTA Stiftung.
|
|
3029
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
3030
|
+
/**
|
|
3031
|
+
* The types the extracted data can be coerced to.
|
|
3032
|
+
*/
|
|
3033
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
3034
|
+
const CoerceType = {
|
|
3035
|
+
/**
|
|
3036
|
+
* String.
|
|
3037
|
+
*/
|
|
3038
|
+
String: "string",
|
|
3039
|
+
/**
|
|
3040
|
+
* Number.
|
|
3041
|
+
*/
|
|
3042
|
+
Number: "number",
|
|
3043
|
+
/**
|
|
3044
|
+
* Integer.
|
|
3045
|
+
*/
|
|
3046
|
+
Integer: "integer",
|
|
3047
|
+
/**
|
|
3048
|
+
* Boolean.
|
|
3049
|
+
*/
|
|
3050
|
+
Boolean: "boolean",
|
|
3051
|
+
/**
|
|
3052
|
+
* Big Integer.
|
|
3053
|
+
*/
|
|
3054
|
+
BigInt: "bigint",
|
|
3055
|
+
/**
|
|
3056
|
+
* Date.
|
|
3057
|
+
*/
|
|
3058
|
+
Date: "date",
|
|
3059
|
+
/**
|
|
3060
|
+
* Date Time.
|
|
3061
|
+
*/
|
|
3062
|
+
DateTime: "datetime",
|
|
3063
|
+
/**
|
|
3064
|
+
* Time.
|
|
3065
|
+
*/
|
|
3066
|
+
Time: "time",
|
|
3067
|
+
/**
|
|
3068
|
+
* Object.
|
|
3069
|
+
*/
|
|
3070
|
+
Object: "object",
|
|
3071
|
+
/**
|
|
3072
|
+
* Uint8Array.
|
|
3073
|
+
*/
|
|
3074
|
+
Uint8Array: "uint8array"
|
|
3075
|
+
};
|
|
3076
|
+
|
|
2955
3077
|
// Copyright 2024 IOTA Stiftung.
|
|
2956
3078
|
// SPDX-License-Identifier: Apache-2.0.
|
|
2957
3079
|
/**
|
|
@@ -3184,6 +3306,38 @@ class Coerce {
|
|
|
3184
3306
|
}
|
|
3185
3307
|
}
|
|
3186
3308
|
}
|
|
3309
|
+
/**
|
|
3310
|
+
* Coerces a value based on the coercion type.
|
|
3311
|
+
* @param value The value to coerce.
|
|
3312
|
+
* @param type The coercion type to perform.
|
|
3313
|
+
* @returns The coerced value.
|
|
3314
|
+
*/
|
|
3315
|
+
static byType(value, type) {
|
|
3316
|
+
switch (type) {
|
|
3317
|
+
case CoerceType.String:
|
|
3318
|
+
return Coerce.string(value);
|
|
3319
|
+
case CoerceType.Number:
|
|
3320
|
+
return Coerce.number(value);
|
|
3321
|
+
case CoerceType.Integer:
|
|
3322
|
+
return Coerce.integer(value);
|
|
3323
|
+
case CoerceType.BigInt:
|
|
3324
|
+
return Coerce.bigint(value);
|
|
3325
|
+
case CoerceType.Boolean:
|
|
3326
|
+
return Coerce.boolean(value);
|
|
3327
|
+
case CoerceType.Date:
|
|
3328
|
+
return Coerce.date(value);
|
|
3329
|
+
case CoerceType.DateTime:
|
|
3330
|
+
return Coerce.dateTime(value);
|
|
3331
|
+
case CoerceType.Time:
|
|
3332
|
+
return Coerce.time(value);
|
|
3333
|
+
case CoerceType.Object:
|
|
3334
|
+
return Coerce.object(value);
|
|
3335
|
+
case CoerceType.Uint8Array:
|
|
3336
|
+
return Coerce.uint8Array(value);
|
|
3337
|
+
default:
|
|
3338
|
+
return value;
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3187
3341
|
}
|
|
3188
3342
|
|
|
3189
3343
|
// Copyright 2024 IOTA Stiftung.
|
|
@@ -4588,4 +4742,4 @@ class Validation {
|
|
|
4588
4742
|
}
|
|
4589
4743
|
}
|
|
4590
4744
|
|
|
4591
|
-
export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base58, Base64, Base64Url, BaseError, BitString, Coerce, ComponentFactory, Compression, CompressionType, ConflictError, Converter, EnvHelper, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
|
|
4745
|
+
export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base58, Base64, Base64Url, BaseError, BitString, Coerce, CoerceType, ComponentFactory, Compression, CompressionType, ConflictError, Converter, EnvHelper, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
|
|
@@ -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/dist/types/index.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export * from "./helpers/jsonHelper";
|
|
|
24
24
|
export * from "./helpers/objectHelper";
|
|
25
25
|
export * from "./helpers/randomHelper";
|
|
26
26
|
export * from "./helpers/stringHelper";
|
|
27
|
+
export * from "./models/coerceType";
|
|
27
28
|
export * from "./models/compressionType";
|
|
28
29
|
export * from "./models/IComponent";
|
|
29
30
|
export * from "./models/IError";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The types the extracted data can be coerced to.
|
|
3
|
+
*/
|
|
4
|
+
export declare const CoerceType: {
|
|
5
|
+
/**
|
|
6
|
+
* String.
|
|
7
|
+
*/
|
|
8
|
+
readonly String: "string";
|
|
9
|
+
/**
|
|
10
|
+
* Number.
|
|
11
|
+
*/
|
|
12
|
+
readonly Number: "number";
|
|
13
|
+
/**
|
|
14
|
+
* Integer.
|
|
15
|
+
*/
|
|
16
|
+
readonly Integer: "integer";
|
|
17
|
+
/**
|
|
18
|
+
* Boolean.
|
|
19
|
+
*/
|
|
20
|
+
readonly Boolean: "boolean";
|
|
21
|
+
/**
|
|
22
|
+
* Big Integer.
|
|
23
|
+
*/
|
|
24
|
+
readonly BigInt: "bigint";
|
|
25
|
+
/**
|
|
26
|
+
* Date.
|
|
27
|
+
*/
|
|
28
|
+
readonly Date: "date";
|
|
29
|
+
/**
|
|
30
|
+
* Date Time.
|
|
31
|
+
*/
|
|
32
|
+
readonly DateTime: "datetime";
|
|
33
|
+
/**
|
|
34
|
+
* Time.
|
|
35
|
+
*/
|
|
36
|
+
readonly Time: "time";
|
|
37
|
+
/**
|
|
38
|
+
* Object.
|
|
39
|
+
*/
|
|
40
|
+
readonly Object: "object";
|
|
41
|
+
/**
|
|
42
|
+
* Uint8Array.
|
|
43
|
+
*/
|
|
44
|
+
readonly Uint8Array: "uint8array";
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The types the extracted data can be coerced to.
|
|
48
|
+
*/
|
|
49
|
+
export type CoerceType = (typeof CoerceType)[keyof typeof CoerceType];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CoerceType } from "../models/coerceType";
|
|
1
2
|
/**
|
|
2
3
|
* Coerce an object from one type to another.
|
|
3
4
|
*/
|
|
@@ -72,4 +73,11 @@ export declare class Coerce {
|
|
|
72
73
|
* @returns The value if it can be coerced.
|
|
73
74
|
*/
|
|
74
75
|
static uint8Array(value: unknown): Uint8Array | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Coerces a value based on the coercion type.
|
|
78
|
+
* @param value The value to coerce.
|
|
79
|
+
* @param type The coercion type to perform.
|
|
80
|
+
* @returns The coerced value.
|
|
81
|
+
*/
|
|
82
|
+
static byType(value: unknown, type?: CoerceType): unknown;
|
|
75
83
|
}
|
package/docs/changelog.md
CHANGED
|
@@ -275,3 +275,31 @@ The value if it can be coerced.
|
|
|
275
275
|
#### Throws
|
|
276
276
|
|
|
277
277
|
TypeError If the value can not be coerced.
|
|
278
|
+
|
|
279
|
+
***
|
|
280
|
+
|
|
281
|
+
### byType()
|
|
282
|
+
|
|
283
|
+
> `static` **byType**(`value`, `type`?): `unknown`
|
|
284
|
+
|
|
285
|
+
Coerces a value based on the coercion type.
|
|
286
|
+
|
|
287
|
+
#### Parameters
|
|
288
|
+
|
|
289
|
+
##### value
|
|
290
|
+
|
|
291
|
+
`unknown`
|
|
292
|
+
|
|
293
|
+
The value to coerce.
|
|
294
|
+
|
|
295
|
+
##### type?
|
|
296
|
+
|
|
297
|
+
[`CoerceType`](../type-aliases/CoerceType.md)
|
|
298
|
+
|
|
299
|
+
The coercion type to perform.
|
|
300
|
+
|
|
301
|
+
#### Returns
|
|
302
|
+
|
|
303
|
+
`unknown`
|
|
304
|
+
|
|
305
|
+
The coerced value.
|
|
@@ -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/docs/reference/index.md
CHANGED
|
@@ -54,9 +54,11 @@
|
|
|
54
54
|
|
|
55
55
|
## Type Aliases
|
|
56
56
|
|
|
57
|
+
- [CoerceType](type-aliases/CoerceType.md)
|
|
57
58
|
- [CompressionType](type-aliases/CompressionType.md)
|
|
58
59
|
|
|
59
60
|
## Variables
|
|
60
61
|
|
|
61
62
|
- [ComponentFactory](variables/ComponentFactory.md)
|
|
63
|
+
- [CoerceType](variables/CoerceType.md)
|
|
62
64
|
- [CompressionType](variables/CompressionType.md)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Variable: CoerceType
|
|
2
|
+
|
|
3
|
+
> `const` **CoerceType**: `object`
|
|
4
|
+
|
|
5
|
+
The types the extracted data can be coerced to.
|
|
6
|
+
|
|
7
|
+
## Type declaration
|
|
8
|
+
|
|
9
|
+
### String
|
|
10
|
+
|
|
11
|
+
> `readonly` **String**: `"string"` = `"string"`
|
|
12
|
+
|
|
13
|
+
String.
|
|
14
|
+
|
|
15
|
+
### Number
|
|
16
|
+
|
|
17
|
+
> `readonly` **Number**: `"number"` = `"number"`
|
|
18
|
+
|
|
19
|
+
Number.
|
|
20
|
+
|
|
21
|
+
### Integer
|
|
22
|
+
|
|
23
|
+
> `readonly` **Integer**: `"integer"` = `"integer"`
|
|
24
|
+
|
|
25
|
+
Integer.
|
|
26
|
+
|
|
27
|
+
### Boolean
|
|
28
|
+
|
|
29
|
+
> `readonly` **Boolean**: `"boolean"` = `"boolean"`
|
|
30
|
+
|
|
31
|
+
Boolean.
|
|
32
|
+
|
|
33
|
+
### BigInt
|
|
34
|
+
|
|
35
|
+
> `readonly` **BigInt**: `"bigint"` = `"bigint"`
|
|
36
|
+
|
|
37
|
+
Big Integer.
|
|
38
|
+
|
|
39
|
+
### Date
|
|
40
|
+
|
|
41
|
+
> `readonly` **Date**: `"date"` = `"date"`
|
|
42
|
+
|
|
43
|
+
Date.
|
|
44
|
+
|
|
45
|
+
### DateTime
|
|
46
|
+
|
|
47
|
+
> `readonly` **DateTime**: `"datetime"` = `"datetime"`
|
|
48
|
+
|
|
49
|
+
Date Time.
|
|
50
|
+
|
|
51
|
+
### Time
|
|
52
|
+
|
|
53
|
+
> `readonly` **Time**: `"time"` = `"time"`
|
|
54
|
+
|
|
55
|
+
Time.
|
|
56
|
+
|
|
57
|
+
### Object
|
|
58
|
+
|
|
59
|
+
> `readonly` **Object**: `"object"` = `"object"`
|
|
60
|
+
|
|
61
|
+
Object.
|
|
62
|
+
|
|
63
|
+
### Uint8Array
|
|
64
|
+
|
|
65
|
+
> `readonly` **Uint8Array**: `"uint8array"` = `"uint8array"`
|
|
66
|
+
|
|
67
|
+
Uint8Array.
|