@twin.org/core 0.0.1-next.56 → 0.0.1-next.58

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,49 +2207,6 @@ class Factory {
2120
2207
  // eslint-disable-next-line @typescript-eslint/naming-convention
2121
2208
  const ComponentFactory = Factory.createFactory("component");
2122
2209
 
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
- * Convert an object or array to an array.
2152
- * @param value The object or array to convert.
2153
- * @returns The array.
2154
- */
2155
- static fromObjectOrArray(value) {
2156
- if (Is.empty(value)) {
2157
- return undefined;
2158
- }
2159
- if (Is.array(value)) {
2160
- return value;
2161
- }
2162
- return [value];
2163
- }
2164
- }
2165
-
2166
2210
  // Copyright 2024 IOTA Stiftung.
2167
2211
  // SPDX-License-Identifier: Apache-2.0.
2168
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,49 +2205,6 @@ class Factory {
2118
2205
  // eslint-disable-next-line @typescript-eslint/naming-convention
2119
2206
  const ComponentFactory = Factory.createFactory("component");
2120
2207
 
2121
- /**
2122
- * Class to help with arrays.
2123
- */
2124
- class ArrayHelper {
2125
- /**
2126
- * Do the two arrays match.
2127
- * @param arr1 The first array.
2128
- * @param arr2 The second array.
2129
- * @returns True if both arrays are empty of have the same values.
2130
- */
2131
- static matches(arr1, arr2) {
2132
- if (Is.empty(arr1) && Is.empty(arr2)) {
2133
- return true;
2134
- }
2135
- if (!((Is.array(arr1) && Is.array(arr2)) || (Is.typedArray(arr1) && Is.typedArray(arr2)))) {
2136
- return false;
2137
- }
2138
- if (arr1.length !== arr2.length) {
2139
- return false;
2140
- }
2141
- for (let i = 0; i < arr1.length; i++) {
2142
- if (arr1[i] !== arr2[i]) {
2143
- return false;
2144
- }
2145
- }
2146
- return true;
2147
- }
2148
- /**
2149
- * Convert an object or array to an array.
2150
- * @param value The object or array to convert.
2151
- * @returns The array.
2152
- */
2153
- static fromObjectOrArray(value) {
2154
- if (Is.empty(value)) {
2155
- return undefined;
2156
- }
2157
- if (Is.array(value)) {
2158
- return value;
2159
- }
2160
- return [value];
2161
- }
2162
- }
2163
-
2164
2208
  // Copyright 2024 IOTA Stiftung.
2165
2209
  // SPDX-License-Identifier: Apache-2.0.
2166
2210
  /* eslint-disable no-bitwise */
@@ -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.58](https://github.com/twinfoundation/framework/compare/core-v0.0.1-next.57...core-v0.0.1-next.58) (2025-06-13)
4
+
5
+
6
+ ### Miscellaneous Chores
7
+
8
+ * **core:** Synchronize repo versions
9
+
10
+ ## [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)
11
+
12
+
13
+ ### Features
14
+
15
+ * add guards arrayEndsWith and arrayStartsWith ([95d875e](https://github.com/twinfoundation/framework/commit/95d875ec8ccb4713c145fdde941d4cfedcec2ed3))
16
+
3
17
  ## [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)
4
18
 
5
19
 
@@ -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
 
@@ -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
 
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.56",
3
+ "version": "0.0.1-next.58",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",