@twin.org/core 0.0.1-next.55 → 0.0.1-next.57

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.
@@ -1494,6 +1494,49 @@ class ValidationError extends BaseError {
1494
1494
  }
1495
1495
  }
1496
1496
 
1497
+ /**
1498
+ * Class to help with arrays.
1499
+ */
1500
+ class ArrayHelper {
1501
+ /**
1502
+ * Do the two arrays match.
1503
+ * @param arr1 The first array.
1504
+ * @param arr2 The second array.
1505
+ * @returns True if both arrays are empty of have the same values.
1506
+ */
1507
+ static matches(arr1, arr2) {
1508
+ if (Is.empty(arr1) && Is.empty(arr2)) {
1509
+ return true;
1510
+ }
1511
+ if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
1512
+ return false;
1513
+ }
1514
+ if (arr1.length !== arr2.length) {
1515
+ return false;
1516
+ }
1517
+ for (let i = 0; i < arr1.length; i++) {
1518
+ if (arr1[i] !== arr2[i]) {
1519
+ return false;
1520
+ }
1521
+ }
1522
+ return true;
1523
+ }
1524
+ /**
1525
+ * Convert an object or array to an array.
1526
+ * @param value The object or array to convert.
1527
+ * @returns The array.
1528
+ */
1529
+ static fromObjectOrArray(value) {
1530
+ if (Is.empty(value)) {
1531
+ return undefined;
1532
+ }
1533
+ if (Is.array(value)) {
1534
+ return value;
1535
+ }
1536
+ return [value];
1537
+ }
1538
+ }
1539
+
1497
1540
  // Copyright 2024 IOTA Stiftung.
1498
1541
  // SPDX-License-Identifier: Apache-2.0.
1499
1542
  /**
@@ -1776,6 +1819,50 @@ class Guards {
1776
1819
  throw new GuardError(source, "guard.arrayOneOf", property, value, options.join(", "));
1777
1820
  }
1778
1821
  }
1822
+ /**
1823
+ * Does the array start with the specified data.
1824
+ * @param source The source of the error.
1825
+ * @param property The name of the property.
1826
+ * @param value The value to test.
1827
+ * @param startValues The values that must start the array.
1828
+ * @throws GuardError If the value does not match the assertion.
1829
+ */
1830
+ static arrayStartsWith(source, property, value, startValues) {
1831
+ if (!Is.arrayValue(value)) {
1832
+ throw new GuardError(source, "guard.array", property, value);
1833
+ }
1834
+ const startValuesArray = ArrayHelper.fromObjectOrArray(startValues);
1835
+ if (!Is.arrayValue(startValuesArray)) {
1836
+ throw new GuardError(source, "guard.array", property, startValuesArray);
1837
+ }
1838
+ for (let i = 0; i < startValuesArray.length; i++) {
1839
+ if (value[i] !== startValuesArray[i]) {
1840
+ throw new GuardError(source, "guard.arrayStartsWith", property, value, startValuesArray.join(", "));
1841
+ }
1842
+ }
1843
+ }
1844
+ /**
1845
+ * Does the array end with the specified data.
1846
+ * @param source The source of the error.
1847
+ * @param property The name of the property.
1848
+ * @param value The value to test.
1849
+ * @param endValues The values that must end the array.
1850
+ * @throws GuardError If the value does not match the assertion.
1851
+ */
1852
+ static arrayEndsWith(source, property, value, endValues) {
1853
+ if (!Is.arrayValue(value)) {
1854
+ throw new GuardError(source, "guard.array", property, value);
1855
+ }
1856
+ const endValuesArray = ArrayHelper.fromObjectOrArray(endValues);
1857
+ if (!Is.arrayValue(endValuesArray)) {
1858
+ throw new GuardError(source, "guard.array", property, endValuesArray);
1859
+ }
1860
+ for (let i = 0; i < endValuesArray.length; i++) {
1861
+ if (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {
1862
+ throw new GuardError(source, "guard.arrayEndsWith", property, value, endValuesArray.join(", "));
1863
+ }
1864
+ }
1865
+ }
1779
1866
  /**
1780
1867
  * Is the property a Uint8Array.
1781
1868
  * @param source The source of the error.
@@ -2120,37 +2207,6 @@ class Factory {
2120
2207
  // eslint-disable-next-line @typescript-eslint/naming-convention
2121
2208
  const ComponentFactory = Factory.createFactory("component");
2122
2209
 
2123
- // Copyright 2024 IOTA Stiftung.
2124
- // SPDX-License-Identifier: Apache-2.0.
2125
- /**
2126
- * Class to help with arrays.
2127
- */
2128
- class ArrayHelper {
2129
- /**
2130
- * Do the two arrays match.
2131
- * @param arr1 The first array.
2132
- * @param arr2 The second array.
2133
- * @returns True if both arrays are empty of have the same values.
2134
- */
2135
- static matches(arr1, arr2) {
2136
- if (Is.empty(arr1) && Is.empty(arr2)) {
2137
- return true;
2138
- }
2139
- if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
2140
- return false;
2141
- }
2142
- if (arr1.length !== arr2.length) {
2143
- return false;
2144
- }
2145
- for (let i = 0; i < arr1.length; i++) {
2146
- if (arr1[i] !== arr2[i]) {
2147
- return false;
2148
- }
2149
- }
2150
- return true;
2151
- }
2152
- }
2153
-
2154
2210
  // Copyright 2024 IOTA Stiftung.
