@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 @@
1
+ {"version":3,"file":"guards.js","sourceRoot":"","sources":["../../../src/ut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AI,CAAC,CACzB,CAAC;YACH,CAAC;QACF,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,UAAU,CACvB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,kBAAkB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACnE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACH,8DAA8D;IACvD,MAAM,CAAC,QAAQ,CACrB,MAAc,EACd,QAAgB,EAChB,KAAc;QAEd,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAI,KAAK,CAAC,EAAE,CAAC;YAC5B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,gBAAgB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACnE,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACtB,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC9D,CAAC;IACF,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is } from \"./is.js\";\nimport { GuardError } from \"../errors/guardError.js\";\nimport { ArrayHelper } from \"../helpers/arrayHelper.js\";\nimport { HexHelper } from \"../helpers/hexHelper.js\";\nimport type { ObjectOrArray } from \"../models/objectOrArray.js\";\n\n/**\n * Class to handle guard operations for parameters.\n */\nexport class Guards {\n\t/**\n\t * Is the property defined.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static defined(source: string, property: string, value: unknown): asserts value {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.undefined\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static string(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringValue(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.string(value)) {\n\t\t\tthrow new GuardError(source, \"guard.string\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.stringEmpty\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a JSON value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static json(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.json(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringJson\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base64 url string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase64Url(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase64Url(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase64Url\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a base58 string.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringBase58(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is string {\n\t\tif (!Is.stringBase58(value)) {\n\t\t\tthrow new GuardError(source, \"guard.stringBase58\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHex(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringValue(source, property, value);\n\t\tif (!HexHelper.isHex(value, allowPrefix)) {\n\t\t\tthrow new GuardError(source, \"guard.stringHex\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string with a hex value with fixed length.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param length The length of the string to match.\n\t * @param allowPrefix Allow the hex to have the 0x prefix.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static stringHexLength(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tlength: number,\n\t\tallowPrefix: boolean = false\n\t): asserts value is string {\n\t\tGuards.stringHex(source, property, value, allowPrefix);\n\t\tif (HexHelper.stripPrefix(value).length !== length) {\n\t\t\tthrow new GuardError(\n\t\t\t\tsource,\n\t\t\t\t\"guard.stringHexLength\",\n\t\t\t\tproperty,\n\t\t\t\tvalue.length,\n\t\t\t\tlength.toString()\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a number.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static number(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.number(value)) {\n\t\t\tthrow new GuardError(source, \"guard.number\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an integer.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static integer(source: string, property: string, value: unknown): asserts value is number {\n\t\tif (!Is.integer(value)) {\n\t\t\tthrow new GuardError(source, \"guard.integer\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a bigint.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static bigint(source: string, property: string, value: unknown): asserts value is bigint {\n\t\tif (!Is.bigint(value)) {\n\t\t\tthrow new GuardError(source, \"guard.bigint\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a boolean.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static boolean(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is boolean {\n\t\tif (!Is.boolean(value)) {\n\t\t\tthrow new GuardError(source, \"guard.boolean\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a date.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static date(source: string, property: string, value: unknown): asserts value is Date {\n\t\tif (!Is.date(value)) {\n\t\t\tthrow new GuardError(source, \"guard.date\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in milliseconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampMilliseconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampMilliseconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampMilliseconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a timestamp in seconds.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static timestampSeconds(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is number {\n\t\tif (!Is.timestampSeconds(value)) {\n\t\t\tthrow new GuardError(source, \"guard.timestampSeconds\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property an object.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static object<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an object with at least one property.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static objectValue<T = { [id: string]: unknown }>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (Is.undefined(value)) {\n\t\t\tthrow new GuardError(source, \"guard.objectUndefined\", property, value);\n\t\t}\n\t\tif (!Is.object<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.object\", property, value);\n\t\t}\n\t\tif (Object.keys(value || {}).length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.objectValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static array<T>(source: string, property: string, value: unknown): asserts value is T[] {\n\t\tif (!Is.array<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property is an array with at least one item.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayValue<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T[] {\n\t\tif (!Is.array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new GuardError(source, \"guard.arrayValue\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property one of a list of items.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param options The options the value must be one of.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayOneOf<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: T,\n\t\toptions: T[]\n\t): asserts value is T {\n\t\tif (!Is.array<T>(options)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\t\tif (!options.includes(value)) {\n\t\t\tthrow new GuardError(source, \"guard.arrayOneOf\", property, value, options.join(\", \"));\n\t\t}\n\t}\n\n\t/**\n\t * Does the array start with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param startValues The values that must start the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayStartsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tstartValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst startValuesArray = ArrayHelper.fromObjectOrArray(startValues);\n\n\t\tif (!Is.arrayValue<T>(startValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, startValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < startValuesArray.length; i++) {\n\t\t\tif (value[i] !== startValuesArray[i]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayStartsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tstartValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Does the array end with the specified data.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @param endValues The values that must end the array.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static arrayEndsWith<T>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tendValues: ObjectOrArray<T>\n\t): asserts value is T[] {\n\t\tif (!Is.arrayValue<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, value);\n\t\t}\n\n\t\tconst endValuesArray = ArrayHelper.fromObjectOrArray(endValues);\n\n\t\tif (!Is.arrayValue<T>(endValuesArray)) {\n\t\t\tthrow new GuardError(source, \"guard.array\", property, endValuesArray);\n\t\t}\n\n\t\tfor (let i = 0; i < endValuesArray.length; i++) {\n\t\t\tif (value[value.length - i - 1] !== endValuesArray[endValuesArray.length - i - 1]) {\n\t\t\t\tthrow new GuardError(\n\t\t\t\t\tsource,\n\t\t\t\t\t\"guard.arrayEndsWith\",\n\t\t\t\t\tproperty,\n\t\t\t\t\tvalue,\n\t\t\t\t\tendValuesArray.join(\", \")\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a Uint8Array.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uint8Array(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is Uint8Array {\n\t\tif (!Is.uint8Array(value)) {\n\t\t\tthrow new GuardError(source, \"guard.uint8Array\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a function.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tpublic static function<T extends (...args: any[]) => any = (...args: any[]) => any>(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown\n\t): asserts value is T {\n\t\tif (!Is.function<T>(value)) {\n\t\t\tthrow new GuardError(source, \"guard.function\", property, value);\n\t\t}\n\t}\n\n\t/**\n\t * Is the property a string formatted as an email address.\n\t * @param source The source of the error.\n\t * @param property The name of the property.\n\t * @param value The value to test.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static email(source: string, property: string, value: unknown): asserts value is string {\n\t\tif (!Is.email(value)) {\n\t\t\tthrow new GuardError(source, \"guard.email\", property, value);\n\t\t}\n\t}\n}\n"]}
@@ -0,0 +1,243 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { IntlMessageFormat } from "intl-messageformat";
4
+ import { Is } from "./is.js";
5
+ import { SharedStore } from "./sharedStore.js";
6
+ /**
7
+ * Class to perform internationalization.
8
+ */
9
+ export class I18n {
10
+ /**
11
+ * The default translation.
12
+ */
13
+ static DEFAULT_LOCALE = "en";
14
+ /**
15
+ * Set the locale.
16
+ * @param locale The new locale.
17
+ */
18
+ static setLocale(locale) {
19
+ const i18nShared = I18n.getI18nShared();
20
+ i18nShared.currentLocale = locale;
21
+ for (const callback in i18nShared.localeChangedHandlers) {
22
+ i18nShared.localeChangedHandlers[callback](i18nShared.currentLocale);
23
+ }
24
+ }
25
+ /**
26
+ * Get the locale.
27
+ * @returns The current locale.
28
+ */
29
+ static getLocale() {
30
+ const i18nShared = I18n.getI18nShared();
31
+ return i18nShared.currentLocale;
32
+ }
33
+ /**
34
+ * Add a locale dictionary.
35
+ * @param locale The locale.
36
+ * @param dictionary The dictionary to add.
37
+ */
38
+ static addDictionary(locale, dictionary) {
39
+ const i18nShared = I18n.getI18nShared();
40
+ const mergedKeys = {};
41
+ I18n.flattenTranslationKeys(dictionary, "", mergedKeys);
42
+ i18nShared.localeDictionaries[locale] = mergedKeys;
43
+ for (const callback in i18nShared.dictionaryChangedHandlers) {
44
+ i18nShared.dictionaryChangedHandlers[callback](i18nShared.currentLocale);
45
+ }
46
+ }
47
+ /**
48
+ * Get a locale dictionary.
49
+ * @param locale The locale.
50
+ * @returns The dictionary of undefined if it does not exist.
51
+ */
52
+ static getDictionary(locale) {
53
+ const i18nShared = I18n.getI18nShared();
54
+ return i18nShared.localeDictionaries[locale];
55
+ }
56
+ /**
57
+ * Get all the locale dictionaries.
58
+ * @returns The dictionaries.
59
+ */
60
+ static getAllDictionaries() {
61
+ const i18nShared = I18n.getI18nShared();
62
+ return i18nShared.localeDictionaries;
63
+ }
64
+ /**
65
+ * Add a locale changed handler.
66
+ * @param id The id of the handler.
67
+ * @param handler The handler to add.
68
+ */
69
+ static addLocaleHandler(id, handler) {
70
+ const i18nShared = I18n.getI18nShared();
71
+ i18nShared.localeChangedHandlers[id] = handler;
72
+ }
73
+ /**
74
+ * Remove a locale changed handler.
75
+ * @param id The id of the handler.
76
+ */
77
+ static removeLocaleHandler(id) {
78
+ const i18nShared = I18n.getI18nShared();
79
+ delete i18nShared.localeChangedHandlers[id];
80
+ }
81
+ /**
82
+ * Add a dictionary changed handler.
83
+ * @param id The id of the handler.
84
+ * @param handler The handler to add.
85
+ */
86
+ static addDictionaryHandler(id, handler) {
87
+ const i18nShared = I18n.getI18nShared();
88
+ i18nShared.dictionaryChangedHandlers[id] = handler;
89
+ }
90
+ /**
91
+ * Remove a dictionary changed handler.
92
+ * @param id The id of the handler.
93
+ */
94
+ static removeDictionaryHandler(id) {
95
+ const i18nShared = I18n.getI18nShared();
96
+ delete i18nShared.dictionaryChangedHandlers[id];
97
+ }
98
+ /**
99
+ * Format a message.
100
+ * @param key The key of the message to format.
101
+ * @param values The values to substitute into the message.
102
+ * @param overrideLocale Override the locale.
103
+ * @returns The formatted string.
104
+ */
105
+ static formatMessage(key, values, overrideLocale) {
106
+ const i18nShared = I18n.getI18nShared();
107
+ let cl = overrideLocale ?? i18nShared.currentLocale;
108
+ if (cl.startsWith("debug-")) {
109
+ cl = I18n.DEFAULT_LOCALE;
110
+ }
111
+ if (!i18nShared.localeDictionaries[cl]) {
112
+ return `!!Missing ${cl}`;
113
+ }
114
+ if (!i18nShared.localeDictionaries[cl][key]) {
115
+ return `!!Missing ${cl}.${key}`;
116
+ }
117
+ if (i18nShared.currentLocale === "debug-k") {
118
+ return key;
119
+ }
120
+ try {
121
+ let ret = new IntlMessageFormat(i18nShared.localeDictionaries[cl][key], cl).format(values);
122
+ if (i18nShared.currentLocale === "debug-x") {
123
+ ret = ret.replace(/[a-z]/g, "x").replace(/[A-Z]/g, "x").replace(/\d/g, "n");
124
+ }
125
+ return ret;
126
+ }
127
+ catch {
128
+ // if there is an error the assumption is that one of the properties
129
+ // is undefined, which the formatting library does not like
130
+ // in this case we get all the property names and ensure they are defined
131
+ const propertyNames = I18n.getPropertyNames(i18nShared.localeDictionaries[cl][key]);
132
+ const safeValues = values ?? {};
133
+ for (const propertyName of propertyNames) {
134
+ safeValues[propertyName] ??= "";
135
+ }
136
+ // Format again with the safe values, if it still fails we let the error
137
+ // propagate up as there is nothing more we can do
138
+ return new IntlMessageFormat(i18nShared.localeDictionaries[cl][key], cl).format(safeValues);
139
+ }
140
+ }
141
+ /**
142
+ * Check if the dictionaries have a message for the given key.
143
+ * @param key The key to check for existence.
144
+ * @returns True if the key exists.
145
+ */
146
+ static hasMessage(key) {
147
+ const i18nShared = I18n.getI18nShared();
148
+ return Is.string(i18nShared.localeDictionaries[i18nShared.currentLocale]?.[key]);
149
+ }
150
+ /**
151
+ * Flatten the translation property paths for faster lookup.
152
+ * @param translation The translation to merge.
153
+ * @param propertyPath The current root path.
154
+ * @param mergedKeys The merged keys dictionary to populate.
155
+ */
156
+ static flattenTranslationKeys(translation, propertyPath, mergedKeys) {
157
+ for (const key in translation) {
158
+ const val = translation[key];
159
+ const mergedPath = propertyPath.length > 0 ? `${propertyPath}.${key}` : key;
160
+ if (Is.string(val)) {
161
+ mergedKeys[mergedPath] = val;
162
+ }
163
+ else if (Is.object(val)) {
164
+ I18n.flattenTranslationKeys(val, mergedPath, mergedKeys);
165
+ }
166
+ }
167
+ }
168
+ /**
169
+ * Get a list of the property names from the message.
170
+ * @param message The message to extract the property names from.
171
+ * @returns The list of property names.
172
+ */
173
+ static getPropertyNames(message) {
174
+ const i18nShared = I18n.getI18nShared();
175
+ try {
176
+ const ast = new IntlMessageFormat(message, i18nShared.currentLocale).getAst();
177
+ const properties = new Set();
178
+ I18n.extractPropertiesFromAst(ast, properties);
179
+ return Array.from(properties);
180
+ }
181
+ catch {
182
+ return [];
183
+ }
184
+ }
185
+ /**
186
+ * Recursively extract property names from the AST.
187
+ * @param node The AST node to process.
188
+ * @param properties The set to collect property names.
189
+ * @internal
190
+ */
191
+ static extractPropertiesFromAst(node, properties) {
192
+ if (Is.array(node)) {
193
+ for (const item of node) {
194
+ I18n.extractPropertiesFromAst(item, properties);
195
+ }
196
+ }
197
+ else if (Is.object(node)) {
198
+ const obj = node;
199
+ // Check for elements that have property names
200
+ // Type 1 = ArgumentElement (simple placeholder like {name})
201
+ // Type 2 = NumberElement ({count, number})
202
+ // Type 3 = DateElement ({date, date, short})
203
+ // Type 4 = TimeElement ({time, time, medium})
204
+ // Type 5 = SelectElement ({gender, select, ...})
205
+ // Type 6 = PluralElement ({count, plural, ...})
206
+ if ((obj.type === 1 ||
207
+ obj.type === 2 ||
208
+ obj.type === 3 ||
209
+ obj.type === 4 ||
210
+ obj.type === 5 ||
211
+ obj.type === 6) &&
212
+ Is.string(obj.value)) {
213
+ properties.add(obj.value);
214
+ }
215
+ // Recursively process all object properties
216
+ for (const key in obj) {
217
+ const value = obj[key];
218
+ if (Is.object(value) || Is.array(value)) {
219
+ I18n.extractPropertiesFromAst(value, properties);
220
+ }
221
+ }
222
+ }
223
+ }
224
+ /**
225
+ * Get the I18n shared data.
226
+ * @returns The I18n shared data.
227
+ * @internal
228
+ */
229
+ static getI18nShared() {
230
+ let i18nShared = SharedStore.get("i18n");
231
+ if (Is.undefined(i18nShared)) {
232
+ i18nShared = {
233
+ localeDictionaries: {},
234
+ currentLocale: I18n.DEFAULT_LOCALE,
235
+ localeChangedHandlers: {},
236
+ dictionaryChangedHandlers: {}
237
+ };
238
+ SharedStore.set("i18n", i18nShared);
239
+ }
240
+ return i18nShared;
241
+ }
242
+ }
243
+ //# sourceMappingURL=i18n.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"i18n.js","sourceRoot":"","sources":["../../../src/utils/i18n.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAI/C;;GAEG;AACH,MAAM,OAAO,IAAI;IAChB;;OAEG;IACI,MAAM,CAAC,cAAc,GAAW,IAAI,CAAC;IAE5C;;;OAGG;IACI,MAAM,CAAC,SAAS,CAAC,MAAc;QACrC,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QAExC,UAAU,CAAC,aAAa,GAAG,MAAM,CAAC;QAElC,KAAK,MAAM,QAAQ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B,aAAa,EAAE,IAAI,CAAC,cAAc;gBAClC,qBAAqB,EAAE,EAAE;gBACzB,yBAAyB,EAAE,EAAE;aAC7B,CAAC;YACF,WAAW,CAAC,GAAG,CAAc,MAAM,EAAE,UAAU,CAAC,CAAC;QAClD,CAAC;QAED,OAAO,UAAU,CAAC;IACnB,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { IntlMessageFormat } from \"intl-messageformat\";\nimport { Is } from \"./is.js\";\nimport { SharedStore } from \"./sharedStore.js\";\nimport type { II18nShared } from \"../models/II18nShared.js\";\nimport type { ILocaleDictionary } from \"../models/ILocaleDictionary.js\";\n\n/**\n * Class to perform internationalization.\n */\nexport class I18n {\n\t/**\n\t * The default translation.\n\t */\n\tpublic static DEFAULT_LOCALE: string = \"en\";\n\n\t/**\n\t * Set the locale.\n\t * @param locale The new locale.\n\t */\n\tpublic static setLocale(locale: string): void {\n\t\tconst i18nShared = I18n.getI18nShared();\n\n\t\ti18nShared.currentLocale = locale;\n\n\t\tfor (const callback in i18nShared.localeChangedHandlers) {\n\t\t\ti18nShared.localeChangedHandlers[callback](i18nShared.currentLocale);\n\t\t}\n\t}\n\n\t/**\n\t * Get the locale.\n\t * @returns The current locale.\n\t */\n\tpublic static getLocale(): string {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\treturn i18nShared.currentLocale;\n\t}\n\n\t/**\n\t * Add a locale dictionary.\n\t * @param locale The locale.\n\t * @param dictionary The dictionary to add.\n\t */\n\tpublic static addDictionary(locale: string, dictionary: ILocaleDictionary): void {\n\t\tconst i18nShared = I18n.getI18nShared();\n\n\t\tconst mergedKeys: { [key: string]: string } = {};\n\t\tI18n.flattenTranslationKeys(dictionary, \"\", mergedKeys);\n\t\ti18nShared.localeDictionaries[locale] = mergedKeys;\n\n\t\tfor (const callback in i18nShared.dictionaryChangedHandlers) {\n\t\t\ti18nShared.dictionaryChangedHandlers[callback](i18nShared.currentLocale);\n\t\t}\n\t}\n\n\t/**\n\t * Get a locale dictionary.\n\t * @param locale The locale.\n\t * @returns The dictionary of undefined if it does not exist.\n\t */\n\tpublic static getDictionary(locale: string): { [key: string]: string } | undefined {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\treturn i18nShared.localeDictionaries[locale];\n\t}\n\n\t/**\n\t * Get all the locale dictionaries.\n\t * @returns The dictionaries.\n\t */\n\tpublic static getAllDictionaries(): {\n\t\t[locale: string]: { [key: string]: string };\n\t} {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\treturn i18nShared.localeDictionaries;\n\t}\n\n\t/**\n\t * Add a locale changed handler.\n\t * @param id The id of the handler.\n\t * @param handler The handler to add.\n\t */\n\tpublic static addLocaleHandler(id: string, handler: (locale: string) => void): void {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\ti18nShared.localeChangedHandlers[id] = handler;\n\t}\n\n\t/**\n\t * Remove a locale changed handler.\n\t * @param id The id of the handler.\n\t */\n\tpublic static removeLocaleHandler(id: string): void {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\tdelete i18nShared.localeChangedHandlers[id];\n\t}\n\n\t/**\n\t * Add a dictionary changed handler.\n\t * @param id The id of the handler.\n\t * @param handler The handler to add.\n\t */\n\tpublic static addDictionaryHandler(id: string, handler: (locale: string) => void): void {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\ti18nShared.dictionaryChangedHandlers[id] = handler;\n\t}\n\n\t/**\n\t * Remove a dictionary changed handler.\n\t * @param id The id of the handler.\n\t */\n\tpublic static removeDictionaryHandler(id: string): void {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\tdelete i18nShared.dictionaryChangedHandlers[id];\n\t}\n\n\t/**\n\t * Format a message.\n\t * @param key The key of the message to format.\n\t * @param values The values to substitute into the message.\n\t * @param overrideLocale Override the locale.\n\t * @returns The formatted string.\n\t */\n\tpublic static formatMessage(\n\t\tkey: string,\n\t\tvalues?: { [key: string]: unknown },\n\t\toverrideLocale?: string\n\t): string {\n\t\tconst i18nShared = I18n.getI18nShared();\n\n\t\tlet cl = overrideLocale ?? i18nShared.currentLocale;\n\t\tif (cl.startsWith(\"debug-\")) {\n\t\t\tcl = I18n.DEFAULT_LOCALE;\n\t\t}\n\n\t\tif (!i18nShared.localeDictionaries[cl]) {\n\t\t\treturn `!!Missing ${cl}`;\n\t\t}\n\t\tif (!i18nShared.localeDictionaries[cl][key]) {\n\t\t\treturn `!!Missing ${cl}.${key}`;\n\t\t}\n\n\t\tif (i18nShared.currentLocale === \"debug-k\") {\n\t\t\treturn key;\n\t\t}\n\n\t\ttry {\n\t\t\tlet ret = new IntlMessageFormat(i18nShared.localeDictionaries[cl][key], cl).format(\n\t\t\t\tvalues\n\t\t\t) as string;\n\n\t\t\tif (i18nShared.currentLocale === \"debug-x\") {\n\t\t\t\tret = ret.replace(/[a-z]/g, \"x\").replace(/[A-Z]/g, \"x\").replace(/\\d/g, \"n\");\n\t\t\t}\n\n\t\t\treturn ret;\n\t\t} catch {\n\t\t\t// if there is an error the assumption is that one of the properties\n\t\t\t// is undefined, which the formatting library does not like\n\t\t\t// in this case we get all the property names and ensure they are defined\n\t\t\tconst propertyNames = I18n.getPropertyNames(i18nShared.localeDictionaries[cl][key]);\n\t\t\tconst safeValues: { [key: string]: unknown } = values ?? {};\n\n\t\t\tfor (const propertyName of propertyNames) {\n\t\t\t\tsafeValues[propertyName] ??= \"\";\n\t\t\t}\n\n\t\t\t// Format again with the safe values, if it still fails we let the error\n\t\t\t// propagate up as there is nothing more we can do\n\t\t\treturn new IntlMessageFormat(i18nShared.localeDictionaries[cl][key], cl).format(\n\t\t\t\tsafeValues\n\t\t\t) as string;\n\t\t}\n\t}\n\n\t/**\n\t * Check if the dictionaries have a message for the given key.\n\t * @param key The key to check for existence.\n\t * @returns True if the key exists.\n\t */\n\tpublic static hasMessage(key: string): boolean {\n\t\tconst i18nShared = I18n.getI18nShared();\n\t\treturn Is.string(i18nShared.localeDictionaries[i18nShared.currentLocale]?.[key]);\n\t}\n\n\t/**\n\t * Flatten the translation property paths for faster lookup.\n\t * @param translation The translation to merge.\n\t * @param propertyPath The current root path.\n\t * @param mergedKeys The merged keys dictionary to populate.\n\t */\n\tpublic static flattenTranslationKeys(\n\t\ttranslation: ILocaleDictionary,\n\t\tpropertyPath: string,\n\t\tmergedKeys: { [key: string]: string }\n\t): void {\n\t\tfor (const key in translation) {\n\t\t\tconst val = translation[key];\n\t\t\tconst mergedPath = propertyPath.length > 0 ? `${propertyPath}.${key}` : key;\n\t\t\tif (Is.string(val)) {\n\t\t\t\tmergedKeys[mergedPath] = val;\n\t\t\t} else if (Is.object<ILocaleDictionary>(val)) {\n\t\t\t\tI18n.flattenTranslationKeys(val, mergedPath, mergedKeys);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Get a list of the property names from the message.\n\t * @param message The message to extract the property names from.\n\t * @returns The list of property names.\n\t */\n\tpublic static getPropertyNames(message: string): string[] {\n\t\tconst i18nShared = I18n.getI18nShared();\n\n\t\ttry {\n\t\t\tconst ast = new IntlMessageFormat(message, i18nShared.currentLocale).getAst();\n\t\t\tconst properties = new Set<string>();\n\n\t\t\tI18n.extractPropertiesFromAst(ast, properties);\n\n\t\t\treturn Array.from(properties);\n\t\t} catch {\n\t\t\treturn [];\n\t\t}\n\t}\n\n\t/**\n\t * Recursively extract property names from the AST.\n\t * @param node The AST node to process.\n\t * @param properties The set to collect property names.\n\t * @internal\n\t */\n\tprivate static extractPropertiesFromAst(node: unknown, properties: Set<string>): void {\n\t\tif (Is.array(node)) {\n\t\t\tfor (const item of node) {\n\t\t\t\tI18n.extractPropertiesFromAst(item, properties);\n\t\t\t}\n\t\t} else if (Is.object(node)) {\n\t\t\tconst obj = node as { [key: string]: unknown };\n\n\t\t\t// Check for elements that have property names\n\t\t\t// Type 1 = ArgumentElement (simple placeholder like {name})\n\t\t\t// Type 2 = NumberElement ({count, number})\n\t\t\t// Type 3 = DateElement ({date, date, short})\n\t\t\t// Type 4 = TimeElement ({time, time, medium})\n\t\t\t// Type 5 = SelectElement ({gender, select, ...})\n\t\t\t// Type 6 = PluralElement ({count, plural, ...})\n\t\t\tif (\n\t\t\t\t(obj.type === 1 ||\n\t\t\t\t\tobj.type === 2 ||\n\t\t\t\t\tobj.type === 3 ||\n\t\t\t\t\tobj.type === 4 ||\n\t\t\t\t\tobj.type === 5 ||\n\t\t\t\t\tobj.type === 6) &&\n\t\t\t\tIs.string(obj.value)\n\t\t\t) {\n\t\t\t\tproperties.add(obj.value);\n\t\t\t}\n\n\t\t\t// Recursively process all object properties\n\t\t\tfor (const key in obj) {\n\t\t\t\tconst value = obj[key];\n\t\t\t\tif (Is.object(value) || Is.array(value)) {\n\t\t\t\t\tI18n.extractPropertiesFromAst(value, properties);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Get the I18n shared data.\n\t * @returns The I18n shared data.\n\t * @internal\n\t */\n\tprivate static getI18nShared(): II18nShared {\n\t\tlet i18nShared = SharedStore.get<II18nShared>(\"i18n\");\n\n\t\tif (Is.undefined(i18nShared)) {\n\t\t\ti18nShared = {\n\t\t\t\tlocaleDictionaries: {},\n\t\t\t\tcurrentLocale: I18n.DEFAULT_LOCALE,\n\t\t\t\tlocaleChangedHandlers: {},\n\t\t\t\tdictionaryChangedHandlers: {}\n\t\t\t};\n\t\t\tSharedStore.set<II18nShared>(\"i18n\", i18nShared);\n\t\t}\n\n\t\treturn i18nShared;\n\t}\n}\n"]}
@@ -0,0 +1,351 @@
1
+ // Copyright 2024 IOTA Stiftung.
2
+ // SPDX-License-Identifier: Apache-2.0.
3
+ import { HexHelper } from "../helpers/hexHelper.js";
4
+ /**
5
+ * Class to check types of objects.
6
+ */
7
+ export class Is {
8
+ /**
9
+ * Is the property undefined.
10
+ * @param value The value to test.
11
+ * @returns True if the value is a empty.
12
+ */
13
+ static undefined(value) {
14
+ return value === undefined;
15
+ }
16
+ /**
17
+ * Is the property null.
18
+ * @param value The value to test.
19
+ * @returns True if the value is a empty.
20
+ */
21
+ static null(value) {
22
+ return value === null;
23
+ }
24
+ /**
25
+ * Is the property null or undefined.
26
+ * @param value The value to test.
27
+ * @returns True if the value is a empty.
28
+ */
29
+ static empty(value) {
30
+ return value === null || value === undefined;
31
+ }
32
+ /**
33
+ * Is the property is not null or undefined.
34
+ * @param value The value to test.
35
+ * @returns True if the value is a not empty.
36
+ */
37
+ static notEmpty(value) {
38
+ return value !== null && value !== undefined;
39
+ }
40
+ /**
41
+ * Is the value a string.
42
+ * @param value The value to test.
43
+ * @returns True if the value is a string.
44
+ */
45
+ static string(value) {
46
+ return typeof value === "string";
47
+ }
48
+ /**
49
+ * Is the value a string.
50
+ * @param value The value to test.
51
+ * @returns True if the value is a string.
52
+ */
53
+ static stringValue(value) {
54
+ return Is.string(value) && value.trim().length > 0;
55
+ }
56
+ /**
57
+ * Is the value a JSON string.
58
+ * @param value The value to test.
59
+ * @returns True if the value is a JSON string.
60
+ */
61
+ static json(value) {
62
+ if (!Is.stringValue(value)) {
63
+ return false;
64
+ }
65
+ try {
66
+ const json = JSON.parse(value);
67
+ return (Is.object(json) ||
68
+ Is.array(json) ||
69
+ Is.string(json) ||
70
+ Is.number(json) ||
71
+ Is.boolean(json) ||
72
+ Is.null(json));
73
+ }
74
+ catch {
75
+ return false;
76
+ }
77
+ }
78
+ /**
79
+ * Is the value a base64 string.
80
+ * @param value The value to test.
81
+ * @returns True if the value is a base64 string.
82
+ */
83
+ static stringBase64(value) {
84
+ return (Is.stringValue(value) &&
85
+ // eslint-disable-next-line unicorn/better-regex
86
+ /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value));
87
+ }
88
+ /**
89
+ * Is the value a base64 url string.
90
+ * @param value The value to test.
91
+ * @returns True if the value is a base64 string.
92
+ */
93
+ static stringBase64Url(value) {
94
+ return (Is.stringValue(value) &&
95
+ // eslint-disable-next-line unicorn/better-regex
96
+ /^([A-Za-z0-9-_])*$/.test(value));
97
+ }
98
+ /**
99
+ * Is the value a base58 string.
100
+ * @param value The value to test.
101
+ * @returns True if the value is a base58 string.
102
+ */
103
+ static stringBase58(value) {
104
+ return (Is.stringValue(value) &&
105
+ // eslint-disable-next-line unicorn/better-regex
106
+ /^[A-HJ-NP-Za-km-z1-9]*$/.test(value));
107
+ }
108
+ /**
109
+ * Is the value a hex string.
110
+ * @param value The value to test.
111
+ * @param allowPrefix Allow the hex to have the 0x prefix.
112
+ * @returns True if the value is a hex string.
113
+ */
114
+ static stringHex(value, allowPrefix = false) {
115
+ return Is.string(value) && HexHelper.isHex(value, allowPrefix);
116
+ }
117
+ /**
118
+ * Is the value a hex string of fixed length.
119
+ * @param value The value to test.
120
+ * @param length The length to test.
121
+ * @param allowPrefix Allow the hex to have the 0x prefix.
122
+ * @returns True if the value is a hex string of required length.
123
+ */
124
+ static stringHexLength(value, length, allowPrefix = false) {
125
+ return Is.stringHex(value, allowPrefix) && value.length === length;
126
+ }
127
+ /**
128
+ * Is the value a number.
129
+ * @param value The value to test.
130
+ * @returns True if the value is a number.
131
+ */
132
+ static number(value) {
133
+ return typeof value === "number" && Number.isFinite(value) && !Number.isNaN(value);
134
+ }
135
+ /**
136
+ * Is the value an integer.
137
+ * @param value The value to test.
138
+ * @returns True if the value is an integer.
139
+ */
140
+ static integer(value) {
141
+ return Is.number(value) && Number.isInteger(value);
142
+ }
143
+ /**
144
+ * Is the value a big integer.
145
+ * @param value The value to test.
146
+ * @returns True if the value is a big integer.
147
+ */
148
+ static bigint(value) {
149
+ return typeof value === "bigint";
150
+ }
151
+ /**
152
+ * Is the value a boolean.
153
+ * @param value The value to test.
154
+ * @returns True if the value is a boolean.
155
+ */
156
+ static boolean(value) {
157
+ return typeof value === "boolean";
158
+ }
159
+ /**
160
+ * Is the value a date.
161
+ * @param value The value to test.
162
+ * @returns True if the value is a date.
163
+ */
164
+ static date(value) {
165
+ return (Object.prototype.toString.call(value) === "[object Date]" &&
166
+ !Number.isNaN(value.getTime()));
167
+ }
168
+ /**
169
+ * Is the value an empty date.
170
+ * @param value The value to test.
171
+ * @returns True if the value is an empty date.
172
+ */
173
+ static dateEmpty(value) {
174
+ return (Object.prototype.toString.call(value) === "[object Date]" &&
175
+ Number.isNaN(value.getTime()));
176
+ }
177
+ /**
178
+ * Is the value a date string.
179
+ * @param value The value to test.
180
+ * @returns True if the value is a string in ISO 8601 date format.
181
+ */
182
+ static dateString(value) {
183
+ if (typeof value !== "string" || value.length === 0 || value.includes("T")) {
184
+ return false;
185
+ }
186
+ return !Number.isNaN(Date.parse(value));
187
+ }
188
+ /**
189
+ * Is the value a date string.
190
+ * @param value The value to test.
191
+ * @returns True if the value is a string in ISO 8601 date/time format.
192
+ */
193
+ static dateTimeString(value) {
194
+ if (typeof value !== "string" || value.length === 0 || !value.includes("T")) {
195
+ return false;
196
+ }
197
+ return !Number.isNaN(Date.parse(value));
198
+ }
199
+ /**
200
+ * Is the value a time string.
201
+ * @param value The value to test.
202
+ * @returns True if the value is a string in ISO 8601 time format.
203
+ */
204
+ static timeString(value) {
205
+ if (typeof value !== "string" || value.length === 0 || value.includes("T")) {
206
+ return false;
207
+ }
208
+ return !Number.isNaN(Date.parse(`1970-01-01T${value}`));
209
+ }
210
+ /**
211
+ * Is the value a timestamp in seconds.
212
+ * @param value The value to test.
213
+ * @returns True if the value is a date.
214
+ */
215
+ static timestampSeconds(value) {
216
+ if (!Is.integer(value)) {
217
+ return false;
218
+ }
219
+ return value.toString().length < 12;
220
+ }
221
+ /**
222
+ * Is the value a timestamp in milliseconds.
223
+ * @param value The value to test.
224
+ * @returns True if the value is a date.
225
+ */
226
+ static timestampMilliseconds(value) {
227
+ if (!Is.integer(value)) {
228
+ return false;
229
+ }
230
+ return value.toString().length >= 12;
231
+ }
232
+ /**
233
+ * Is the value an object.
234
+ * @param value The value to test.
235
+ * @returns True if the value is a object.
236
+ */
237
+ static object(value) {
238
+ return typeof value === "object" && value !== null && !Array.isArray(value);
239
+ }
240
+ /**
241
+ * Is the value an object with at least one property.
242
+ * @param value The value to test.
243
+ * @returns True if the value is a object.
244
+ */
245
+ static objectValue(value) {
246
+ return (typeof value === "object" &&
247
+ value !== null &&
248
+ !Array.isArray(value) &&
249
+ Object.keys(value).length > 0);
250
+ }
251
+ /**
252
+ * Is the value an array.
253
+ * @param value The value to test.
254
+ * @returns True if the value is an array.
255
+ */
256
+ static array(value) {
257
+ return Array.isArray(value);
258
+ }
259
+ /**
260
+ * Is the value an array with at least one element.
261
+ * @param value The value to test.
262
+ * @returns True if the value is an array with at least one element.
263
+ */
264
+ static arrayValue(value) {
265
+ return Array.isArray(value) && value.length > 0;
266
+ }
267
+ /**
268
+ * Is the value an array with at least one element.
269
+ * @param value The value to test.
270
+ * @param options The options the value must be one of.
271
+ * @returns True if the value is an element from the options array.
272
+ */
273
+ static arrayOneOf(value, options) {
274
+ if (Is.empty(value) || !Is.array(options) || !options.includes(value)) {
275
+ return false;
276
+ }
277
+ return true;
278
+ }
279
+ /**
280
+ * Is the value a Uint8Array.
281
+ * @param value The value to test.
282
+ * @returns True if the value is a Uint8Array.
283
+ */
284
+ static uint8Array(value) {
285
+ // This is the only way we can reliably check for Uint8Array
286
+ // eslint-disable-next-line no-restricted-syntax
287
+ return value instanceof Uint8Array;
288
+ }
289
+ /**
290
+ * Is the value a TypedArray.
291
+ * @param value The value to test.
292
+ * @returns True if the value is a TypedArray.
293
+ */
294
+ static typedArray(value) {
295
+ // This is the only way we can reliably check for TypedArray
296
+ // eslint-disable-next-line no-restricted-syntax
297
+ return value instanceof Object.getPrototypeOf(Uint8Array);
298
+ }
299
+ /**
300
+ * Is the property a function.
301
+ * @param value The value to test.
302
+ * @returns True if the value is a function.
303
+ */
304
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
305
+ static function(value) {
306
+ return typeof value === "function";
307
+ }
308
+ /**
309
+ * Is the value a string formatted as an email address.
310
+ * @param value The value to test.
311
+ * @returns True if the value is a string.
312
+ */
313
+ static email(value) {
314
+ return (Is.stringValue(value) &&
315
+ /^[\w!#$%&'*+./=?^`{|}~-]+@[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?)*$/.test(value));
316
+ }
317
+ /**
318
+ * Is the value a promise.
319
+ * @param value The value to test.
320
+ * @returns True if the value is a promise.
321
+ */
322
+ static promise(value) {
323
+ // This is the only way we can reliably check for Promise
324
+ // eslint-disable-next-line no-restricted-syntax
325
+ return value instanceof Promise;
326
+ }
327
+ /**
328
+ * Is the value a regexp.
329
+ * @param value The value to test.
330
+ * @returns True if the value is a regexp.
331
+ */
332
+ static regexp(value) {
333
+ // This is the only way we can reliably check for RegExp
334
+ // eslint-disable-next-line no-restricted-syntax
335
+ return value instanceof RegExp;
336
+ }
337
+ /**
338
+ * Is the provided object a class constructor.
339
+ * @param obj The object to check.
340
+ * @returns True if the object is a class, false otherwise.
341
+ */
342
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
343
+ static class(obj) {
344
+ if (typeof obj !== "function") {
345
+ return false;
346
+ }
347
+ const str = Function.prototype.toString.call(obj);
348
+ return /^class\s/.test(str);
349
+ }
350
+ }
351
+ //# sourceMappingURL=is.js.map