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

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.
@@ -2954,6 +2954,55 @@ class ErrorHelper {
2954
2954
  }
2955
2955
  }
2956
2956
 
2957
+ // Copyright 2024 IOTA Stiftung.
2958
+ // SPDX-License-Identifier: Apache-2.0.
2959
+ /**
2960
+ * The types the extracted data can be coerced to.
2961
+ */
2962
+ // eslint-disable-next-line @typescript-eslint/naming-convention
2963
+ const CoerceType = {
2964
+ /**
2965
+ * String.
2966
+ */
2967
+ String: "string",
2968
+ /**
2969
+ * Number.
2970
+ */
2971
+ Number: "number",
2972
+ /**
2973
+ * Integer.
2974
+ */
2975
+ Integer: "integer",
2976
+ /**
2977
+ * Boolean.
2978
+ */
2979
+ Boolean: "boolean",
2980
+ /**
2981
+ * Big Integer.
2982
+ */
2983
+ BigInt: "bigint",
2984
+ /**
2985
+ * Date.
2986
+ */
2987
+ Date: "date",
2988
+ /**
2989
+ * Date Time.
2990
+ */
2991
+ DateTime: "datetime",
2992
+ /**
2993
+ * Time.
2994
+ */
2995
+ Time: "time",
2996
+ /**
2997
+ * Object.
2998
+ */
2999
+ Object: "object",
3000
+ /**
3001
+ * Uint8Array.
3002
+ */
3003
+ Uint8Array: "uint8array"
3004
+ };
3005
+
2957
3006
  // Copyright 2024 IOTA Stiftung.
2958
3007
  // SPDX-License-Identifier: Apache-2.0.
2959
3008
  /**
@@ -3009,6 +3058,18 @@ class Coerce {
3009
3058
  return value.getTime();
3010
3059
  }
3011
3060
  }
3061
+ /**
3062
+ * Coerce the value to an integer.
3063
+ * @param value The value to coerce.
3064
+ * @throws TypeError If the value can not be coerced.
3065
+ * @returns The value if it can be coerced.
3066
+ */
3067
+ static integer(value) {
3068
+ const num = Coerce.number(value);
3069
+ if (!Is.undefined(num)) {
3070
+ return Math.trunc(num);
3071
+ }
3072
+ }
3012
3073
  /**
3013
3074
  * Coerce the value to a bigint.
3014
3075
  * @param value The value to coerce.
@@ -3155,6 +3216,57 @@ class Coerce {
3155
3216
  catch { }
3156
3217
  }
3157
3218
  }
3219
+ /**
3220
+ * Coerce the value to a Uint8Array.
3221
+ * @param value The value to coerce.
3222
+ * @throws TypeError If the value can not be coerced.
3223
+ * @returns The value if it can be coerced.
3224
+ */
3225
+ static uint8Array(value) {
3226
+ if (Is.undefined(value)) {
3227
+ return value;
3228
+ }
3229
+ if (Is.string(value)) {
3230
+ if (Is.stringHex(value.toLowerCase(), true)) {
3231
+ return Converter.hexToBytes(value.toLowerCase());
3232
+ }
3233
+ if (Is.stringBase64(value)) {
3234
+ return Converter.base64ToBytes(value);
3235
+ }
3236
+ }
3237
+ }
3238
+ /**
3239
+ * Coerces a value based on the coercion type.
3240
+ * @param value The value to coerce.
3241
+ * @param type The coercion type to perform.
3242
+ * @returns The coerced value.
3243
+ */
3244
+ static byType(value, type) {
3245
+ switch (type) {
3246
+ case CoerceType.String:
3247
+ return Coerce.string(value);
3248
+ case CoerceType.Number:
3249
+ return Coerce.number(value);
3250
+ case CoerceType.Integer:
3251
+ return Coerce.integer(value);
3252
+ case CoerceType.BigInt:
3253
+ return Coerce.bigint(value);
3254
+ case CoerceType.Boolean:
3255
+ return Coerce.boolean(value);
3256
+ case CoerceType.Date:
3257
+ return Coerce.date(value);
3258
+ case CoerceType.DateTime:
3259
+ return Coerce.dateTime(value);
3260
+ case CoerceType.Time:
3261
+ return Coerce.time(value);
3262
+ case CoerceType.Object:
3263
+ return Coerce.object(value);
3264
+ case CoerceType.Uint8Array:
3265
+ return Coerce.uint8Array(value);
3266
+ default:
3267
+ return value;
3268
+ }
3269
+ }
3158
3270
  }