2155
2211
  // SPDX-License-Identifier: Apache-2.0.
2156
2212
  /* eslint-disable no-bitwise */
@@ -1492,6 +1492,49 @@ class ValidationError extends BaseError {
1492
1492
  }
1493
1493
  }
1494
1494
 
1495
+ /**
1496
+ * Class to help with arrays.
1497
+ */
1498
+ class ArrayHelper {
1499
+ /**
1500
+ * Do the two arrays match.
1501
+ * @param arr1 The first array.
1502
+ * @param arr2 The second array.
1503
+ * @returns True if both arrays are empty of have the same values.
1504
+ */
1505
+ static matches(arr1, arr2) {
1506
+ if (Is.empty(arr1) && Is.empty(arr2)) {
1507
+ return true;
1508
+ }
1509
+ if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
1510
+ return false;
1511
+ }
1512
+ if (arr1.length !== arr2.length) {
1513
+ return false;
1514
+ }
1515
+ for (let i = 0; i < arr1.length; i++) {
1516
+ if (arr1[i] !== arr2[i]) {
1517
+ return false;
1518
+ }
1519
+ }
1520
+ return true;
1521
+ }
1522
+ /**
1523
+ * Convert an object or array to an array.
1524
+ * @param value The object or array to convert.
1525
+ * @returns The array.
1526
+ */
1527
+ static fromObjectOrArray(value) {
1528
+ if (Is.empty(value)) {
1529
+ return undefined;
1530
+ }
1531
+ if (Is.array(value)) {
1532
+ return value;
1533
+ }
1534
+ return [value];
1535
+ }
1536
+ }
1537
+
1495
1538
  // Copyright 2024 IOTA Stiftung.
1496
1539
  // SPDX-License-Identifier: Apache-2.0.
