@twin.org/core 0.0.1-next.25 → 0.0.1-next.27

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.
@@ -2525,7 +2525,7 @@ class ObjectHelper {
2525
2525
  let pathValue = obj;
2526
2526
  for (const pathPart of pathParts) {
2527
2527
  // Is the path part numeric i.e. an array index.
2528
- const arrayMatch = /(\d+)/.exec(pathPart);
2528
+ const arrayMatch = /^(\d+)$/.exec(pathPart);
2529
2529
  if (arrayMatch) {
2530
2530
  const arrayIndex = Number.parseInt(arrayMatch[1], 10);
2531
2531
  if (Is.arrayValue(pathValue) && arrayIndex < pathValue.length) {
@@ -2561,7 +2561,7 @@ class ObjectHelper {
2561
2561
  for (let i = 0; i < pathParts.length; i++) {
2562
2562
  const pathPart = pathParts[i];
2563
2563
  // Is the path part numeric i.e. an array index.
2564
- const arrayMatch = /(\d+)/.exec(pathPart);
2564
+ const arrayMatch = /^(\d+)$/.exec(pathPart);
2565
2565
  const arrayIndex = arrayMatch ? Number.parseInt(arrayMatch[1], 10) : -1;
2566
2566
  if (i === pathParts.length - 1) {
2567
2567
  // Last part of path so set the value
@@ -2592,7 +2592,7 @@ class ObjectHelper {
2592
2592
  pathValue = pathValue[arrayIndex];
2593
2593
  }
2594
2594
  if (Is.empty(pathValue)) {
2595
- const nextArrayMatch = /(\d+)/.exec(pathParts[i + 1]);
2595
+ const nextArrayMatch = /^(\d+)$/.exec(pathParts[i + 1]);
2596
2596
  const nextArrayIndex = nextArrayMatch ? Number.parseInt(nextArrayMatch[1], 10) : -1;
2597
2597
  if (nextArrayIndex >= 0) {
2598
2598
  pathValue = [];
@@ -3009,6 +3009,18 @@ class Coerce {
3009
3009
  return value.getTime();
3010
3010
  }
3011
3011
  }
3012
+ /**
3013
+ * Coerce the value to an integer.
3014
+ * @param value The value to coerce.
3015
+ * @throws TypeError If the value can not be coerced.
3016
+ * @returns The value if it can be coerced.
3017
+ */
3018
+ static integer(value) {
3019
+ const num = Coerce.number(value);
3020
+ if (!Is.undefined(num)) {
3021
+ return Math.trunc(num);
3022
+ }
3023
+ }
3012
3024
  /**
3013
3025
  * Coerce the value to a bigint.
3014
3026
  * @param value The value to coerce.
@@ -3155,6 +3167,25 @@ class Coerce {
3155
3167
  catch { }
3156
3168
  }
3157
3169
  }
3170
+ /**
3171
+ * Coerce the value to a Uint8Array.
3172
+ * @param value The value to coerce.
3173
+ * @throws TypeError If the value can not be coerced.
3174
+ * @returns The value if it can be coerced.
3175
+ */
3176
+ static uint8Array(value) {
3177
+ if (Is.undefined(value)) {
3178
+ return value;
3179
+ }
3180
+ if (Is.string(value)) {
3181
+ if (Is.stringHex(value.toLowerCase(), true)) {
3182
+ return Converter.hexToBytes(value.toLowerCase());
3183
+ }
3184
+ if (Is.stringBase64(value)) {
3185
+ return Converter.base64ToBytes(value);
3186
+ }
3187
+ }
3188
+ }
3158
3189
  }
3159
3190
 
3160
3191
  // Copyright 2024 IOTA Stiftung.
@@ -2523,7 +2523,7 @@ class ObjectHelper {
2523
2523
  let pathValue = obj;
2524
2524
  for (const pathPart of pathParts) {
2525
2525
  // Is the path part numeric i.e. an array index.
2526
- const arrayMatch = /(\d+)/.exec(pathPart);
2526
+ const arrayMatch = /^(\d+)$/.exec(pathPart);
2527
2527
  if (arrayMatch) {
2528
2528
  const arrayIndex = Number.parseInt(arrayMatch[1], 10);
2529
2529
  if (Is.arrayValue(pathValue) && arrayIndex < pathValue.length) {
@@ -2559,7 +2559,7 @@ class ObjectHelper {
2559
2559
  for (let i = 0; i < pathParts.length; i++) {
2560
2560
  const pathPart = pathParts[i];
2561
2561
  // Is the path part numeric i.e. an array index.
2562
- const arrayMatch = /(\d+)/.exec(pathPart);
2562
+ const arrayMatch = /^(\d+)$/.exec(pathPart);
2563
2563
  const arrayIndex = arrayMatch ? Number.parseInt(arrayMatch[1], 10) : -1;
2564
2564
  if (i === pathParts.length - 1) {
2565
2565
  // Last part of path so set the value
@@ -2590,7 +2590,7 @@ class ObjectHelper {
2590
2590
  pathValue = pathValue[arrayIndex];
2591
2591
  }
2592
2592
  if (Is.empty(pathValue)) {
2593
- const nextArrayMatch = /(\d+)/.exec(pathParts[i + 1]);
2593
+ const nextArrayMatch = /^(\d+)$/.exec(pathParts[i + 1]);
2594
2594
  const nextArrayIndex = nextArrayMatch ? Number.parseInt(nextArrayMatch[1], 10) : -1;
2595
2595
  if (nextArrayIndex >= 0) {
2596
2596
  pathValue = [];
@@ -3007,6 +3007,18 @@ class Coerce {
3007
3007
  return value.getTime();
3008
3008
  }
3009
3009
  }
3010
+ /**
3011
+ * Coerce the value to an integer.
3012
+ * @param value The value to coerce.
3013
+ * @throws TypeError If the value can not be coerced.
3014
+ * @returns The value if it can be coerced.
3015
+ */
3016
+ static integer(value) {
3017
+ const num = Coerce.number(value);
3018
+ if (!Is.undefined(num)) {
3019
+ return Math.trunc(num);
3020
+ }
3021
+ }
3010
3022
  /**
3011
3023
  * Coerce the value to a bigint.
3012
3024
  * @param value The value to coerce.
@@ -3153,6 +3165,25 @@ class Coerce {
3153
3165
  catch { }
3154
3166
  }
3155
3167
  }
3168
+ /**
3169
+ * Coerce the value to a Uint8Array.
3170
+ * @param value The value to coerce.
3171
+ * @throws TypeError If the value can not be coerced.
3172
+ * @returns The value if it can be coerced.
3173
+ */
3174
+ static uint8Array(value) {
3175
+ if (Is.undefined(value)) {
3176
+ return value;
3177
+ }
3178
+ if (Is.string(value)) {
3179
+ if (Is.stringHex(value.toLowerCase(), true)) {
3180
+ return Converter.hexToBytes(value.toLowerCase());
3181
+ }
3182
+ if (Is.stringBase64(value)) {
3183
+ return Converter.base64ToBytes(value);
3184
+ }
3185
+ }
3186
+ }
3156
3187
  }
3157
3188
 
3158
3189
  // Copyright 2024 IOTA Stiftung.
@@ -16,6 +16,13 @@ export declare class Coerce {
16
16
  * @returns The value if it can be coerced.
17
17
  */
18
18
  static number(value: unknown): number | undefined;
19
+ /**
20
+ * Coerce the value to an integer.
21
+ * @param value The value to coerce.
22
+ * @throws TypeError If the value can not be coerced.
23
+ * @returns The value if it can be coerced.
24
+ */
25
+ static integer(value: unknown): number | undefined;
19
26
  /**
20
27
  * Coerce the value to a bigint.
21
28
  * @param value The value to coerce.
@@ -58,4 +65,11 @@ export declare class Coerce {
58
65
  * @returns The value if it can be coerced.
59
66
  */
60
67
  static object<T = unknown>(value: unknown): T | undefined;
68
+ /**
69
+ * Coerce the value to a Uint8Array.
70
+ * @param value The value to coerce.
71
+ * @throws TypeError If the value can not be coerced.
72
+ * @returns The value if it can be coerced.
73
+ */
74
+ static uint8Array(value: unknown): Uint8Array | undefined;
61
75
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.25
3
+ ## 0.0.1-next.27
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,29 @@ 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.25",
3
+ "version": "0.0.1-next.27",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",