3159
3271
 
3160
3272
  // Copyright 2024 IOTA Stiftung.
@@ -4569,6 +4681,7 @@ exports.Base64Url = Base64Url;
4569
4681
  exports.BaseError = BaseError;
4570
4682
  exports.BitString = BitString;
4571
4683
  exports.Coerce = Coerce;
4684
+ exports.CoerceType = CoerceType;
4572
4685
  exports.ComponentFactory = ComponentFactory;
4573
4686
  exports.Compression = Compression;
4574
4687
  exports.CompressionType = CompressionType;
@@ -2952,6 +2952,55 @@ class ErrorHelper {
2952
2952
  }
2953
2953
  }
2954
2954
 
2955
+ // Copyright 2024 IOTA Stiftung.
2956
+ // SPDX-License-Identifier: Apache-2.0.
2957
+ /**
2958
+ * The types the extracted data can be coerced to.
2959
+ */
2960
+ // eslint-disable-next-line @typescript-eslint/naming-convention
2961
+ const CoerceType = {
2962
+ /**
2963
+ * String.
2964
+ */
2965
+ String: "string",
2966
+ /**
2967
+ * Number.
2968
+ */
2969
+ Number: "number",
2970
+ /**
2971
+ * Integer.
2972
+ */
2973
+ Integer: "integer",
2974
+ /**
2975
+ * Boolean.
2976
+ */
2977
+ Boolean: "boolean",
2978
+ /**
2979
+ * Big Integer.
2980
+ */
2981
+ BigInt: "bigint",
2982
+ /**
2983
+ * Date.
2984
+ */
2985
+ Date: "date",
2986
+ /**
2987
+ * Date Time.
2988
+ */
2989
+ DateTime: "datetime",
2990
+ /**
2991
+ * Time.
2992
+ */
2993
+ Time: "time",
2994
+ /**
2995
+ * Object.
2996
+ */
2997
+ Object: "object",
2998
+ /**
2999
+ * Uint8Array.
3000
+ */
3001
+ Uint8Array: "uint8array"
3002
+ };
3003
+
2955
3004
  // Copyright 2024 IOTA Stiftung.
2956
3005
  // SPDX-License-Identifier: Apache-2.0.
2957
3006
  /**
@@ -3007,6 +3056,18 @@ class Coerce {
3007
3056
  return value.getTime();
3008
3057
  }
3009
3058
  }
3059
+ /**
3060
+ * Coerce the value to an integer.
3061
+ * @param value The value to coerce.
3062
+ * @throws TypeError If the value can not be coerced.
3063
+ * @returns The value if it can be coerced.
3064
+ */
3065
+ static integer(value) {
3066
+ const num = Coerce.number(value);
3067
+ if (!Is.undefined(num)) {
3068
+ return Math.trunc(num);
3069
+ }
3070
+ }
3010
3071
  /**
3011
3072
  * Coerce the value to a bigint.
3012
3073
  * @param value The value to coerce.
@@ -3153,6 +3214,57 @@ class Coerce {
3153
3214
  catch { }
3154
3215
  }
3155
3216
  }
3217
+ /**
3218
+ * Coerce the value to a Uint8Array.
3219
+ * @param value The value to coerce.
3220
+ * @throws TypeError If the value can not be coerced.
3221
+ * @returns The value if it can be coerced.
3222
+ */
3223
+ static uint8Array(value) {
3224
+ if (Is.undefined(value)) {
3225
+ return value;
3226
+ }
3227
+ if (Is.string(value)) {
3228
+ if (Is.stringHex(value.toLowerCase(), true)) {
3229
+ return Converter.hexToBytes(value.toLowerCase());
3230
+ }
3231
+ if (Is.stringBase64(value)) {
3232
+ return Converter.base64ToBytes(value);
3233
+ }
3234
+ }
3235
+ }
3236
+ /**
3237
+ * Coerces a value based on the coercion type.
3238
+ * @param value The value to coerce.
3239
+ * @param type The coercion type to perform.
3240
+ * @returns The coerced value.
3241
+ */
3242
+ static byType(value, type) {
3243
+ switch (type) {
3244
+ case CoerceType.String:
3245
+ return Coerce.string(value);
3246
+ case CoerceType.Number:
3247
+ return Coerce.number(value);
3248
+ case CoerceType.Integer:
3249
+ return Coerce.integer(value);
3250
+ case CoerceType.BigInt:
3251
+ return Coerce.bigint(value);
3252
+ case CoerceType.Boolean:
3253
+ return Coerce.boolean(value);
3254
+ case CoerceType.Date:
3255
+ return Coerce.date(value);
3256
+ case CoerceType.DateTime:
3257
+ return Coerce.dateTime(value);
3258
+ case CoerceType.Time:
3259
+ return Coerce.time(value);
3260
+ case CoerceType.Object:
3261
+ return Coerce.object(value);
3262
+ case CoerceType.Uint8Array:
3263
+ return Coerce.uint8Array(value);
3264
+ default:
3265
+ return value;
3266
+ }
3267
+ }
3156
3268
  }
