@twin.org/core 0.0.2-next.9 → 0.0.3-next.2

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.
Files changed (187) hide show
  1. package/dist/es/encoding/base32.js +75 -0
  2. package/dist/es/encoding/base32.js.map +1 -0
  3. package/dist/es/encoding/base58.js +124 -0
  4. package/dist/es/encoding/base58.js.map +1 -0
  5. package/dist/es/encoding/base64.js +231 -0
  6. package/dist/es/encoding/base64.js.map +1 -0
  7. package/dist/es/encoding/base64Url.js +42 -0
  8. package/dist/es/encoding/base64Url.js.map +1 -0
  9. package/dist/es/errors/alreadyExistsError.js +22 -0
  10. package/dist/es/errors/alreadyExistsError.js.map +1 -0
  11. package/dist/es/errors/baseError.js +257 -0
  12. package/dist/es/errors/baseError.js.map +1 -0
  13. package/dist/es/errors/conflictError.js +23 -0
  14. package/dist/es/errors/conflictError.js.map +1 -0
  15. package/dist/es/errors/generalError.js +21 -0
  16. package/dist/es/errors/generalError.js.map +1 -0
  17. package/dist/es/errors/guardError.js +27 -0
  18. package/dist/es/errors/guardError.js.map +1 -0
  19. package/dist/es/errors/notFoundError.js +22 -0
  20. package/dist/es/errors/notFoundError.js.map +1 -0
  21. package/dist/es/errors/notImplementedError.js +24 -0
  22. package/dist/es/errors/notImplementedError.js.map +1 -0
  23. package/dist/es/errors/notSupportedError.js +21 -0
  24. package/dist/es/errors/notSupportedError.js.map +1 -0
  25. package/dist/es/errors/unauthorizedError.js +21 -0
  26. package/dist/es/errors/unauthorizedError.js.map +1 -0
  27. package/dist/es/errors/unprocessableError.js +21 -0
  28. package/dist/es/errors/unprocessableError.js.map +1 -0
  29. package/dist/es/errors/validationError.js +23 -0
  30. package/dist/es/errors/validationError.js.map +1 -0
  31. package/dist/es/factories/componentFactory.js +9 -0
  32. package/dist/es/factories/componentFactory.js.map +1 -0
  33. package/dist/es/factories/factory.js +256 -0
  34. package/dist/es/factories/factory.js.map +1 -0
  35. package/dist/es/helpers/arrayHelper.js +44 -0
  36. package/dist/es/helpers/arrayHelper.js.map +1 -0
  37. package/dist/es/helpers/envHelper.js +39 -0
  38. package/dist/es/helpers/envHelper.js.map +1 -0
  39. package/dist/es/helpers/errorHelper.js +100 -0
  40. package/dist/es/helpers/errorHelper.js.map +1 -0
  41. package/dist/es/helpers/filenameHelper.js +32 -0
  42. package/dist/es/helpers/filenameHelper.js.map +1 -0
  43. package/dist/es/helpers/hexHelper.js +45 -0
  44. package/dist/es/helpers/hexHelper.js.map +1 -0
  45. package/dist/es/helpers/jsonHelper.js +163 -0
  46. package/dist/es/helpers/jsonHelper.js.map +1 -0
  47. package/dist/es/helpers/numberHelper.js +30 -0
  48. package/dist/es/helpers/numberHelper.js.map +1 -0
  49. package/dist/es/helpers/objectHelper.js +333 -0
  50. package/dist/es/helpers/objectHelper.js.map +1 -0
  51. package/dist/es/helpers/randomHelper.js +18 -0
  52. package/dist/es/helpers/randomHelper.js.map +1 -0
  53. package/dist/es/helpers/stringHelper.js +241 -0
  54. package/dist/es/helpers/stringHelper.js.map +1 -0
  55. package/dist/es/helpers/uint8ArrayHelper.js +26 -0
  56. package/dist/es/helpers/uint8ArrayHelper.js.map +1 -0
  57. package/dist/es/index.js +57 -0
  58. package/dist/es/index.js.map +1 -0
  59. package/dist/es/models/IComponent.js +4 -0
  60. package/dist/es/models/IComponent.js.map +1 -0
  61. package/dist/es/models/IError.js +4 -0
  62. package/dist/es/models/IError.js.map +1 -0
  63. package/dist/es/models/II18nShared.js +2 -0
  64. package/dist/es/models/II18nShared.js.map +1 -0
  65. package/dist/es/models/IKeyValue.js +2 -0
  66. package/dist/es/models/IKeyValue.js.map +1 -0
  67. package/dist/es/models/ILabelledValue.js +2 -0
  68. package/dist/es/models/ILabelledValue.js.map +1 -0
  69. package/dist/es/models/ILocale.js +2 -0
  70. package/dist/es/models/ILocale.js.map +1 -0
  71. package/dist/es/models/ILocaleDictionary.js +4 -0
  72. package/dist/es/models/ILocaleDictionary.js.map +1 -0
  73. package/dist/es/models/ILocalesIndex.js +2 -0
  74. package/dist/es/models/ILocalesIndex.js.map +1 -0
  75. package/dist/es/models/IPatchOperation.js +4 -0
  76. package/dist/es/models/IPatchOperation.js.map +1 -0
  77. package/dist/es/models/IUrlParts.js +4 -0
  78. package/dist/es/models/IUrlParts.js.map +1 -0
  79. package/dist/es/models/IValidationFailure.js +2 -0
  80. package/dist/es/models/IValidationFailure.js.map +1 -0
  81. package/dist/es/models/coerceType.js +49 -0
  82. package/dist/es/models/coerceType.js.map +1 -0
  83. package/dist/es/models/compressionType.js +17 -0
  84. package/dist/es/models/compressionType.js.map +1 -0
  85. package/dist/es/models/objectOrArray.js +2 -0
  86. package/dist/es/models/objectOrArray.js.map +1 -0
  87. package/dist/es/types/bitString.js +98 -0
  88. package/dist/es/types/bitString.js.map +1 -0
  89. package/dist/es/types/url.js +144 -0
  90. package/dist/es/types/url.js.map +1 -0
  91. package/dist/es/types/urn.js +212 -0
  92. package/dist/es/types/urn.js.map +1 -0
  93. package/dist/es/utils/asyncCache.js +179 -0
  94. package/dist/es/utils/asyncCache.js.map +1 -0
  95. package/dist/es/utils/coerce.js +269 -0
  96. package/dist/es/utils/coerce.js.map +1 -0
  97. package/dist/es/utils/compression.js +49 -0
  98. package/dist/es/utils/compression.js.map +1 -0
  99. package/dist/es/utils/converter.js +256 -0
  100. package/dist/es/utils/converter.js.map +1 -0
  101. package/dist/es/utils/guards.js +369 -0
  102. package/dist/es/utils/guards.js.map +1 -0
  103. package/dist/es/utils/i18n.js +243 -0
  104. package/dist/es/utils/i18n.js.map +1 -0
  105. package/dist/es/utils/is.js +351 -0
  106. package/dist/es/utils/is.js.map +1 -0
  107. package/dist/es/utils/sharedStore.js +48 -0
  108. package/dist/es/utils/sharedStore.js.map +1 -0
  109. package/dist/es/utils/validation.js +774 -0
  110. package/dist/es/utils/validation.js.map +1 -0
  111. package/dist/types/encoding/base32.d.ts +4 -0
  112. package/dist/types/encoding/base58.d.ts +4 -0
  113. package/dist/types/encoding/base64.d.ts +4 -0
  114. package/dist/types/encoding/base64Url.d.ts +4 -0
  115. package/dist/types/errors/alreadyExistsError.d.ts +5 -2
  116. package/dist/types/errors/baseError.d.ts +1 -1
  117. package/dist/types/errors/conflictError.d.ts +5 -2
  118. package/dist/types/errors/generalError.d.ts +1 -1
  119. package/dist/types/errors/guardError.d.ts +1 -1
  120. package/dist/types/errors/notFoundError.d.ts +5 -2
  121. package/dist/types/errors/notImplementedError.d.ts +6 -2
  122. package/dist/types/errors/notSupportedError.d.ts +5 -2
  123. package/dist/types/errors/unauthorizedError.d.ts +5 -2
  124. package/dist/types/errors/unprocessableError.d.ts +1 -1
  125. package/dist/types/errors/validationError.d.ts +2 -2
  126. package/dist/types/factories/componentFactory.d.ts +2 -2
  127. package/dist/types/factories/factory.d.ts +4 -0
  128. package/dist/types/helpers/arrayHelper.d.ts +1 -1
  129. package/dist/types/helpers/errorHelper.d.ts +1 -1
  130. package/dist/types/helpers/jsonHelper.d.ts +5 -1
  131. package/dist/types/helpers/numberHelper.d.ts +17 -0
  132. package/dist/types/helpers/objectHelper.d.ts +14 -0
  133. package/dist/types/helpers/stringHelper.d.ts +6 -6
  134. package/dist/types/index.d.ts +54 -53
  135. package/dist/types/models/IComponent.d.ts +6 -7
  136. package/dist/types/models/ILocalesIndex.d.ts +1 -1
  137. package/dist/types/models/IValidationFailure.d.ts +0 -4
  138. package/dist/types/types/bitString.d.ts +4 -0
  139. package/dist/types/types/url.d.ts +8 -3
  140. package/dist/types/types/urn.d.ts +7 -2
  141. package/dist/types/utils/coerce.d.ts +1 -1
  142. package/dist/types/utils/compression.d.ts +5 -1
  143. package/dist/types/utils/guards.d.ts +2 -3
  144. package/dist/types/utils/i18n.d.ts +16 -1
  145. package/dist/types/utils/is.d.ts +1 -1
  146. package/dist/types/utils/validation.d.ts +1 -1
  147. package/docs/changelog.md +283 -0
  148. package/docs/reference/classes/AlreadyExistsError.md +8 -4
  149. package/docs/reference/classes/AsyncCache.md +5 -5
  150. package/docs/reference/classes/Base32.md +8 -0
  151. package/docs/reference/classes/Base58.md +8 -0
  152. package/docs/reference/classes/Base64.md +8 -0
  153. package/docs/reference/classes/Base64Url.md +8 -0
  154. package/docs/reference/classes/BaseError.md +3 -3
  155. package/docs/reference/classes/BitString.md +8 -0
  156. package/docs/reference/classes/Coerce.md +20 -20
  157. package/docs/reference/classes/Compression.md +8 -0
  158. package/docs/reference/classes/ConflictError.md +8 -4
  159. package/docs/reference/classes/ErrorHelper.md +2 -2
  160. package/docs/reference/classes/Factory.md +11 -3
  161. package/docs/reference/classes/GeneralError.md +3 -3
  162. package/docs/reference/classes/GuardError.md +3 -3
  163. package/docs/reference/classes/Guards.md +8 -4
  164. package/docs/reference/classes/I18n.md +54 -2
  165. package/docs/reference/classes/Is.md +10 -4
  166. package/docs/reference/classes/JsonHelper.md +8 -0
  167. package/docs/reference/classes/NotFoundError.md +8 -4
  168. package/docs/reference/classes/NotImplementedError.md +14 -4
  169. package/docs/reference/classes/NotSupportedError.md +8 -4
  170. package/docs/reference/classes/NumberHelper.md +55 -0
  171. package/docs/reference/classes/ObjectHelper.md +58 -8
  172. package/docs/reference/classes/SharedStore.md +2 -2
  173. package/docs/reference/classes/StringHelper.md +8 -8
  174. package/docs/reference/classes/UnauthorizedError.md +8 -4
  175. package/docs/reference/classes/UnprocessableError.md +3 -3
  176. package/docs/reference/classes/Url.md +17 -3
  177. package/docs/reference/classes/Urn.md +19 -5
  178. package/docs/reference/classes/Validation.md +2 -2
  179. package/docs/reference/classes/ValidationError.md +3 -3
  180. package/docs/reference/index.md +1 -0
  181. package/docs/reference/interfaces/IComponent.md +19 -25
  182. package/docs/reference/interfaces/IValidationFailure.md +0 -8
  183. package/locales/.validate-ignore +1 -0
  184. package/locales/en.json +4 -5
  185. package/package.json +22 -10
  186. package/dist/cjs/index.cjs +0 -5175
  187. package/dist/esm/index.mjs +0 -5133