1497
1540
  /**
@@ -1774,6 +1817,50 @@ class Guards {
1774
1817
  throw new GuardError(source, "guard.arrayOneOf", property, value, options.join(", "));
1775
1818
  }
1776
1819
  }
1820
+ /**
1821
+ * Does the array start with the specified data.
1822
+ * @param source The source of the error.
1823
+ * @param property The name of the property.
1824
+ * @param value The value to test.
1825
+ * @param startValues The values that must start the array.
1826
+ * @throws GuardError If the value does not match the assertion.
1827
+ */
1828
+ static arrayStartsWith(source, property, value, startValues) {
1829
+ if (!Is.arrayValue(value)) {
1830
+ throw new GuardError(source, "guard.array", property, value);
1831
+ }
1832
+ const startValuesArray = ArrayHelper.fromObjectOrArray(startValues);
1833
+ if (!Is.arrayValue(startValuesArray)) {
1834
+ throw new GuardError(source, "guard.array", property, startValuesArray);
1835
+ }
1836
+ for (let i = 0; i < startValuesArray.length; i++) {
1837
+ if (value[i] !== startValuesArray[i]) {
1838
+ throw new GuardError(source, "guard.arrayStartsWith", property, value, startValuesArray.join(", "));
1839
+ }
1840
+ }
1841
+ }
1842
+ /**
1843
+ * Does the array end with the specified data.
1844
+ * @param source The source of the error.
1845
+ * @param property The name of the property.
1846
+ * @param value The value to test.
1847
+ * @param endValues The values that must end the array.
1848
+ * @throws GuardError If the value does not match the assertion.
1849
+ */
1850
+ static arrayEndsWith(source, property, value, endValues) {
1851
+ if (!Is.arrayValue(value)) {
1852
+ throw new GuardError(source, "guard.array", property, value);
1853
+ }
1854
+ const endValuesArray = ArrayHelper.fromObjectOrArray(endValues);
1855
+ if (!Is.arrayValue(endValuesArray)) {
1856
+ throw new GuardError(source, "guard.array", property, endValuesArray);
1857
+ }
1858
+ for (let i = 0; i < endValuesArray.length; i++) {
1859
+ if (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {
1860
+ throw new GuardError(source, "guard.arrayEndsWith", property, value, endValuesArray.join(", "));
1861
+ }
1862
+ }
1863
+ }
1777
1864
  /**
1778
1865
  * Is the property a Uint8Array.
1779
1866
  * @param source The source of the error.
@@ -2118,37 +2205,6 @@ class Factory {
2118
2205
  // eslint-disable-next-line @typescript-eslint/naming-convention
2119
2206
  const ComponentFactory = Factory.createFactory("component");
2120
2207
 
2121
- // Copyright 2024 IOTA Stiftung.
2122
- // SPDX-License-Identifier: Apache-2.0.
2123
- /**
2124
- * Class to help with arrays.
2125
- */
2126
- class ArrayHelper {
2127
- /**
2128
- * Do the two arrays match.
2129
- * @param arr1 The first array.
2130
- * @param arr2 The second array.
2131
- * @returns True if both arrays are empty of have the same values.
2132
- */
2133
- static matches(arr1, arr2) {
2134
- if (Is.empty(arr1) && Is.empty(arr2)) {
2135
- return true;
2136
- }
2137
- if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
2138
- return false;
2139
- }
2140
- if (arr1.length !== arr2.length) {
2141
- return false;
2142
- }
2143
- for (let i = 0; i < arr1.length; i++) {
2144
- if (arr1[i] !== arr2[i]) {
2145
- return false;
2146
- }
2147
- }
2148
- return true;
2149
- }
2150
- }
2151
-
2152
2208
  // Copyright 2024 IOTA Stiftung.
2153
2209
  // SPDX-License-Identifier: Apache-2.0.
2154
2210
  /* eslint-disable no-bitwise */
@@ -1,3 +1,4 @@
1
+ import type { ObjectOrArray } from "../models/objectOrArray";
1
2
  /**
2
3
  * Class to help with arrays.
3
4
  */
@@ -9,4 +10,16 @@ export declare class ArrayHelper {
9
10
  * @returns True if both arrays are empty of have the same values.
10
11
  */
11
12
  static matches(arr1: unknown, arr2: unknown): boolean;
13
+ /**
14
+ * Convert an object or array to an array.
15
+ * @param value The object or array to convert.
16
+ * @returns The array.
17
+ */
18
+ static fromObjectOrArray<T = unknown>(value: undefined): undefined;
19
+ /**
20
+ * Convert an object or array to an array.
21
+ * @param value The object or array to convert.
22
+ * @returns The array.
23
+ */
24
+ static fromObjectOrArray<T = unknown>(value: ObjectOrArray<T>): T[];
12
25
  }