3157
3269
 
3158
3270
  // Copyright 2024 IOTA Stiftung.
@@ -4557,4 +4669,4 @@ class Validation {
4557
4669
  }
4558
4670
  }
4559
4671
 
4560
- 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 };
4672
+ 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 };
@@ -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
  */
@@ -16,6 +17,13 @@ export declare class Coerce {
16
17
  * @returns The value if it can be coerced.
17
18
  */
18
19
  static number(value: unknown): number | undefined;
20
+ /**
21
+ * Coerce the value to an integer.
22
+ * @param value The value to coerce.
23
+ * @throws TypeError If the value can not be coerced.
24
+ * @returns The value if it can be coerced.
25
+ */
26
+ static integer(value: unknown): number | undefined;
19
27
  /**
20
28
  * Coerce the value to a bigint.
21
29
  * @param value The value to coerce.
@@ -58,4 +66,18 @@ export declare class Coerce {
58
66
  * @returns The value if it can be coerced.
59
67
  */
60
68
  static object<T = unknown>(value: unknown): T | undefined;
69
+ /**
70
+ * Coerce the value to a Uint8Array.
71
+ * @param value The value to coerce.
72
+ * @throws TypeError If the value can not be coerced.
73
+ * @returns The value if it can be coerced.
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;
61
83
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.26
3
+ ## 0.0.1-next.28
4
4
 
5
5
  - Initial Release
@@ -66,6 +66,32 @@ TypeError If the value can not be coerced.
66
66
 
67
67
  ***
68
68
 
69
+ ### integer()
70
+
71
+ > `static` **integer**(`value`): `undefined` \| `number`
72
+
73
+ Coerce the value to an integer.
74
+
75
+ #### Parameters
76
+
77
+ ##### value
78
+
79
+ `unknown`
80
+
81
+ The value to coerce.
82
+
83
+ #### Returns
84
+
85
+ `undefined` \| `number`
86
+
87
+ The value if it can be coerced.
88
+
89
+ #### Throws
90
+
91
+ TypeError If the value can not be coerced.
92
+
93
+ ***
94
+
69
95
  ### bigint()
70
96
 
71
97
  > `static` **bigint**(`value`): `undefined` \| `bigint`
@@ -223,3 +249,57 @@ The value if it can be coerced.
223
249
  #### Throws
224
250
 
225
251
  TypeError If the value can not be coerced.
252
+
253
+ ***
254
+
255
+ ### uint8Array()
256
+
257
+ > `static` **uint8Array**(`value`): `undefined` \| `Uint8Array`
258
+
259
+ Coerce the value to a Uint8Array.
260
+
261
+ #### Parameters
262
+
263
+ ##### value
264
+
265
+ `unknown`
266
+
267
+ The value to coerce.
268
+
269
+ #### Returns
270
+
271
+ `undefined` \| `Uint8Array`
272
+
273
+ The value if it can be coerced.
274
+
275
+ #### Throws
276
+
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.
@@ -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,5 @@
1
+ # Type Alias: CoerceType
2
+
3
+ > **CoerceType**: *typeof* [`CoerceType`](../variables/CoerceType.md)\[keyof *typeof* [`CoerceType`](../variables/CoerceType.md)\]
4
+
5
+ The types the extracted data can be coerced to.
@@ -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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.26",
3
+ "version": "0.0.1-next.28",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",