@@ -0,0 +1,256 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ /* eslint-disable no-bitwise */
4
+ import { Base58 } from "../encoding/base58.js";
5
+ import { Base64 } from "../encoding/base64.js";
6
+ import { Base64Url } from "../encoding/base64Url.js";
7
+ import { HexHelper } from "../helpers/hexHelper.js";
8
+ /**
9
+ * Convert arrays to and from different formats.
10
+ */
11
+ export class Converter {
12
+ /**
13
+ * Lookup table for encoding.
14
+ * @internal
15
+ */
16
+ static _ENCODE_LOOKUP;
17
+ /**
18
+ * Lookup table for decoding.
19
+ * @internal
20
+ */
21
+ static _DECODE_LOOKUP;
22
+ /**
23
+ * Encode a raw array to UTF8 string.
24
+ * @param array The bytes to encode.
25
+ * @param startIndex The index to start in the bytes.
26
+ * @param length The length of bytes to read.
27
+ * @returns The array formatted as UTF8.
28
+ */
29
+ static bytesToUtf8(array, startIndex, length) {
30
+ const start = startIndex ?? 0;
31
+ const len = length ?? array.length;
32
+ let str = "";
33
+ for (let i = start; i < start + len; i++) {
34
+ const value = array[i];
35
+ if (value < 0x80) {
36
+ str += String.fromCharCode(value);
37
+ }
38
+ else if (value > 0xbf && value < 0xe0) {
39
+ str += String.fromCharCode(((value & 0x1f) << 6) | (array[i + 1] & 0x3f));
40
+ i += 1;
41
+ }
42
+ else if (value > 0xdf && value < 0xf0) {
43
+ str += String.fromCharCode(((value & 0x0f) << 12) | ((array[i + 1] & 0x3f) << 6) | (array[i + 2] & 0x3f));
44
+ i += 2;
45
+ }
46
+ else {
47
+ // surrogate pair
48
+ const charCode = (((value & 0x07) << 18) |
49
+ ((array[i + 1] & 0x3f) << 12) |
50
+ ((array[i + 2] & 0x3f) << 6) |
51
+ (array[i + 3] & 0x3f)) -
52
+ 0x010000;
53
+ str += String.fromCharCode((charCode >> 10) | 0xd800, (charCode & 0x03ff) | 0xdc00);
54
+ i += 3;
55
+ }
56
+ }
57
+ return str;
58
+ }
59
+ /**
60
+ * Convert a UTF8 string to raw array.
61
+ * @param utf8 The text to decode.
62
+ * @returns The array.
63
+ */
64
+ static utf8ToBytes(utf8) {
65
+ const bytes = [];
66
+ for (let i = 0; i < utf8.length; i++) {
67
+ let charCode = utf8.charCodeAt(i);
68
+ if (charCode < 0x80) {
69
+ bytes.push(charCode);
70
+ }
71
+ else if (charCode < 0x800) {
72
+ bytes.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
73
+ }
74
+ else if (charCode < 0xd800 || charCode >= 0xe000) {
75
+ bytes.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
76
+ }
77
+ else {
78
+ // surrogate pair
79
+ i++;
80
+ // UTF-16 encodes 0x10000-0x10FFFF by
81
+ // subtracting 0x10000 and splitting the
82
+ // 20 bits of 0x0-0xFFFFF into two halves
83
+ charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (utf8.charCodeAt(i) & 0x3ff));
84
+ bytes.push(0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
85
+ }
86
+ }
87
+ return Uint8Array.from(bytes);
88
+ }
89
+ /**
90
+ * Encode a raw array to hex string.
91
+ * @param array The bytes to encode.
92
+ * @param includePrefix Include the 0x prefix on the returned hex.
93
+ * @param startIndex The index to start in the bytes.
94
+ * @param length The length of bytes to read.
95
+ * @param reverse Reverse the combine direction.
96
+ * @returns The array formatted as hex.
97
+ */
98
+ static bytesToHex(array, includePrefix = false, startIndex, length, reverse) {
99
+ let hex = "";
100
+ Converter.buildHexLookups();
101
+ if (Converter._ENCODE_LOOKUP) {
102
+ const len = length ?? array.length;
103
+ const start = startIndex ?? 0;
104
+ if (reverse) {
105
+ for (let i = 0; i < len; i++) {
106
+ hex = Converter._ENCODE_LOOKUP[array[start + i]] + hex;
107
+ }
108
+ }
109
+ else {
110
+ for (let i = 0; i < len; i++) {
111
+ hex += Converter._ENCODE_LOOKUP[array[start + i]];
112
+ }
113
+ }
114
+ }
115
+ return includePrefix ? HexHelper.addPrefix(hex) : hex;
116
+ }
117
+ /**
118
+ * Decode a hex string to raw array.
119
+ * @param hex The hex to decode.
120
+ * @param reverse Store the characters in reverse.
121
+ * @returns The array.
122
+ */
123
+ static hexToBytes(hex, reverse) {
124
+ const strippedHex = HexHelper.stripPrefix(hex);
125
+ const sizeof = strippedHex.length >> 1;
126
+ const length = sizeof << 1;
127
+ const array = new Uint8Array(sizeof);
128
+ Converter.buildHexLookups();
129
+ if (Converter._DECODE_LOOKUP) {
130
+ let i = 0;
131
+ let n = 0;
132
+ while (i < length) {
133
+ array[n++] =
134
+ (Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)] << 4) |
135
+ Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)];
136
+ }
137
+ if (reverse) {
138
+ array.reverse();
139
+ }
140
+ }
141
+ return array;
142
+ }
143
+ /**
144
+ * Convert the UTF8 to hex.
145
+ * @param utf8 The text to convert.
146
+ * @param includePrefix Include the 0x prefix on the returned hex.
147
+ * @returns The hex version of the bytes.
148
+ */
149
+ static utf8ToHex(utf8, includePrefix = false) {
150
+ const hex = Converter.bytesToHex(Converter.utf8ToBytes(utf8));
151
+ return includePrefix ? HexHelper.addPrefix(hex) : hex;
152
+ }
153
+ /**
154
+ * Convert the hex text to text.
155
+ * @param hex The hex to convert.
156
+ * @returns The UTF8 version of the bytes.
157
+ */
158
+ static hexToUtf8(hex) {
159
+ return Converter.bytesToUtf8(Converter.hexToBytes(HexHelper.stripPrefix(hex)));
160
+ }
161
+ /**
162
+ * Convert bytes to binary string.
163
+ * @param bytes The bytes to convert.
164
+ * @returns A binary string of the bytes.
165
+ */
166
+ static bytesToBinary(bytes) {
167
+ const b = [];
168
+ for (let i = 0; i < bytes.length; i++) {
169
+ b.push(bytes[i].toString(2).padStart(8, "0"));
170
+ }
171
+ return b.join("");
172
+ }
173
+ /**
174
+ * Convert a binary string to bytes.
175
+ * @param binary The binary string.
176
+ * @returns The bytes.
177
+ */
178
+ static binaryToBytes(binary) {
179
+ const bytes = new Uint8Array(Math.ceil(binary.length / 8));
180
+ for (let i = 0; i < bytes.length; i++) {
181
+ bytes[i] = Number.parseInt(binary.slice(i * 8, (i + 1) * 8), 2);
182
+ }
183
+ return bytes;
184
+ }
185
+ /**
186
+ * Convert bytes to base64 string.
187
+ * @param bytes The bytes to convert.
188
+ * @returns A base64 string of the bytes.
189
+ */
190
+ static bytesToBase64(bytes) {
191
+ return Base64.encode(bytes);
192
+ }
193
+ /**
194
+ * Convert a base64 string to bytes.
195
+ * @param base64 The base64 string.
196
+ * @returns The bytes.
197
+ */
198
+ static base64ToBytes(base64) {
199
+ return Base64.decode(base64);
200
+ }
201
+ /**
202
+ * Convert bytes to base64 url string.
203
+ * @param bytes The bytes to convert.
204
+ * @returns A base64 url string of the bytes.
205
+ */
206
+ static bytesToBase64Url(bytes) {
207
+ return Base64Url.encode(bytes);
208
+ }
209
+ /**
210
+ * Convert a base64 url string to bytes.
211
+ * @param base64Url The base64 url string.
212
+ * @returns The bytes.
213
+ */
214
+ static base64UrlToBytes(base64Url) {
215
+ return Base64Url.decode(base64Url);
216
+ }
217
+ /**
218
+ * Convert bytes to base58 string.
219
+ * @param bytes The bytes to convert.
220
+ * @returns A base58 string of the bytes.
221
+ */
222
+ static bytesToBase58(bytes) {
223
+ return Base58.encode(bytes);
224
+ }
225
+ /**
226
+ * Convert a base58 string to bytes.
227
+ * @param base58 The base58 string.
228
+ * @returns The bytes.
229
+ */
230
+ static base58ToBytes(base58) {
231
+ return Base58.decode(base58);
232
+ }
233
+ /**
234
+ * Build the static lookup tables.
235
+ * @internal
236
+ */
237
+ static buildHexLookups() {
238
+ if (!Converter._ENCODE_LOOKUP || !Converter._DECODE_LOOKUP) {
239
+ const alphabet = "0123456789abcdef";
240
+ Converter._ENCODE_LOOKUP = [];
241
+ Converter._DECODE_LOOKUP = [];
242
+ for (let i = 0; i < 256; i++) {
243
+ Converter._ENCODE_LOOKUP[i] = alphabet[(i >> 4) & 0xf] + alphabet[i & 0xf];
244
+ if (i < 16) {
245
+ if (i < 10) {
246
+ Converter._DECODE_LOOKUP[0x30 + i] = i;
247
+ }
248
+ else {
249
+ Converter._DECODE_LOOKUP[0x61 - 10 + i] = i;
250
+ }
251
+ }
252
+ }
253
+ }
254
+ }
255
+ }
256
+ //# sourceMappingURL=converter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"converter.js","sourceRoot":"","sources":["../../../src/utils/converter.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,+BAA+B;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD;;GAEG;AACH,MAAM,OAAO,SAAS;IACrB;;;OAGG;IACK,MAAM,CAAC,cAAc,CAAuB;IAEpD;;;OAGG;IACK,MAAM,CAAC,cAAc,CAAuB;IAEpD;;;;;;OAMG;IACI,MAAM,CAAC,WAAW,CACxB,KAAwB,EACxB,UAAmB,EACnB,MAA2B;QAE3B,MAAM,KAAK,GAAG,UAAU,IAAI,CAAC,CAAC;QAC9B,MAAM,GAAG,GAAG,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;QACnC,IAAI,GAAG,GAAG,EAAE,CAAC;QAEb,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,KAAK,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YAC1C,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;YAEvB,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;gBAClB,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YACnC,CAAC;iBAAM,IAAI,KAAK,GAAG,IAAI,IAAI,KAAK,GAAG,IAAI,EAAE,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AC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\n/* eslint-disable no-bitwise */\nimport { Base58 } from \"../encoding/base58.js\";\nimport { Base64 } from \"../encoding/base64.js\";\nimport { Base64Url } from \"../encoding/base64Url.js\";\nimport { HexHelper } from \"../helpers/hexHelper.js\";\n\n/**\n * Convert arrays to and from different formats.\n */\nexport class Converter {\n\t/**\n\t * Lookup table for encoding.\n\t * @internal\n\t */\n\tprivate static _ENCODE_LOOKUP: string[] | undefined;\n\n\t/**\n\t * Lookup table for decoding.\n\t * @internal\n\t */\n\tprivate static _DECODE_LOOKUP: number[] | undefined;\n\n\t/**\n\t * Encode a raw array to UTF8 string.\n\t * @param array The bytes to encode.\n\t * @param startIndex The index to start in the bytes.\n\t * @param length The length of bytes to read.\n\t * @returns The array formatted as UTF8.\n\t */\n\tpublic static bytesToUtf8(\n\t\tarray: ArrayLike<number>,\n\t\tstartIndex?: number,\n\t\tlength?: number | undefined\n\t): string {\n\t\tconst start = startIndex ?? 0;\n\t\tconst len = length ?? array.length;\n\t\tlet str = \"\";\n\n\t\tfor (let i = start; i < start + len; i++) {\n\t\t\tconst value = array[i];\n\n\t\t\tif (value < 0x80) {\n\t\t\t\tstr += String.fromCharCode(value);\n\t\t\t} else if (value > 0xbf && value < 0xe0) {\n\t\t\t\tstr += String.fromCharCode(((value & 0x1f) << 6) | (array[i + 1] & 0x3f));\n\t\t\t\ti += 1;\n\t\t\t} else if (value > 0xdf && value < 0xf0) {\n\t\t\t\tstr += String.fromCharCode(\n\t\t\t\t\t((value & 0x0f) << 12) | ((array[i + 1] & 0x3f) << 6) | (array[i + 2] & 0x3f)\n\t\t\t\t);\n\t\t\t\ti += 2;\n\t\t\t} else {\n\t\t\t\t// surrogate pair\n\t\t\t\tconst charCode =\n\t\t\t\t\t(((value & 0x07) << 18) |\n\t\t\t\t\t\t((array[i + 1] & 0x3f) << 12) |\n\t\t\t\t\t\t((array[i + 2] & 0x3f) << 6) |\n\t\t\t\t\t\t(array[i + 3] & 0x3f)) -\n\t\t\t\t\t0x010000;\n\n\t\t\t\tstr += String.fromCharCode((charCode >> 10) | 0xd800, (charCode & 0x03ff) | 0xdc00);\n\t\t\t\ti += 3;\n\t\t\t}\n\t\t}\n\n\t\treturn str;\n\t}\n\n\t/**\n\t * Convert a UTF8 string to raw array.\n\t * @param utf8 The text to decode.\n\t * @returns The array.\n\t */\n\tpublic static utf8ToBytes(utf8: string): Uint8Array {\n\t\tconst bytes: number[] = [];\n\n\t\tfor (let i = 0; i < utf8.length; i++) {\n\t\t\tlet charCode = utf8.charCodeAt(i);\n\t\t\tif (charCode < 0x80) {\n\t\t\t\tbytes.push(charCode);\n\t\t\t} else if (charCode < 0x800) {\n\t\t\t\tbytes.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));\n\t\t\t} else if (charCode < 0xd800 || charCode >= 0xe000) {\n\t\t\t\tbytes.push(\n\t\t\t\t\t0xe0 | (charCode >> 12),\n\t\t\t\t\t0x80 | ((charCode >> 6) & 0x3f),\n\t\t\t\t\t0x80 | (charCode & 0x3f)\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// surrogate pair\n\t\t\t\ti++;\n\t\t\t\t// UTF-16 encodes 0x10000-0x10FFFF by\n\t\t\t\t// subtracting 0x10000 and splitting the\n\t\t\t\t// 20 bits of 0x0-0xFFFFF into two halves\n\t\t\t\tcharCode = 0x10000 + (((charCode & 0x3ff) << 10) | (utf8.charCodeAt(i) & 0x3ff));\n\t\t\t\tbytes.push(\n\t\t\t\t\t0xf0 | (charCode >> 18),\n\t\t\t\t\t0x80 | ((charCode >> 12) & 0x3f),\n\t\t\t\t\t0x80 | ((charCode >> 6) & 0x3f),\n\t\t\t\t\t0x80 | (charCode & 0x3f)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\treturn Uint8Array.from(bytes);\n\t}\n\n\t/**\n\t * Encode a raw array to hex string.\n\t * @param array The bytes to encode.\n\t * @param includePrefix Include the 0x prefix on the returned hex.\n\t * @param startIndex The index to start in the bytes.\n\t * @param length The length of bytes to read.\n\t * @param reverse Reverse the combine direction.\n\t * @returns The array formatted as hex.\n\t */\n\tpublic static bytesToHex(\n\t\tarray: ArrayLike<number>,\n\t\tincludePrefix: boolean = false,\n\t\tstartIndex?: number,\n\t\tlength?: number | undefined,\n\t\treverse?: boolean\n\t): string {\n\t\tlet hex = \"\";\n\t\tConverter.buildHexLookups();\n\t\tif (Converter._ENCODE_LOOKUP) {\n\t\t\tconst len = length ?? array.length;\n\t\t\tconst start = startIndex ?? 0;\n\t\t\tif (reverse) {\n\t\t\t\tfor (let i = 0; i < len; i++) {\n\t\t\t\t\thex = Converter._ENCODE_LOOKUP[array[start + i]] + hex;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfor (let i = 0; i < len; i++) {\n\t\t\t\t\thex += Converter._ENCODE_LOOKUP[array[start + i]];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn includePrefix ? HexHelper.addPrefix(hex) : hex;\n\t}\n\n\t/**\n\t * Decode a hex string to raw array.\n\t * @param hex The hex to decode.\n\t * @param reverse Store the characters in reverse.\n\t * @returns The array.\n\t */\n\tpublic static hexToBytes(hex: string, reverse?: boolean): Uint8Array {\n\t\tconst strippedHex = HexHelper.stripPrefix(hex);\n\t\tconst sizeof = strippedHex.length >> 1;\n\t\tconst length = sizeof << 1;\n\t\tconst array = new Uint8Array(sizeof);\n\n\t\tConverter.buildHexLookups();\n\t\tif (Converter._DECODE_LOOKUP) {\n\t\t\tlet i = 0;\n\t\t\tlet n = 0;\n\t\t\twhile (i < length) {\n\t\t\t\tarray[n++] =\n\t\t\t\t\t(Converter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)] << 4) |\n\t\t\t\t\tConverter._DECODE_LOOKUP[strippedHex.charCodeAt(i++)];\n\t\t\t}\n\n\t\t\tif (reverse) {\n\t\t\t\tarray.reverse();\n\t\t\t}\n\t\t}\n\t\treturn array;\n\t}\n\n\t/**\n\t * Convert the UTF8 to hex.\n\t * @param utf8 The text to convert.\n\t * @param includePrefix Include the 0x prefix on the returned hex.\n\t * @returns The hex version of the bytes.\n\t */\n\tpublic static utf8ToHex(utf8: string, includePrefix: boolean = false): string {\n\t\tconst hex = Converter.bytesToHex(Converter.utf8ToBytes(utf8));\n\t\treturn includePrefix ? HexHelper.addPrefix(hex) : hex;\n\t}\n\n\t/**\n\t * Convert the hex text to text.\n\t * @param hex The hex to convert.\n\t * @returns The UTF8 version of the bytes.\n\t */\n\tpublic static hexToUtf8(hex: string): string {\n\t\treturn Converter.bytesToUtf8(Converter.hexToBytes(HexHelper.stripPrefix(hex)));\n\t}\n\n\t/**\n\t * Convert bytes to binary string.\n\t * @param bytes The bytes to convert.\n\t * @returns A binary string of the bytes.\n\t */\n\tpublic static bytesToBinary(bytes: Uint8Array): string {\n\t\tconst b = [];\n\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\tb.push(bytes[i].toString(2).padStart(8, \"0\"));\n\t\t}\n\t\treturn b.join(\"\");\n\t}\n\n\t/**\n\t * Convert a binary string to bytes.\n\t * @param binary The binary string.\n\t * @returns The bytes.\n\t */\n\tpublic static binaryToBytes(binary: string): Uint8Array {\n\t\tconst bytes = new Uint8Array(Math.ceil(binary.length / 8));\n\t\tfor (let i = 0; i < bytes.length; i++) {\n\t\t\tbytes[i] = Number.parseInt(binary.slice(i * 8, (i + 1) * 8), 2);\n\t\t}\n\t\treturn bytes;\n\t}\n\n\t/**\n\t * Convert bytes to base64 string.\n\t * @param bytes The bytes to convert.\n\t * @returns A base64 string of the bytes.\n\t */\n\tpublic static bytesToBase64(bytes: Uint8Array): string {\n\t\treturn Base64.encode(bytes);\n\t}\n\n\t/**\n\t * Convert a base64 string to bytes.\n\t * @param base64 The base64 string.\n\t * @returns The bytes.\n\t */\n\tpublic static base64ToBytes(base64: string): Uint8Array {\n\t\treturn Base64.decode(base64);\n\t}\n\n\t/**\n\t * Convert bytes to base64 url string.\n\t * @param bytes The bytes to convert.\n\t * @returns A base64 url string of the bytes.\n\t */\n\tpublic static bytesToBase64Url(bytes: Uint8Array): string {\n\t\treturn Base64Url.encode(bytes);\n\t}\n\n\t/**\n\t * Convert a base64 url string to bytes.\n\t * @param base64Url The base64 url string.\n\t * @returns The bytes.\n\t */\n\tpublic static base64UrlToBytes(base64Url: string): Uint8Array {\n\t\treturn Base64Url.decode(base64Url);\n\t}\n\n\t/**\n\t * Convert bytes to base58 string.\n\t * @param bytes The bytes to convert.\n\t * @returns A base58 string of the bytes.\n\t */\n\tpublic static bytesToBase58(bytes: Uint8Array): string {\n\t\treturn Base58.encode(bytes);\n\t}\n\n\t/**\n\t * Convert a base58 string to bytes.\n\t * @param base58 The base58 string.\n\t * @returns The bytes.\n\t */\n\tpublic static base58ToBytes(base58: string): Uint8Array {\n\t\treturn Base58.decode(base58);\n\t}\n\n\t/**\n\t * Build the static lookup tables.\n\t * @internal\n\t */\n\tprivate static buildHexLookups(): void {\n\t\tif (!Converter._ENCODE_LOOKUP || !Converter._DECODE_LOOKUP) {\n\t\t\tconst alphabet = \"0123456789abcdef\";\n\t\t\tConverter._ENCODE_LOOKUP = [];\n\t\t\tConverter._DECODE_LOOKUP = [];\n\n\t\t\tfor (let i = 0; i < 256; i++) {\n\t\t\t\tConverter._ENCODE_LOOKUP[i] = alphabet[(i >> 4) & 0xf] + alphabet[i & 0xf];\n\t\t\t\tif (i < 16) {\n\t\t\t\t\tif (i < 10) {\n\t\t\t\t\t\tConverter._DECODE_LOOKUP[0x30 + i] = i;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tConverter._DECODE_LOOKUP[0x61 - 10 + i] = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,369 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { Is } from "./is.js";
4
+ import { GuardError } from "../errors/guardError.js";
5
+ import { ArrayHelper } from "../helpers/arrayHelper.js";
6
+ import { HexHelper } from "../helpers/hexHelper.js";
7
+ /**
8
+ * Class to handle guard operations for parameters.
9
+ */
10
+ export class Guards {
11
+ /**
12
+ * Is the property defined.
13
+ * @param source The source of the error.
14
+ * @param property The name of the property.
15
+ * @param value The value to test.
16
+ * @throws GuardError If the value does not match the assertion.
17
+ */
18
+ static defined(source, property, value) {
19
+ if (Is.undefined(value)) {
20
+ throw new GuardError(source, "guard.undefined", property, value);
21
+ }
22
+ }
23
+ /**
24
+ * Is the property a string.
25
+ * @param source The source of the error.
26
+ * @param property The name of the property.
27
+ * @param value The value to test.
28
+ * @throws GuardError If the value does not match the assertion.
29
+ */
30
+ static string(source, property, value) {
31
+ if (!Is.string(value)) {
32
+ throw new GuardError(source, "guard.string", property, value);
33
+ }
34
+ }
35
+ /**
36
+ * Is the property a string with a value.
37
+ * @param source The source of the error.
38
+ * @param property The name of the property.
39
+ * @param value The value to test.
40
+ * @throws GuardError If the value does not match the assertion.
41
+ */
42
+ static stringValue(source, property, value) {
43
+ if (!Is.string(value)) {
44
+ throw new GuardError(source, "guard.string", property, value);
45
+ }
46
+ if (value.length === 0) {
47
+ throw new GuardError(source, "guard.stringEmpty", property, value);
48
+ }
49
+ }
50
+ /**
51
+ * Is the property a JSON value.
52
+ * @param source The source of the error.
53
+ * @param property The name of the property.
54
+ * @param value The value to test.
55
+ * @throws GuardError If the value does not match the assertion.
56
+ */
57
+ static json(source, property, value) {
58
+ if (!Is.json(value)) {
59
+ throw new GuardError(source, "guard.stringJson", property, value);
60
+ }
61
+ }
62
+ /**
63
+ * Is the property a base64 string.
64
+ * @param source The source of the error.
65
+ * @param property The name of the property.
66
+ * @param value The value to test.
67
+ * @throws GuardError If the value does not match the assertion.
68
+ */
69
+ static stringBase64(source, property, value) {
70
+ if (!Is.stringBase64(value)) {
71
+ throw new GuardError(source, "guard.stringBase64", property, value);
72
+ }
73
+ }
74
+ /**
75
+ * Is the property a base64 url string.
76
+ * @param source The source of the error.
77
+ * @param property The name of the property.
78
+ * @param value The value to test.
79
+ * @throws GuardError If the value does not match the assertion.
80
+ */
81
+ static stringBase64Url(source, property, value) {
82
+ if (!Is.stringBase64Url(value)) {
83
+ throw new GuardError(source, "guard.stringBase64Url", property, value);
84
+ }
85
+ }
86
+ /**
87
+ * Is the property a base58 string.
88
+ * @param source The source of the error.
89
+ * @param property The name of the property.
90
+ * @param value The value to test.
91
+ * @throws GuardError If the value does not match the assertion.
92
+ */
93
+ static stringBase58(source, property, value) {
94
+ if (!Is.stringBase58(value)) {
95
+ throw new GuardError(source, "guard.stringBase58", property, value);
96
+ }
97
+ }
98
+ /**
99
+ * Is the property a string with a hex value.
100
+ * @param source The source of the error.
101
+ * @param property The name of the property.
102
+ * @param value The value to test.
103
+ * @param allowPrefix Allow the hex to have the 0x prefix.
104
+ * @throws GuardError If the value does not match the assertion.
105
+ */
106
+ static stringHex(source, property, value, allowPrefix = false) {
107
+ Guards.stringValue(source, property, value);
108
+ if (!HexHelper.isHex(value, allowPrefix)) {
109
+ throw new GuardError(source, "guard.stringHex", property, value);
110
+ }
111
+ }
112
+ /**
113
+ * Is the property a string with a hex value with fixed length.
114
+ * @param source The source of the error.
115
+ * @param property The name of the property.
116
+ * @param value The value to test.
117
+ * @param length The length of the string to match.
118
+ * @param allowPrefix Allow the hex to have the 0x prefix.
119
+ * @throws GuardError If the value does not match the assertion.
120
+ */
121
+ static stringHexLength(source, property, value, length, allowPrefix = false) {
122
+ Guards.stringHex(source, property, value, allowPrefix);
123
+ if (HexHelper.stripPrefix(value).length !== length) {
124
+ throw new GuardError(source, "guard.stringHexLength", property, value.length, length.toString());
125
+ }
126
+ }
127
+ /**
128
+ * Is the property a number.
129
+ * @param source The source of the error.
130
+ * @param property The name of the property.
131
+ * @param value The value to test.
132
+ * @throws GuardError If the value does not match the assertion.
133
+ */
134
+ static number(source, property, value) {
135
+ if (!Is.number(value)) {
136
+ throw new GuardError(source, "guard.number", property, value);
137
+ }
138
+ }
139
+ /**
140
+ * Is the property an integer.
141
+ * @param source The source of the error.
142
+ * @param property The name of the property.
143
+ * @param value The value to test.
144
+ * @throws GuardError If the value does not match the assertion.
145
+ */
146
+ static integer(source, property, value) {
147
+ if (!Is.integer(value)) {
148
+ throw new GuardError(source, "guard.integer", property, value);
149
+ }
150
+ }
151
+ /**
152
+ * Is the property a bigint.
153
+ * @param source The source of the error.
154
+ * @param property The name of the property.
155
+ * @param value The value to test.
156
+ * @throws GuardError If the value does not match the assertion.
157
+ */
158
+ static bigint(source, property, value) {
159
+ if (!Is.bigint(value)) {
160
+ throw new GuardError(source, "guard.bigint", property, value);
161
+ }
162
+ }
163
+ /**
164
+ * Is the property a boolean.
165
+ * @param source The source of the error.
166
+ * @param property The name of the property.
167
+ * @param value The value to test.
168
+ * @throws GuardError If the value does not match the assertion.
169
+ */
170
+ static boolean(source, property, value) {
171
+ if (!Is.boolean(value)) {
172
+ throw new GuardError(source, "guard.boolean", property, value);
173
+ }
174
+ }
175
+ /**
176
+ * Is the property a date.
177
+ * @param source The source of the error.
178
+ * @param property The name of the property.
179
+ * @param value The value to test.
180
+ * @throws GuardError If the value does not match the assertion.
181
+ */
182
+ static date(source, property, value) {
183
+ if (!Is.date(value)) {
184
+ throw new GuardError(source, "guard.date", property, value);
185
+ }
186
+ }
187
+ /**
188
+ * Is the property a timestamp in milliseconds.
189
+ * @param source The source of the error.
190
+ * @param property The name of the property.
191
+ * @param value The value to test.
192
+ * @throws GuardError If the value does not match the assertion.
193
+ */
194
+ static timestampMilliseconds(source, property, value) {
195
+ if (!Is.timestampMilliseconds(value)) {
196
+ throw new GuardError(source, "guard.timestampMilliseconds", property, value);
197
+ }
198
+ }
199
+ /**
200
+ * Is the property a timestamp in seconds.
201
+ * @param source The source of the error.
202
+ * @param property The name of the property.
203
+ * @param value The value to test.
204
+ * @throws GuardError If the value does not match the assertion.
205
+ */
206
+ static timestampSeconds(source, property, value) {
207
+ if (!Is.timestampSeconds(value)) {
208
+ throw new GuardError(source, "guard.timestampSeconds", property, value);
209
+ }
210
+ }
211
+ /**
212
+ * Is the property an object.
213
+ * @param source The source of the error.
214
+ * @param property The name of the property.
215
+ * @param value The value to test.
216
+ * @throws GuardError If the value does not match the assertion.
217
+ */
218
+ static object(source, property, value) {
219
+ if (Is.undefined(value)) {
220
+ throw new GuardError(source, "guard.objectUndefined", property, value);
221
+ }
222
+ if (!Is.object(value)) {
223
+ throw new GuardError(source, "guard.object", property, value);
224
+ }
225
+ }
226
+ /**
227
+ * Is the property is an object with at least one property.
228
+ * @param source The source of the error.
229
+ * @param property The name of the property.
230
+ * @param value The value to test.
231
+ * @throws GuardError If the value does not match the assertion.
232
+ */
233
+ static objectValue(source, property, value) {
234
+ if (Is.undefined(value)) {
235
+ throw new GuardError(source, "guard.objectUndefined", property, value);
236
+ }
237
+ if (!Is.object(value)) {
238
+ throw new GuardError(source, "guard.object", property, value);
239
+ }
240
+ if (Object.keys(value || {}).length === 0) {
241
+ throw new GuardError(source, "guard.objectValue", property, value);
242
+ }
243
+ }
244
+ /**
245
+ * Is the property is an array.
246
+ * @param source The source of the error.
247
+ * @param property The name of the property.
248
+ * @param value The value to test.
249
+ * @throws GuardError If the value does not match the assertion.
250
+ */
251
+ static array(source, property, value) {
252
+ if (!Is.array(value)) {
253
+ throw new GuardError(source, "guard.array", property, value);
254
+ }
255
+ }
256
+ /**
257
+ * Is the property is an array with at least one item.
258
+ * @param source The source of the error.
259
+ * @param property The name of the property.
260
+ * @param value The value to test.
261
+ * @throws GuardError If the value does not match the assertion.
262
+ */
263
+ static arrayValue(source, property, value) {
264
+ if (!Is.array(value)) {
265
+ throw new GuardError(source, "guard.array", property, value);
266
+ }
267
+ if (value.length === 0) {
268
+ throw new GuardError(source, "guard.arrayValue", property, value);
269
+ }
270
+ }
271
+ /**
272
+ * Is the property one of a list of items.
273
+ * @param source The source of the error.
274
+ * @param property The name of the property.
275
+ * @param value The value to test.
276
+ * @param options The options the value must be one of.
277
+ * @throws GuardError If the value does not match the assertion.
278
+ */
279
+ static arrayOneOf(source, property, value, options) {
280
+ if (!Is.array(options)) {
281
+ throw new GuardError(source, "guard.array", property, value);
282
+ }
283
+ if (!options.includes(value)) {
284
+ throw new GuardError(source, "guard.arrayOneOf", property, value, options.join(", "));
285
+ }
286
+ }
287
+ /**
288
+ * Does the array start with the specified data.
289
+ * @param source The source of the error.
290
+ * @param property The name of the property.
291
+ * @param value The value to test.
292
+ * @param startValues The values that must start the array.
293
+ * @throws GuardError If the value does not match the assertion.
294
+ */
295
+ static arrayStartsWith(source, property, value, startValues) {
296
+ if (!Is.arrayValue(value)) {
297
+ throw new GuardError(source, "guard.array", property, value);
298
+ }
299
+ const startValuesArray = ArrayHelper.fromObjectOrArray(startValues);
300
+ if (!Is.arrayValue(startValuesArray)) {
301
+ throw new GuardError(source, "guard.array", property, startValuesArray);
302
+ }
303
+ for (let i = 0; i < startValuesArray.length; i++) {
304
+ if (value[i] !== startValuesArray[i]) {
305
+ throw new GuardError(source, "guard.arrayStartsWith", property, value, startValuesArray.join(", "));
306
+ }
307
+ }
308
+ }
309
+ /**
310
+ * Does the array end with the specified data.
311
+ * @param source The source of the error.
312
+ * @param property The name of the property.
313
+ * @param value The value to test.
314
+ * @param endValues The values that must end the array.
315
+ * @throws GuardError If the value does not match the assertion.
316
+ */
317
+ static arrayEndsWith(source, property, value, endValues) {
318
+ if (!Is.arrayValue(value)) {
319
+ throw new GuardError(source, "guard.array", property, value);
320
+ }
321
+ const endValuesArray = ArrayHelper.fromObjectOrArray(endValues);
322
+ if (!Is.arrayValue(endValuesArray)) {
323
+ throw new GuardError(source, "guard.array", property, endValuesArray);
324
+ }
325
+ for (let i = 0; i < endValuesArray.length; i++) {
326
+ if (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {
327
+ throw new GuardError(source, "guard.arrayEndsWith", property, value, endValuesArray.join(", "));
328
+ }
329
+ }
330
+ }
331
+ /**
332
+ * Is the property a Uint8Array.
333
+ * @param source The source of the error.
334
+ * @param property The name of the property.
335
+ * @param value The value to test.
336
+ * @throws GuardError If the value does not match the assertion.
337
+ */
338
+ static uint8Array(source, property, value) {
339
+ if (!Is.uint8Array(value)) {
340
+ throw new GuardError(source, "guard.uint8Array", property, value);
341
+ }
342
+ }
343
+ /**
344
+ * Is the property a function.
345
+ * @param source The source of the error.
346
+ * @param property The name of the property.
347
+ * @param value The value to test.
348
+ * @throws GuardError If the value does not match the assertion.
349
+ */
350
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
351
+ static function(source, property, value) {
352
+ if (!Is.function(value)) {
353
+ throw new GuardError(source, "guard.function", property, value);
354
+ }
355
+ }
356
+ /**
357
+ * Is the property a string formatted as an email address.
358
+ * @param source The source of the error.
359
+ * @param property The name of the property.
360
+ * @param value The value to test.
361
+ * @throws GuardError If the value does not match the assertion.
362
+ */
363
+ static email(source, property, value) {
364
+ if (!Is.email(value)) {
365
+ throw new GuardError(source, "guard.email", property, value);
366
+ }
367
+ }
368
+ }
369
+ //# sourceMappingURL=guards.js.map