@@ -38,6 +38,7 @@ export * from "./models/ILocalesIndex";
38
38
  export * from "./models/IPatchOperation";
39
39
  export * from "./models/IUrlParts";
40
40
  export * from "./models/IValidationFailure";
41
+ export * from "./models/objectOrArray";
41
42
  export * from "./types/bitString";
42
43
  export * from "./types/url";
43
44
  export * from "./types/urn";
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Object or array data type
3
+ */
4
+ export type ObjectOrArray<T = unknown> = T | T[];
@@ -1,3 +1,4 @@
1
+ import type { ObjectOrArray } from "../models/objectOrArray";
1
2
  /**
2
3
  * Class to handle guard operations for parameters.
3
4
  */
@@ -178,6 +179,24 @@ export declare class Guards {
178
179
  * @throws GuardError If the value does not match the assertion.
179
180
  */
180
181
  static arrayOneOf<T>(source: string, property: string, value: T, options: T[]): asserts value is T;
182
+ /**
183
+ * Does the array start with the specified data.
184
+ * @param source The source of the error.
185
+ * @param property The name of the property.
186
+ * @param value The value to test.
187
+ * @param startValues The values that must start the array.
188
+ * @throws GuardError If the value does not match the assertion.
189
+ */
190
+ static arrayStartsWith<T>(source: string, property: string, value: unknown, startValues: ObjectOrArray<T>): asserts value is T[];
191
+ /**
192
+ * Does the array end with the specified data.
193
+ * @param source The source of the error.
194
+ * @param property The name of the property.
195
+ * @param value The value to test.
196
+ * @param endValues The values that must end the array.
197
+ * @throws GuardError If the value does not match the assertion.
198
+ */
199
+ static arrayEndsWith<T>(source: string, property: string, value: unknown, endValues: ObjectOrArray<T>): asserts value is T[];
181
200
  /**
182
201
  * Is the property a Uint8Array.
183
202
  * @param source The source of the error.
package/docs/changelog.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
+ ## [0.0.1-next.57](https://github.com/twinfoundation/framework/compare/core-v0.0.1-next.56...core-v0.0.1-next.57) (2025-06-10)
4
+
5
+
6
+ ### Features
7
+
8
+ * add guards arrayEndsWith and arrayStartsWith ([95d875e](https://github.com/twinfoundation/framework/commit/95d875ec8ccb4713c145fdde941d4cfedcec2ed3))
9
+
10
+ ## [0.0.1-next.56](https://github.com/twinfoundation/framework/compare/core-v0.0.1-next.55...core-v0.0.1-next.56) (2025-05-08)
11
+
12
+
13
+ ### Features
14
+
15
+ * add ObjectOrArray and ArrayHelper methods ([0ac9077](https://github.com/twinfoundation/framework/commit/0ac907764d64b38ad1b04b0e9c3027055b527559))
16
+
3
17
  ## [0.0.1-next.55](https://github.com/twinfoundation/framework/compare/core-v0.0.1-next.54...core-v0.0.1-next.55) (2025-05-07)
4
18
 
5
19
 
@@ -39,3 +39,73 @@ The second array.
39
39
  `boolean`
40
40
 
41
41
  True if both arrays are empty of have the same values.
42
+
43
+ ***
44
+
45
+ ### fromObjectOrArray()
46
+
47
+ Convert an object or array to an array.
48
+
49
+ #### Param
50
+
51
+ The object or array to convert.
52
+
53
+ #### Call Signature
54
+
55
+ > `static` **fromObjectOrArray**\<`T`\>(`value`): `undefined`
56
+
57
+ Convert an object or array to an array.
58
+
59
+ ##### Type Parameters
60
+
61
+ ###### T
62
+
63
+ `T` = `unknown`
64
+
65
+ ##### Parameters
66
+
67
+ ###### value
68
+
69
+ `undefined`
70
+
71
+ The object or array to convert.
72
+
73
+ ##### Returns
74
+
75
+ `undefined`
76
+
77
+ The array.
78
+
79
+ ##### Param
80
+
81
+ The object or array to convert.
82
+
83
+ #### Call Signature
84
+
85
+ > `static` **fromObjectOrArray**\<`T`\>(`value`): `T`[]
86
+
87
+ Convert an object or array to an array.
88
+
89
+ ##### Type Parameters
90
+
91
+ ###### T
92
+
93
+ `T` = `unknown`
94
+
95
+ ##### Parameters
96
+
97
+ ###### value
98
+
99
+ [`ObjectOrArray`](../type-aliases/ObjectOrArray.md)\<`T`\>
100
+
101
+ The object or array to convert.
102
+
103
+ ##### Returns
104
+
105
+ `T`[]
106
+
107
+ The array.
108
+
109
+ ##### Param
110
+
111
+ The object or array to convert.
@@ -824,6 +824,102 @@ GuardError If the value does not match the assertion.
824
824
 
825
825
  ***
826
826
 
827
+ ### arrayStartsWith()
828
+
829
+ > `static` **arrayStartsWith**\<`T`\>(`source`, `property`, `value`, `startValues`): `asserts value is T[]`
830
+
831
+ Does the array start with the specified data.
832
+
833
+ #### Type Parameters
834
+
835
+ ##### T
836
+
837
+ `T`
838
+
839
+ #### Parameters
840
+
841
+ ##### source
842
+
843
+ `string`
844
+
845
+ The source of the error.
846
+
847
+ ##### property
848
+
849
+ `string`
850
+
851
+ The name of the property.
852
+
853
+ ##### value
854
+
855
+ `unknown`
856
+
857
+ The value to test.
858
+
859
+ ##### startValues
860
+
861
+ [`ObjectOrArray`](../type-aliases/ObjectOrArray.md)\<`T`\>
862
+
863
+ The values that must start the array.
864
+
865
+ #### Returns
866
+
867
+ `asserts value is T[]`
868
+
869
+ #### Throws
870
+
871
+ GuardError If the value does not match the assertion.
872
+
873
+ ***
874
+
875
+ ### arrayEndsWith()
876
+
877
+ > `static` **arrayEndsWith**\<`T`\>(`source`, `property`, `value`, `endValues`): `asserts value is T[]`
878
+
879
+ Does the array end with the specified data.
880
+
881
+ #### Type Parameters
882
+
883
+ ##### T
884
+
885
+ `T`
886
+
887
+ #### Parameters
888
+
889
+ ##### source
890
+
891
+ `string`
892
+
893
+ The source of the error.
894
+
895
+ ##### property
896
+
897
+ `string`
898
+
899
+ The name of the property.
900
+
901
+ ##### value
902
+
903
+ `unknown`
904
+
905
+ The value to test.
906
+
907
+ ##### endValues
908
+
909
+ [`ObjectOrArray`](../type-aliases/ObjectOrArray.md)\<`T`\>
910
+
911
+ The values that must end the array.
912
+
913
+ #### Returns
914
+
915
+ `asserts value is T[]`
916
+
917
+ #### Throws
918
+
919
+ GuardError If the value does not match the assertion.
920
+
921
+ ***
922
+
827
923
  ### uint8Array()
828
924
 
829
925
  > `static` **uint8Array**(`source`, `property`, `value`): `asserts value is Uint8Array<ArrayBufferLike>`
@@ -708,7 +708,7 @@ True if the value is a Uint8Array.
708
708
 
709
709
  ### typedArray()
710
710
 
711
- > `static` **typedArray**(`value`): value is Int8Array\<ArrayBufferLike\> \| Uint8Array\<ArrayBufferLike\> \| Int16Array\<ArrayBufferLike\> \| Uint16Array\<ArrayBufferLike\> \| Int32Array\<ArrayBufferLike\> \| Uint32Array\<ArrayBufferLike\> \| Float32Array\<ArrayBufferLike\> \| Float64Array\<ArrayBufferLike\>
711
+ > `static` **typedArray**(`value`): value is Uint8Array\<ArrayBufferLike\> \| Int8Array\<ArrayBufferLike\> \| Uint16Array\<ArrayBufferLike\> \| Int16Array\<ArrayBufferLike\> \| Uint32Array\<ArrayBufferLike\> \| Int32Array\<ArrayBufferLike\> \| Float32Array\<ArrayBufferLike\> \| Float64Array\<ArrayBufferLike\>
712
712
 
713
713
  Is the value a TypedArray.
714
714
 
@@ -722,7 +722,7 @@ The value to test.
722
722
 
723
723
  #### Returns
724
724
 
725
- value is Int8Array\<ArrayBufferLike\> \| Uint8Array\<ArrayBufferLike\> \| Int16Array\<ArrayBufferLike\> \| Uint16Array\<ArrayBufferLike\> \| Int32Array\<ArrayBufferLike\> \| Uint32Array\<ArrayBufferLike\> \| Float32Array\<ArrayBufferLike\> \| Float64Array\<ArrayBufferLike\>
725
+ value is Uint8Array\<ArrayBufferLike\> \| Int8Array\<ArrayBufferLike\> \| Uint16Array\<ArrayBufferLike\> \| Int16Array\<ArrayBufferLike\> \| Uint32Array\<ArrayBufferLike\> \| Int32Array\<ArrayBufferLike\> \| Float32Array\<ArrayBufferLike\> \| Float64Array\<ArrayBufferLike\>
726
726
 
727
727
  True if the value is a TypedArray.
728
728
 
@@ -59,6 +59,7 @@
59
59
 
60
60
  - [CoerceType](type-aliases/CoerceType.md)
61
61
  - [CompressionType](type-aliases/CompressionType.md)
62
+ - [ObjectOrArray](type-aliases/ObjectOrArray.md)
62
63
 
63
64
  ## Variables
64
65
 
@@ -6,7 +6,7 @@ Interface describing a patch operation to add a property.
6
6
 
7
7
  ### op
8
8
 
9
- > **op**: `"replace"` \| `"add"` \| `"remove"` \| `"copy"` \| `"move"` \| `"test"`
9
+ > **op**: `"add"` \| `"remove"` \| `"replace"` \| `"move"` \| `"copy"` \| `"test"`
10
10
 
11
11
  The operation that was performed on the item.
12
12
 
@@ -0,0 +1,11 @@
1
+ # Type Alias: ObjectOrArray\<T\>
2
+
3
+ > **ObjectOrArray**\<`T`\> = `T` \| `T`[]
4
+
5
+ Object or array data type
6
+
7
+ ## Type Parameters
8
+
9
+ ### T
10
+
11
+ `T` = `unknown`
package/locales/en.json CHANGED
@@ -65,6 +65,8 @@
65
65
  "array": "Property \"{property}\" must be an array, it is \"{value}\"",
66
66
  "arrayValue": "Property \"{property}\" must be an array with at least one item",
67
67
  "arrayOneOf": "Property \"{property}\" must be one of [{options}], it is \"{value}\"",
68
+ "arrayStartsWith": "Property \"{property}\" must be an array starting with [{startValues}], it is \"{value}\"",
69
+ "arrayEndsWith": "Property \"{property}\" must be an array ending with [{endValues}], it is \"{value}\"",
68
70
  "uint8Array": "Property \"{property}\" must be a Uint8Array, it is \"{value}\"",
69
71
  "function": "Property \"{property}\" must be a function, it is \"{value}\"",
70
72
  "urn": "Property \"{property}\" must be a Urn formatted string, it is \"{value}\"",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.55",
3
+ "version": "0.0.1-next.57",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",