@twin.org/core 0.0.2-next.9 → 0.0.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.
- package/README.md +1 -9
- package/dist/es/encoding/base32.js +75 -0
- package/dist/es/encoding/base32.js.map +1 -0
- package/dist/es/encoding/base58.js +124 -0
- package/dist/es/encoding/base58.js.map +1 -0
- package/dist/es/encoding/base64.js +231 -0
- package/dist/es/encoding/base64.js.map +1 -0
- package/dist/es/encoding/base64Url.js +42 -0
- package/dist/es/encoding/base64Url.js.map +1 -0
- package/dist/es/errors/alreadyExistsError.js +22 -0
- package/dist/es/errors/alreadyExistsError.js.map +1 -0
- package/dist/es/errors/baseError.js +257 -0
- package/dist/es/errors/baseError.js.map +1 -0
- package/dist/es/errors/conflictError.js +23 -0
- package/dist/es/errors/conflictError.js.map +1 -0
- package/dist/es/errors/generalError.js +21 -0
- package/dist/es/errors/generalError.js.map +1 -0
- package/dist/es/errors/guardError.js +27 -0
- package/dist/es/errors/guardError.js.map +1 -0
- package/dist/es/errors/notFoundError.js +22 -0
- package/dist/es/errors/notFoundError.js.map +1 -0
- package/dist/es/errors/notImplementedError.js +24 -0
- package/dist/es/errors/notImplementedError.js.map +1 -0
- package/dist/es/errors/notSupportedError.js +21 -0
- package/dist/es/errors/notSupportedError.js.map +1 -0
- package/dist/es/errors/unauthorizedError.js +21 -0
- package/dist/es/errors/unauthorizedError.js.map +1 -0
- package/dist/es/errors/unprocessableError.js +21 -0
- package/dist/es/errors/unprocessableError.js.map +1 -0
- package/dist/es/errors/validationError.js +23 -0
- package/dist/es/errors/validationError.js.map +1 -0
- package/dist/es/factories/componentFactory.js +9 -0
- package/dist/es/factories/componentFactory.js.map +1 -0
- package/dist/es/factories/factory.js +297 -0
- package/dist/es/factories/factory.js.map +1 -0
- package/dist/es/helpers/arrayHelper.js +46 -0
- package/dist/es/helpers/arrayHelper.js.map +1 -0
- package/dist/es/helpers/envHelper.js +39 -0
- package/dist/es/helpers/envHelper.js.map +1 -0
- package/dist/es/helpers/errorHelper.js +100 -0
- package/dist/es/helpers/errorHelper.js.map +1 -0
- package/dist/es/helpers/filenameHelper.js +32 -0
- package/dist/es/helpers/filenameHelper.js.map +1 -0
- package/dist/es/helpers/hexHelper.js +45 -0
- package/dist/es/helpers/hexHelper.js.map +1 -0
- package/dist/es/helpers/jsonHelper.js +163 -0
- package/dist/es/helpers/jsonHelper.js.map +1 -0
- package/dist/es/helpers/numberHelper.js +30 -0
- package/dist/es/helpers/numberHelper.js.map +1 -0
- package/dist/es/helpers/objectHelper.js +309 -0
- package/dist/es/helpers/objectHelper.js.map +1 -0
- package/dist/es/helpers/randomHelper.js +66 -0
- package/dist/es/helpers/randomHelper.js.map +1 -0
- package/dist/es/helpers/stringHelper.js +252 -0
- package/dist/es/helpers/stringHelper.js.map +1 -0
- package/dist/es/helpers/uint8ArrayHelper.js +26 -0
- package/dist/es/helpers/uint8ArrayHelper.js.map +1 -0
- package/dist/es/index.js +60 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/models/IComponent.js +2 -0
- package/dist/es/models/IComponent.js.map +1 -0
- package/dist/es/models/IError.js +4 -0
- package/dist/es/models/IError.js.map +1 -0
- package/dist/es/models/IHealth.js +2 -0
- package/dist/es/models/IHealth.js.map +1 -0
- package/dist/es/models/II18nShared.js +2 -0
- package/dist/es/models/II18nShared.js.map +1 -0
- package/dist/es/models/IKeyValue.js +2 -0
- package/dist/es/models/IKeyValue.js.map +1 -0
- package/dist/es/models/ILabelledValue.js +2 -0
- package/dist/es/models/ILabelledValue.js.map +1 -0
- package/dist/es/models/ILocale.js +2 -0
- package/dist/es/models/ILocale.js.map +1 -0
- package/dist/es/models/ILocaleDictionary.js +4 -0
- package/dist/es/models/ILocaleDictionary.js.map +1 -0
- package/dist/es/models/ILocalesIndex.js +2 -0
- package/dist/es/models/ILocalesIndex.js.map +1 -0
- package/dist/es/models/IPatchOperation.js +4 -0
- package/dist/es/models/IPatchOperation.js.map +1 -0
- package/dist/es/models/IUrlParts.js +4 -0
- package/dist/es/models/IUrlParts.js.map +1 -0
- package/dist/es/models/IValidationFailure.js +2 -0
- package/dist/es/models/IValidationFailure.js.map +1 -0
- package/dist/es/models/coerceType.js +49 -0
- package/dist/es/models/coerceType.js.map +1 -0
- package/dist/es/models/compressionType.js +17 -0
- package/dist/es/models/compressionType.js.map +1 -0
- package/dist/es/models/healthStatus.js +21 -0
- package/dist/es/models/healthStatus.js.map +1 -0
- package/dist/es/types/bitString.js +98 -0
- package/dist/es/types/bitString.js.map +1 -0
- package/dist/es/types/objectOrArray.js +2 -0
- package/dist/es/types/objectOrArray.js.map +1 -0
- package/dist/es/types/singleOccurrenceArray.js +2 -0
- package/dist/es/types/singleOccurrenceArray.js.map +1 -0
- package/dist/es/types/url.js +144 -0
- package/dist/es/types/url.js.map +1 -0
- package/dist/es/types/urn.js +211 -0
- package/dist/es/types/urn.js.map +1 -0
- package/dist/es/utils/asyncCache.js +206 -0
- package/dist/es/utils/asyncCache.js.map +1 -0
- package/dist/es/utils/coerce.js +269 -0
- package/dist/es/utils/coerce.js.map +1 -0
- package/dist/es/utils/compression.js +49 -0
- package/dist/es/utils/compression.js.map +1 -0
- package/dist/es/utils/converter.js +256 -0
- package/dist/es/utils/converter.js.map +1 -0
- package/dist/es/utils/guards.js +385 -0
- package/dist/es/utils/guards.js.map +1 -0
- package/dist/es/utils/i18n.js +243 -0
- package/dist/es/utils/i18n.js.map +1 -0
- package/dist/es/utils/is.js +367 -0
- package/dist/es/utils/is.js.map +1 -0
- package/dist/es/utils/sharedStore.js +48 -0
- package/dist/es/utils/sharedStore.js.map +1 -0
- package/dist/es/utils/validation.js +774 -0
- package/dist/es/utils/validation.js.map +1 -0
- package/dist/types/encoding/base32.d.ts +5 -1
- package/dist/types/encoding/base58.d.ts +4 -0
- package/dist/types/encoding/base64.d.ts +4 -0
- package/dist/types/encoding/base64Url.d.ts +4 -0
- package/dist/types/errors/alreadyExistsError.d.ts +6 -3
- package/dist/types/errors/baseError.d.ts +2 -2
- package/dist/types/errors/conflictError.d.ts +6 -3
- package/dist/types/errors/generalError.d.ts +2 -2
- package/dist/types/errors/guardError.d.ts +2 -2
- package/dist/types/errors/notFoundError.d.ts +6 -3
- package/dist/types/errors/notImplementedError.d.ts +6 -2
- package/dist/types/errors/notSupportedError.d.ts +6 -3
- package/dist/types/errors/unauthorizedError.d.ts +7 -4
- package/dist/types/errors/unprocessableError.d.ts +2 -2
- package/dist/types/errors/validationError.d.ts +2 -2
- package/dist/types/factories/componentFactory.d.ts +2 -2
- package/dist/types/factories/factory.d.ts +27 -1
- package/dist/types/helpers/arrayHelper.d.ts +1 -8
- package/dist/types/helpers/errorHelper.d.ts +1 -1
- package/dist/types/helpers/jsonHelper.d.ts +5 -1
- package/dist/types/helpers/numberHelper.d.ts +17 -0
- package/dist/types/helpers/objectHelper.d.ts +24 -6
- package/dist/types/helpers/randomHelper.d.ts +16 -0
- package/dist/types/helpers/stringHelper.d.ts +12 -6
- package/dist/types/index.d.ts +57 -53
- package/dist/types/models/IComponent.d.ts +18 -7
- package/dist/types/models/IError.d.ts +1 -1
- package/dist/types/models/IHealth.d.ts +32 -0
- package/dist/types/models/ILocalesIndex.d.ts +1 -1
- package/dist/types/models/IValidationFailure.d.ts +0 -4
- package/dist/types/models/healthStatus.d.ts +21 -0
- package/dist/types/types/bitString.d.ts +4 -0
- package/dist/types/types/singleOccurrenceArray.d.ts +9 -0
- package/dist/types/types/url.d.ts +8 -3
- package/dist/types/types/urn.d.ts +7 -2
- package/dist/types/utils/asyncCache.d.ts +7 -0
- package/dist/types/utils/coerce.d.ts +1 -1
- package/dist/types/utils/compression.d.ts +5 -1
- package/dist/types/utils/guards.d.ts +11 -3
- package/dist/types/utils/i18n.d.ts +16 -1
- package/dist/types/utils/is.d.ts +8 -1
- package/dist/types/utils/validation.d.ts +1 -1
- package/docs/changelog.md +1224 -131
- package/docs/examples.md +308 -1
- package/docs/reference/classes/AlreadyExistsError.md +43 -39
- package/docs/reference/classes/ArrayHelper.md +10 -44
- package/docs/reference/classes/AsyncCache.md +12 -12
- package/docs/reference/classes/Base32.md +11 -3
- package/docs/reference/classes/Base58.md +10 -2
- package/docs/reference/classes/Base64.md +11 -3
- package/docs/reference/classes/Base64Url.md +10 -2
- package/docs/reference/classes/BaseError.md +37 -37
- package/docs/reference/classes/BitString.md +13 -5
- package/docs/reference/classes/Coerce.md +31 -31
- package/docs/reference/classes/Compression.md +10 -2
- package/docs/reference/classes/ConflictError.md +43 -39
- package/docs/reference/classes/Converter.md +18 -18
- package/docs/reference/classes/EnvHelper.md +1 -1
- package/docs/reference/classes/ErrorHelper.md +5 -5
- package/docs/reference/classes/Factory.md +122 -20
- package/docs/reference/classes/FilenameHelper.md +1 -1
- package/docs/reference/classes/GeneralError.md +38 -38
- package/docs/reference/classes/GuardError.md +38 -38
- package/docs/reference/classes/Guards.md +80 -34
- package/docs/reference/classes/HexHelper.md +6 -6
- package/docs/reference/classes/I18n.md +66 -14
- package/docs/reference/classes/Is.md +77 -43
- package/docs/reference/classes/JsonHelper.md +17 -9
- package/docs/reference/classes/NotFoundError.md +43 -39
- package/docs/reference/classes/NotImplementedError.md +48 -38
- package/docs/reference/classes/NotSupportedError.md +43 -39
- package/docs/reference/classes/NumberHelper.md +55 -0
- package/docs/reference/classes/ObjectHelper.md +163 -51
- package/docs/reference/classes/RandomHelper.md +53 -1
- package/docs/reference/classes/SharedStore.md +5 -5
- package/docs/reference/classes/StringHelper.md +52 -30
- package/docs/reference/classes/Uint8ArrayHelper.md +1 -1
- package/docs/reference/classes/UnauthorizedError.md +44 -40
- package/docs/reference/classes/UnprocessableError.md +38 -38
- package/docs/reference/classes/Url.md +24 -10
- package/docs/reference/classes/Urn.md +42 -28
- package/docs/reference/classes/Validation.md +27 -27
- package/docs/reference/classes/ValidationError.md +37 -37
- package/docs/reference/index.md +6 -0
- package/docs/reference/interfaces/IComponent.md +57 -27
- package/docs/reference/interfaces/IError.md +11 -11
- package/docs/reference/interfaces/IHealth.md +55 -0
- package/docs/reference/interfaces/II18nShared.md +4 -4
- package/docs/reference/interfaces/IKeyValue.md +2 -2
- package/docs/reference/interfaces/ILabelledValue.md +2 -2
- package/docs/reference/interfaces/ILocale.md +2 -2
- package/docs/reference/interfaces/ILocaleDictionary.md +1 -1
- package/docs/reference/interfaces/ILocalesIndex.md +1 -1
- package/docs/reference/interfaces/IPatchOperation.md +6 -6
- package/docs/reference/interfaces/IUrlParts.md +9 -9
- package/docs/reference/interfaces/IValidationFailure.md +4 -12
- package/docs/reference/type-aliases/HealthStatus.md +5 -0
- package/docs/reference/type-aliases/SingleOccurrenceArray.md +15 -0
- package/docs/reference/type-aliases/SingleOccurrenceArrayDepthHelper.md +19 -0
- package/docs/reference/variables/CoerceType.md +10 -10
- package/docs/reference/variables/CompressionType.md +2 -2
- package/docs/reference/variables/HealthStatus.md +25 -0
- package/locales/.validate-ignore +1 -0
- package/locales/en.json +9 -7
- package/package.json +24 -12
- package/dist/cjs/index.cjs +0 -5175
- package/dist/esm/index.mjs +0 -5133
- /package/dist/types/{models → types}/objectOrArray.d.ts +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guards.js","sourceRoot":"","sources":["../../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD;;GAEG;AACH,MAAM,OAAO,MAAM;IAClB;;;;;;OAMG;IACI,MAAM,CAAC,OAAO,CAAC,MAAc,EAAE,QAAgB,EAAE,KAAc;QACrE,IAAI,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,UAAU,CAAC,MAAM,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;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;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,MAAM,CACnB,MAAc,EACd,QAAgB,EAChB,KAAc,EACd,MAA+B;QAE/B,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC;YAC/B,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBAC1B,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,qBAAqB,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;YACtE,CAAC;YACD,MAAM,IAAI,UAAU,CAAC,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC;QAC/D,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 \"../types/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\t/**\n\t * Is the property a string containing uuidV7.\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 format The format of the uuidV7, either standard or compact.\n\t * @throws GuardError If the value does not match the assertion.\n\t */\n\tpublic static uuidV7(\n\t\tsource: string,\n\t\tproperty: string,\n\t\tvalue: unknown,\n\t\tformat?: \"standard\" | \"compact\"\n\t): asserts value is string {\n\t\tif (!Is.uuidV7(value, format)) {\n\t\t\tif (format === \"compact\") {\n\t\t\t\tthrow new GuardError(source, \"guard.uuidV7Compact\", property, value);\n\t\t\t}\n\t\t\tthrow new GuardError(source, \"guard.uuidV7\", 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,IAAI,UAAU,CAAC,qBAAqB,EAAE,CAAC;YACzD,UAAU,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC;QACtE,CAAC;IACF,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,SAAS;QACtB,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QACxC,OAAO,UAAU,CAAC,aAAa,CAAC;IACjC,CAAC;IAED;;;;OA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at\";\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;\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"]}
|