generaltranslation 8.2.11 → 8.2.13
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/CHANGELOG.md +15 -0
- package/dist/{IntlCache-k2qfrDqB.cjs → IntlCache-CAW8tKhd.cjs} +12 -12
- package/dist/IntlCache-CAW8tKhd.cjs.map +1 -0
- package/dist/{IntlCache-Ccg_cQPR.mjs → IntlCache-WZk0rKvj.mjs} +12 -12
- package/dist/IntlCache-WZk0rKvj.mjs.map +1 -0
- package/dist/LocaleConfig.d.ts +59 -0
- package/dist/LocaleConfig.js +225 -0
- package/dist/backwards-compatability/dataConversion.d.ts +23 -0
- package/dist/backwards-compatability/dataConversion.js +207 -0
- package/dist/backwards-compatability/oldHashJsxChildren.d.ts +23 -0
- package/dist/backwards-compatability/oldHashJsxChildren.js +79 -0
- package/dist/backwards-compatability/oldTypes.d.ts +21 -0
- package/dist/backwards-compatability/oldTypes.js +1 -0
- package/dist/backwards-compatability/typeChecking.d.ts +21 -0
- package/dist/backwards-compatability/typeChecking.js +61 -0
- package/dist/{base64-CUcEPEC5.cjs → base64-2fu94Klt.cjs} +3 -3
- package/dist/base64-2fu94Klt.cjs.map +1 -0
- package/dist/{base64-C1sogiix.mjs → base64-DH0STixb.mjs} +3 -3
- package/dist/base64-DH0STixb.mjs.map +1 -0
- package/dist/cache/IntlCache.d.ts +26 -0
- package/dist/cache/IntlCache.js +84 -0
- package/dist/cache/types.d.ts +32 -0
- package/dist/cache/types.js +1 -0
- package/dist/{core-3SOQ5ND6.cjs → core-7RP541eY.cjs} +18 -20
- package/dist/core-7RP541eY.cjs.map +1 -0
- package/dist/{core-6rY3oy32.d.cts → core-I9pWGafA.d.mts} +8 -8
- package/dist/{core-BYVWty_t.d.mts → core-TLJoDpJP.d.cts} +8 -8
- package/dist/{core-Bx0m6GOp.mjs → core-isLphYAZ.mjs} +18 -20
- package/dist/core-isLphYAZ.mjs.map +1 -0
- package/dist/core.cjs +1 -1
- package/dist/core.d.cts +1 -1
- package/dist/core.d.mts +1 -1
- package/dist/core.d.ts +128 -0
- package/dist/core.js +137 -0
- package/dist/core.mjs +1 -1
- package/dist/derive/condenseVars.d.ts +7 -0
- package/dist/derive/condenseVars.js +30 -0
- package/dist/derive/declareVar.d.ts +26 -0
- package/dist/derive/declareVar.js +38 -0
- package/dist/derive/decodeVars.d.ts +9 -0
- package/dist/derive/decodeVars.js +52 -0
- package/dist/derive/derive.d.ts +53 -0
- package/dist/derive/derive.js +55 -0
- package/dist/derive/extractVars.d.ts +9 -0
- package/dist/derive/extractVars.js +34 -0
- package/dist/derive/index.d.ts +7 -0
- package/dist/derive/index.js +7 -0
- package/dist/derive/indexVars.d.ts +6 -0
- package/dist/derive/indexVars.js +52 -0
- package/dist/derive/utils/constants.d.ts +2 -0
- package/dist/derive/utils/constants.js +2 -0
- package/dist/derive/utils/regex.d.ts +2 -0
- package/dist/derive/utils/regex.js +4 -0
- package/dist/derive/utils/sanitizeVar.d.ts +12 -0
- package/dist/derive/utils/sanitizeVar.js +38 -0
- package/dist/derive/utils/traverseHelpers.d.ts +4 -0
- package/dist/derive/utils/traverseHelpers.js +22 -0
- package/dist/derive/utils/traverseIcu.d.ts +20 -0
- package/dist/derive/utils/traverseIcu.js +49 -0
- package/dist/derive/utils/types.d.ts +23 -0
- package/dist/derive/utils/types.js +1 -0
- package/dist/errors/ApiError.d.ts +7 -0
- package/dist/errors/ApiError.js +33 -0
- package/dist/errors/formattingErrors.d.ts +1 -0
- package/dist/errors/formattingErrors.js +3 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +1 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.d.ts +59 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.js +147 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.d.ts +4 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.js +30 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.d.ts +48 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.js +2 -0
- package/dist/formatting/format.d.ts +1 -0
- package/dist/formatting/format.js +257 -0
- package/dist/id/hashSource.d.ts +25 -0
- package/dist/id/hashSource.js +98 -0
- package/dist/id/hashTemplate.d.ts +4 -0
- package/dist/id/hashTemplate.js +6 -0
- package/dist/id/types.d.ts +7 -0
- package/dist/id/types.js +1 -0
- package/dist/{id-COlX5v3V.cjs → id-CyiXsQrY.cjs} +9 -9
- package/dist/id-CyiXsQrY.cjs.map +1 -0
- package/dist/{id-BmOyfaug.mjs → id-DbD7K-HL.mjs} +9 -9
- package/dist/id-DbD7K-HL.mjs.map +1 -0
- package/dist/id.cjs +1 -1
- package/dist/id.d.cts +9 -9
- package/dist/id.d.mts +9 -9
- package/dist/id.d.ts +3 -0
- package/dist/id.js +3 -0
- package/dist/id.mjs +1 -1
- package/dist/index.cjs +140 -140
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +88 -88
- package/dist/index.d.mts +88 -88
- package/dist/index.d.ts +1019 -0
- package/dist/index.js +1664 -0
- package/dist/index.mjs +140 -140
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +41 -26
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +38 -23
- package/dist/internal.d.mts +38 -23
- package/dist/internal.d.ts +19 -0
- package/dist/internal.js +17 -0
- package/dist/internal.mjs +41 -26
- package/dist/internal.mjs.map +1 -1
- package/dist/locales/customLocaleMapping.d.ts +11 -0
- package/dist/locales/customLocaleMapping.js +23 -0
- package/dist/locales/determineLocale.d.ts +1 -0
- package/dist/locales/determineLocale.js +72 -0
- package/dist/locales/getLocaleDirection.d.ts +1 -0
- package/dist/locales/getLocaleDirection.js +89 -0
- package/dist/locales/getLocaleEmoji.d.ts +2 -0
- package/dist/locales/getLocaleEmoji.js +319 -0
- package/dist/locales/getLocaleName.d.ts +1 -0
- package/dist/locales/getLocaleName.js +45 -0
- package/dist/locales/getLocaleProperties.d.ts +32 -0
- package/dist/locales/getLocaleProperties.js +220 -0
- package/dist/locales/getPluralForm.d.ts +9 -0
- package/dist/locales/getPluralForm.js +63 -0
- package/dist/locales/getRegionProperties.d.ts +7 -0
- package/dist/locales/getRegionProperties.js +61 -0
- package/dist/locales/isSameDialect.d.ts +1 -0
- package/dist/locales/isSameDialect.js +41 -0
- package/dist/locales/isSameLanguage.d.ts +1 -0
- package/dist/locales/isSameLanguage.js +20 -0
- package/dist/locales/isSupersetLocale.d.ts +1 -0
- package/dist/locales/isSupersetLocale.js +22 -0
- package/dist/locales/isValidLocale.d.ts +1 -0
- package/dist/locales/isValidLocale.js +75 -0
- package/dist/locales/requiresTranslation.d.ts +1 -0
- package/dist/locales/requiresTranslation.js +32 -0
- package/dist/locales/resolveAliasLocale.d.ts +8 -0
- package/dist/locales/resolveAliasLocale.js +21 -0
- package/dist/locales/resolveCanonicalLocale.d.ts +8 -0
- package/dist/locales/resolveCanonicalLocale.js +13 -0
- package/dist/logging/errors.d.ts +10 -0
- package/dist/logging/errors.js +29 -0
- package/dist/logging/logger.d.ts +121 -0
- package/dist/logging/logger.js +259 -0
- package/dist/logging/warnings.d.ts +2 -0
- package/dist/logging/warnings.js +2 -0
- package/dist/projects/getProjectData.d.ts +1 -0
- package/dist/projects/getProjectData.js +87 -0
- package/dist/settings/plurals.d.ts +3 -0
- package/dist/settings/plurals.js +14 -0
- package/dist/settings/settings.d.ts +2 -0
- package/dist/settings/settings.js +2 -0
- package/dist/settings/settingsUrls.d.ts +3 -0
- package/dist/settings/settingsUrls.js +3 -0
- package/dist/translate/api.d.ts +1 -0
- package/dist/translate/api.js +1 -0
- package/dist/translate/awaitJobs.d.ts +19 -0
- package/dist/translate/awaitJobs.js +112 -0
- package/dist/translate/checkJobStatus.d.ts +8 -0
- package/dist/translate/checkJobStatus.js +55 -0
- package/dist/translate/createBranch.d.ts +10 -0
- package/dist/translate/createBranch.js +53 -0
- package/dist/translate/createTag.d.ts +19 -0
- package/dist/translate/createTag.js +67 -0
- package/dist/translate/downloadFileBatch.d.ts +1 -0
- package/dist/translate/downloadFileBatch.js +77 -0
- package/dist/translate/enqueueFiles.d.ts +8 -0
- package/dist/translate/enqueueFiles.js +96 -0
- package/dist/translate/getOrphanedFiles.d.ts +8 -0
- package/dist/translate/getOrphanedFiles.js +97 -0
- package/dist/translate/processFileMoves.d.ts +25 -0
- package/dist/translate/processFileMoves.js +90 -0
- package/dist/translate/publishFiles.d.ts +17 -0
- package/dist/translate/publishFiles.js +56 -0
- package/dist/translate/queryBranchData.d.ts +3 -0
- package/dist/translate/queryBranchData.js +53 -0
- package/dist/translate/queryFileData.d.ts +42 -0
- package/dist/translate/queryFileData.js +71 -0
- package/dist/translate/querySourceFile.d.ts +1 -0
- package/dist/translate/querySourceFile.js +67 -0
- package/dist/translate/setupProject.d.ts +11 -0
- package/dist/translate/setupProject.js +63 -0
- package/dist/translate/submitUserEditDiffs.d.ts +12 -0
- package/dist/translate/submitUserEditDiffs.js +68 -0
- package/dist/translate/translateMany.d.ts +1 -0
- package/dist/translate/translateMany.js +103 -0
- package/dist/translate/uploadSourceFiles.d.ts +1 -0
- package/dist/translate/uploadSourceFiles.js +87 -0
- package/dist/translate/uploadTranslations.d.ts +1 -0
- package/dist/translate/uploadTranslations.js +101 -0
- package/dist/translate/utils/apiRequest.d.ts +1 -0
- package/dist/translate/utils/apiRequest.js +130 -0
- package/dist/translate/utils/batch.d.ts +52 -0
- package/dist/translate/utils/batch.js +126 -0
- package/dist/translate/utils/fetchWithTimeout.d.ts +1 -0
- package/dist/translate/utils/fetchWithTimeout.js +93 -0
- package/dist/translate/utils/generateRequestHeaders.d.ts +2 -0
- package/dist/translate/utils/generateRequestHeaders.js +26 -0
- package/dist/translate/utils/handleFetchError.d.ts +1 -0
- package/dist/translate/utils/handleFetchError.js +12 -0
- package/dist/translate/utils/validateFileFormatTransform.d.ts +16 -0
- package/dist/translate/utils/validateFileFormatTransform.js +29 -0
- package/dist/translate/utils/validateResponse.d.ts +1 -0
- package/dist/translate/utils/validateResponse.js +72 -0
- package/dist/{types-Cy8q2XZX.d.mts → types-AHtYZIP-.d.mts} +63 -40
- package/dist/{types-NKhpChvT.d.cts → types-Bf8_Apq_.d.cts} +63 -40
- package/dist/types-dir/api/branch.d.ts +10 -0
- package/dist/types-dir/api/branch.js +1 -0
- package/dist/types-dir/api/checkFileTranslations.d.ts +37 -0
- package/dist/types-dir/api/checkFileTranslations.js +1 -0
- package/dist/types-dir/api/downloadFile.d.ts +3 -0
- package/dist/types-dir/api/downloadFile.js +1 -0
- package/dist/types-dir/api/downloadFileBatch.d.ts +35 -0
- package/dist/types-dir/api/downloadFileBatch.js +1 -0
- package/dist/types-dir/api/enqueueEntries.d.ts +21 -0
- package/dist/types-dir/api/enqueueEntries.js +1 -0
- package/dist/types-dir/api/enqueueFiles.d.ts +70 -0
- package/dist/types-dir/api/enqueueFiles.js +1 -0
- package/dist/types-dir/api/entry.d.ts +39 -0
- package/dist/types-dir/api/entry.js +1 -0
- package/dist/types-dir/api/fetchTranslations.d.ts +12 -0
- package/dist/types-dir/api/fetchTranslations.js +1 -0
- package/dist/types-dir/api/file.d.ts +57 -0
- package/dist/types-dir/api/file.js +1 -0
- package/dist/types-dir/api/json.d.ts +5 -0
- package/dist/types-dir/api/json.js +1 -0
- package/dist/types-dir/api/project.d.ts +7 -0
- package/dist/types-dir/api/project.js +1 -0
- package/dist/types-dir/api/translate.d.ts +34 -0
- package/dist/types-dir/api/translate.js +1 -0
- package/dist/types-dir/api/translateMany.d.ts +5 -0
- package/dist/types-dir/api/translateMany.js +1 -0
- package/dist/types-dir/api/translationStatus.d.ts +9 -0
- package/dist/types-dir/api/translationStatus.js +1 -0
- package/dist/types-dir/api/uploadFiles.d.ts +44 -0
- package/dist/types-dir/api/uploadFiles.js +1 -0
- package/dist/types-dir/jsx/content.d.ts +61 -0
- package/dist/types-dir/jsx/content.js +11 -0
- package/dist/types-dir/jsx/variables.d.ts +9 -0
- package/dist/types-dir/jsx/variables.js +1 -0
- package/dist/types-dir/transformations.d.ts +8 -0
- package/dist/types-dir/transformations.js +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +129 -0
- package/dist/types.js +2 -0
- package/dist/types.mjs.map +1 -1
- package/dist/utils/base64.d.ts +2 -0
- package/dist/utils/base64.js +28 -0
- package/dist/utils/isSupportedFileFormatTransform.d.ts +8 -0
- package/dist/utils/isSupportedFileFormatTransform.js +26 -0
- package/dist/utils/isVariable.d.ts +2 -0
- package/dist/utils/isVariable.js +22 -0
- package/dist/utils/minify.d.ts +2 -0
- package/dist/utils/minify.js +10 -0
- package/dist/utils/stableStringify.d.ts +1 -0
- package/dist/utils/stableStringify.js +40 -0
- package/package.json +5 -6
- package/dist/IntlCache-Ccg_cQPR.mjs.map +0 -1
- package/dist/IntlCache-k2qfrDqB.cjs.map +0 -1
- package/dist/base64-C1sogiix.mjs.map +0 -1
- package/dist/base64-CUcEPEC5.cjs.map +0 -1
- package/dist/core-3SOQ5ND6.cjs.map +0 -1
- package/dist/core-Bx0m6GOp.mjs.map +0 -1
- package/dist/id-BmOyfaug.mjs.map +0 -1
- package/dist/id-COlX5v3V.cjs.map +0 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MessageFormatElement, ParserOptions } from '@formatjs/icu-messageformat-parser';
|
|
2
|
+
type TraverseIcuOptions = ParserOptions & {
|
|
3
|
+
recurseIntoVisited?: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Given an ICU string, traverse the AST and call the visitor function for each element that matches the type T
|
|
7
|
+
* @param icu - The ICU string to traverse
|
|
8
|
+
* @param shouldVisit - A function that returns true if the element should be visited
|
|
9
|
+
* @param visitor - A function that is called for each element that matches the type T
|
|
10
|
+
* @returns The modified AST of the ICU string.
|
|
11
|
+
*
|
|
12
|
+
* @note This function is a heavy operation, use sparingly
|
|
13
|
+
*/
|
|
14
|
+
export declare function traverseIcu<T extends MessageFormatElement>({ icuString, shouldVisit, visitor, options: { recurseIntoVisited, ...otherOptions }, }: {
|
|
15
|
+
icuString: string;
|
|
16
|
+
shouldVisit: (element: MessageFormatElement) => element is T;
|
|
17
|
+
visitor: (element: T) => void;
|
|
18
|
+
options: TraverseIcuOptions;
|
|
19
|
+
}): MessageFormatElement[];
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import { parse, TYPE, } from '@formatjs/icu-messageformat-parser';
|
|
13
|
+
/**
|
|
14
|
+
* Given an ICU string, traverse the AST and call the visitor function for each element that matches the type T
|
|
15
|
+
* @param icu - The ICU string to traverse
|
|
16
|
+
* @param shouldVisit - A function that returns true if the element should be visited
|
|
17
|
+
* @param visitor - A function that is called for each element that matches the type T
|
|
18
|
+
* @returns The modified AST of the ICU string.
|
|
19
|
+
*
|
|
20
|
+
* @note This function is a heavy operation, use sparingly
|
|
21
|
+
*/
|
|
22
|
+
export function traverseIcu(_a) {
|
|
23
|
+
var icuString = _a.icuString, shouldVisit = _a.shouldVisit, visitor = _a.visitor, _b = _a.options, _c = _b.recurseIntoVisited, recurseIntoVisited = _c === void 0 ? true : _c, otherOptions = __rest(_b, ["recurseIntoVisited"]);
|
|
24
|
+
var ast = parse(icuString, otherOptions);
|
|
25
|
+
handleChildren(ast);
|
|
26
|
+
return ast;
|
|
27
|
+
function handleChildren(children) {
|
|
28
|
+
children.map(handleChild);
|
|
29
|
+
}
|
|
30
|
+
function handleChild(child) {
|
|
31
|
+
// handle select var
|
|
32
|
+
var visited = false;
|
|
33
|
+
if (shouldVisit(child)) {
|
|
34
|
+
visitor(child);
|
|
35
|
+
visited = true;
|
|
36
|
+
}
|
|
37
|
+
// recurse on children
|
|
38
|
+
if (!visited || recurseIntoVisited) {
|
|
39
|
+
if (child.type === TYPE.select || child.type === TYPE.plural) {
|
|
40
|
+
Object.values(child.options)
|
|
41
|
+
.map(function (option) { return option.value; })
|
|
42
|
+
.map(handleChildren);
|
|
43
|
+
}
|
|
44
|
+
else if (child.type === TYPE.tag) {
|
|
45
|
+
handleChildren(child.children);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { VAR_IDENTIFIER } from './constants';
|
|
2
|
+
import type { PluralOrSelectOption, LiteralElement, SelectElement } from '@formatjs/icu-messageformat-parser/types.js';
|
|
3
|
+
interface GTIndexedSelectOption extends PluralOrSelectOption {
|
|
4
|
+
value: Array<LiteralElement>;
|
|
5
|
+
}
|
|
6
|
+
export interface GTIndexedSelectElement extends SelectElement {
|
|
7
|
+
value: `${typeof VAR_IDENTIFIER}${number}`;
|
|
8
|
+
options: {
|
|
9
|
+
other: GTIndexedSelectOption;
|
|
10
|
+
[key: string]: PluralOrSelectOption;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
interface GTUnindexedSelectOption extends PluralOrSelectOption {
|
|
14
|
+
value: Array<LiteralElement>;
|
|
15
|
+
}
|
|
16
|
+
export interface GTUnindexedSelectElement extends SelectElement {
|
|
17
|
+
value: typeof VAR_IDENTIFIER;
|
|
18
|
+
options: {
|
|
19
|
+
other: GTUnindexedSelectOption;
|
|
20
|
+
[key: string]: PluralOrSelectOption;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var ApiError = /** @class */ (function (_super) {
|
|
17
|
+
__extends(ApiError, _super);
|
|
18
|
+
function ApiError(error, code, message) {
|
|
19
|
+
var _this = _super.call(this, error) || this;
|
|
20
|
+
_this.name = 'ApiError';
|
|
21
|
+
_this.code = code;
|
|
22
|
+
_this.message = message;
|
|
23
|
+
return _this;
|
|
24
|
+
}
|
|
25
|
+
ApiError.prototype.getCode = function () {
|
|
26
|
+
return this.code;
|
|
27
|
+
};
|
|
28
|
+
ApiError.prototype.getMessage = function () {
|
|
29
|
+
return this.message;
|
|
30
|
+
};
|
|
31
|
+
return ApiError;
|
|
32
|
+
}(Error));
|
|
33
|
+
export { ApiError };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createInvalidCutoffStyleError: (style: string) => string;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors/ApiError';
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './errors/ApiError';
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CutoffFormat, CutoffFormatOptions, PostpendedCutoffParts, PrependedCutoffParts, ResolvedCutoffFormatOptions } from './types';
|
|
2
|
+
export declare class CutoffFormatConstructor implements CutoffFormat {
|
|
3
|
+
private locale;
|
|
4
|
+
private options;
|
|
5
|
+
private additionLength;
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param {Intl.LocalesArgument} locales - The locales to use for formatting.
|
|
9
|
+
* @param {CutoffFormatOptions} options - The options for formatting.
|
|
10
|
+
* @param {number} [options.maxChars] - The maximum number of characters to display.
|
|
11
|
+
* - Undefined values are treated as no cutoff.
|
|
12
|
+
* - Negative values follow .slice() behavior and terminator will be added before the value.
|
|
13
|
+
* - 0 will result in an empty string.
|
|
14
|
+
* - If cutoff results in an empty string, no terminator is added.
|
|
15
|
+
* @param {CutoffFormatStyle} [options.style='ellipsis'] - The style of the terminator.
|
|
16
|
+
* @param {string} [options.terminator] - Optional override the terminator to use.
|
|
17
|
+
* @param {string} [options.separator] - Optional override the separator to use between the terminator and the value.
|
|
18
|
+
* - If no terminator is provided, then separator is ignored.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const format = new CutoffFormat('en', { maxChars: 5 });
|
|
22
|
+
* format.format('Hello, world!'); // 'Hello...'
|
|
23
|
+
*
|
|
24
|
+
* const format = new CutoffFormat('en', { maxChars: -3 });
|
|
25
|
+
* format.format('Hello, world!'); // '...ld!'
|
|
26
|
+
*/
|
|
27
|
+
constructor(locales: Intl.LocalesArgument, options?: CutoffFormatOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Format a value according to the cutoff options, returning a formatted string.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
32
|
+
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 8, style: 'ellipsis' });
|
|
36
|
+
* formatter.format('Hello, world!'); // Returns 'Hello, w...'
|
|
37
|
+
*/
|
|
38
|
+
format(value: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Format a value to parts according to the cutoff options, returning an array of string parts.
|
|
41
|
+
* This method breaks down the formatted result into individual components for more granular control.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
44
|
+
* @returns {PrependedCutoffParts | PostpendedCutoffParts} An array of string parts representing the formatted result.
|
|
45
|
+
* - For positive maxChars: [cutoffValue, separator?, terminator?]
|
|
46
|
+
* - For negative maxChars: [terminator?, separator?, cutoffValue]
|
|
47
|
+
* - For no cutoff: [originalValue]
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 5, style: 'ellipsis' });
|
|
51
|
+
* formatter.formatToParts('Hello, world!'); // Returns ['Hello', '...']
|
|
52
|
+
*/
|
|
53
|
+
formatToParts(value: string): PrependedCutoffParts | PostpendedCutoffParts;
|
|
54
|
+
/**
|
|
55
|
+
* Get the resolved options
|
|
56
|
+
* @returns {ResolvedCutoffFormatOptions} The resolved options.
|
|
57
|
+
*/
|
|
58
|
+
resolvedOptions(): ResolvedCutoffFormatOptions;
|
|
59
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { createInvalidCutoffStyleError } from '../../../errors/formattingErrors';
|
|
2
|
+
import { libraryDefaultLocale } from '../../../settings/settings';
|
|
3
|
+
import { DEFAULT_CUTOFF_FORMAT_STYLE, DEFAULT_TERMINATOR_KEY, TERMINATOR_MAP, } from './constants';
|
|
4
|
+
var CutoffFormatConstructor = /** @class */ (function () {
|
|
5
|
+
/**
|
|
6
|
+
* Constructor
|
|
7
|
+
* @param {Intl.LocalesArgument} locales - The locales to use for formatting.
|
|
8
|
+
* @param {CutoffFormatOptions} options - The options for formatting.
|
|
9
|
+
* @param {number} [options.maxChars] - The maximum number of characters to display.
|
|
10
|
+
* - Undefined values are treated as no cutoff.
|
|
11
|
+
* - Negative values follow .slice() behavior and terminator will be added before the value.
|
|
12
|
+
* - 0 will result in an empty string.
|
|
13
|
+
* - If cutoff results in an empty string, no terminator is added.
|
|
14
|
+
* @param {CutoffFormatStyle} [options.style='ellipsis'] - The style of the terminator.
|
|
15
|
+
* @param {string} [options.terminator] - Optional override the terminator to use.
|
|
16
|
+
* @param {string} [options.separator] - Optional override the separator to use between the terminator and the value.
|
|
17
|
+
* - If no terminator is provided, then separator is ignored.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* const format = new CutoffFormat('en', { maxChars: 5 });
|
|
21
|
+
* format.format('Hello, world!'); // 'Hello...'
|
|
22
|
+
*
|
|
23
|
+
* const format = new CutoffFormat('en', { maxChars: -3 });
|
|
24
|
+
* format.format('Hello, world!'); // '...ld!'
|
|
25
|
+
*/
|
|
26
|
+
function CutoffFormatConstructor(locales, options) {
|
|
27
|
+
if (options === void 0) { options = {}; }
|
|
28
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
29
|
+
// Determine locale (this replicates Intl.NumberFormat behavior including silent failure)
|
|
30
|
+
try {
|
|
31
|
+
// Normalize locales to string
|
|
32
|
+
var localesList = !locales
|
|
33
|
+
? [libraryDefaultLocale]
|
|
34
|
+
: Array.isArray(locales)
|
|
35
|
+
? locales.map(function (l) { return String(l); })
|
|
36
|
+
: [String(locales)];
|
|
37
|
+
var canonicalLocales = Intl.getCanonicalLocales(localesList);
|
|
38
|
+
this.locale = canonicalLocales.length
|
|
39
|
+
? canonicalLocales[0]
|
|
40
|
+
: libraryDefaultLocale;
|
|
41
|
+
}
|
|
42
|
+
catch (_h) {
|
|
43
|
+
this.locale = libraryDefaultLocale;
|
|
44
|
+
}
|
|
45
|
+
// Follows Intl.NumberFormat behavior of throwing an error when currency is invalid
|
|
46
|
+
if (!TERMINATOR_MAP[(_a = options.style) !== null && _a !== void 0 ? _a : DEFAULT_CUTOFF_FORMAT_STYLE]) {
|
|
47
|
+
throw new Error(createInvalidCutoffStyleError((_b = options.style) !== null && _b !== void 0 ? _b : DEFAULT_CUTOFF_FORMAT_STYLE));
|
|
48
|
+
}
|
|
49
|
+
// Resolve terminator options.
|
|
50
|
+
var style;
|
|
51
|
+
var presetTerminatorOptions;
|
|
52
|
+
if (options.maxChars !== undefined) {
|
|
53
|
+
style = (_c = options.style) !== null && _c !== void 0 ? _c : DEFAULT_CUTOFF_FORMAT_STYLE;
|
|
54
|
+
// TODO: need more sophisticated locale negotiation if we want to add support for region/script/etc.-specific terminators in the future
|
|
55
|
+
var languageCode = new Intl.Locale(this.locale).language;
|
|
56
|
+
presetTerminatorOptions =
|
|
57
|
+
TERMINATOR_MAP[style][languageCode] ||
|
|
58
|
+
TERMINATOR_MAP[style][DEFAULT_TERMINATOR_KEY];
|
|
59
|
+
}
|
|
60
|
+
var terminator = (_d = options.terminator) !== null && _d !== void 0 ? _d : presetTerminatorOptions === null || presetTerminatorOptions === void 0 ? void 0 : presetTerminatorOptions.terminator;
|
|
61
|
+
var separator = terminator != null
|
|
62
|
+
? ((_e = options.separator) !== null && _e !== void 0 ? _e : presetTerminatorOptions === null || presetTerminatorOptions === void 0 ? void 0 : presetTerminatorOptions.separator)
|
|
63
|
+
: undefined;
|
|
64
|
+
// // Remove terminator and separator if maxChars does have enough space
|
|
65
|
+
this.additionLength = ((_f = terminator === null || terminator === void 0 ? void 0 : terminator.length) !== null && _f !== void 0 ? _f : 0) + ((_g = separator === null || separator === void 0 ? void 0 : separator.length) !== null && _g !== void 0 ? _g : 0);
|
|
66
|
+
if (options.maxChars !== undefined &&
|
|
67
|
+
Math.abs(options.maxChars) < this.additionLength) {
|
|
68
|
+
terminator = undefined;
|
|
69
|
+
separator = undefined;
|
|
70
|
+
}
|
|
71
|
+
this.options = {
|
|
72
|
+
maxChars: options.maxChars,
|
|
73
|
+
style: style,
|
|
74
|
+
terminator: terminator,
|
|
75
|
+
separator: separator,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Format a value according to the cutoff options, returning a formatted string.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
82
|
+
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 8, style: 'ellipsis' });
|
|
86
|
+
* formatter.format('Hello, world!'); // Returns 'Hello, w...'
|
|
87
|
+
*/
|
|
88
|
+
CutoffFormatConstructor.prototype.format = function (value) {
|
|
89
|
+
return this.formatToParts(value).join('');
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Format a value to parts according to the cutoff options, returning an array of string parts.
|
|
93
|
+
* This method breaks down the formatted result into individual components for more granular control.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
96
|
+
* @returns {PrependedCutoffParts | PostpendedCutoffParts} An array of string parts representing the formatted result.
|
|
97
|
+
* - For positive maxChars: [cutoffValue, separator?, terminator?]
|
|
98
|
+
* - For negative maxChars: [terminator?, separator?, cutoffValue]
|
|
99
|
+
* - For no cutoff: [originalValue]
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 5, style: 'ellipsis' });
|
|
103
|
+
* formatter.formatToParts('Hello, world!'); // Returns ['Hello', '...']
|
|
104
|
+
*/
|
|
105
|
+
CutoffFormatConstructor.prototype.formatToParts = function (value) {
|
|
106
|
+
var _a = this.options, maxChars = _a.maxChars, terminator = _a.terminator, separator = _a.separator;
|
|
107
|
+
// Slice our value
|
|
108
|
+
// const additionLength = (terminator?.length ?? 0) + (separator?.length ?? 0);
|
|
109
|
+
var adjustedChars = maxChars === undefined || Math.abs(maxChars) >= value.length
|
|
110
|
+
? maxChars
|
|
111
|
+
: maxChars >= 0
|
|
112
|
+
? Math.max(0, maxChars - this.additionLength)
|
|
113
|
+
: Math.min(0, maxChars + this.additionLength);
|
|
114
|
+
var slicedValue = adjustedChars !== undefined && adjustedChars > -1
|
|
115
|
+
? value.slice(0, adjustedChars)
|
|
116
|
+
: value.slice(adjustedChars);
|
|
117
|
+
// No cutoff, no terminator -> value only
|
|
118
|
+
if (maxChars == null ||
|
|
119
|
+
adjustedChars == null ||
|
|
120
|
+
adjustedChars === 0 ||
|
|
121
|
+
terminator == null ||
|
|
122
|
+
value.length <= Math.abs(maxChars)) {
|
|
123
|
+
return [slicedValue];
|
|
124
|
+
}
|
|
125
|
+
// Postpended cutoff.
|
|
126
|
+
if (adjustedChars > 0) {
|
|
127
|
+
return separator != null
|
|
128
|
+
? [slicedValue, separator, terminator]
|
|
129
|
+
: [slicedValue, terminator];
|
|
130
|
+
}
|
|
131
|
+
// Prepended cutoff.
|
|
132
|
+
else {
|
|
133
|
+
return separator != null
|
|
134
|
+
? [terminator, separator, slicedValue]
|
|
135
|
+
: [terminator, slicedValue];
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Get the resolved options
|
|
140
|
+
* @returns {ResolvedCutoffFormatOptions} The resolved options.
|
|
141
|
+
*/
|
|
142
|
+
CutoffFormatConstructor.prototype.resolvedOptions = function () {
|
|
143
|
+
return this.options;
|
|
144
|
+
};
|
|
145
|
+
return CutoffFormatConstructor;
|
|
146
|
+
}());
|
|
147
|
+
export { CutoffFormatConstructor };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CutoffFormatStyle, ResolvedTerminatorOptions } from './types';
|
|
2
|
+
export declare const DEFAULT_CUTOFF_FORMAT_STYLE: CutoffFormatStyle;
|
|
3
|
+
export declare const DEFAULT_TERMINATOR_KEY = "DEFAULT_TERMINATOR_KEY";
|
|
4
|
+
export declare const TERMINATOR_MAP: Record<CutoffFormatStyle, Record<string | typeof DEFAULT_TERMINATOR_KEY, ResolvedTerminatorOptions>>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var _a, _b;
|
|
2
|
+
export var DEFAULT_CUTOFF_FORMAT_STYLE = 'ellipsis';
|
|
3
|
+
export var DEFAULT_TERMINATOR_KEY = 'DEFAULT_TERMINATOR_KEY';
|
|
4
|
+
export var TERMINATOR_MAP = {
|
|
5
|
+
ellipsis: (_a = {
|
|
6
|
+
fr: {
|
|
7
|
+
terminator: '…',
|
|
8
|
+
separator: '\u202F',
|
|
9
|
+
},
|
|
10
|
+
zh: {
|
|
11
|
+
terminator: '……',
|
|
12
|
+
separator: undefined,
|
|
13
|
+
},
|
|
14
|
+
ja: {
|
|
15
|
+
terminator: '……',
|
|
16
|
+
separator: undefined,
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
_a[DEFAULT_TERMINATOR_KEY] = {
|
|
20
|
+
terminator: '…',
|
|
21
|
+
separator: undefined,
|
|
22
|
+
},
|
|
23
|
+
_a),
|
|
24
|
+
none: (_b = {},
|
|
25
|
+
_b[DEFAULT_TERMINATOR_KEY] = {
|
|
26
|
+
terminator: undefined,
|
|
27
|
+
separator: undefined,
|
|
28
|
+
},
|
|
29
|
+
_b),
|
|
30
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Type of terminator */
|
|
2
|
+
export type CutoffFormatStyle = 'none' | 'ellipsis';
|
|
3
|
+
/** Terminator options. */
|
|
4
|
+
export interface TerminatorOptions {
|
|
5
|
+
/** The terminator to use. */
|
|
6
|
+
terminator?: string;
|
|
7
|
+
/** An optional separator between the terminator and the value */
|
|
8
|
+
separator?: string;
|
|
9
|
+
}
|
|
10
|
+
/** Input formatting options (for constructor) */
|
|
11
|
+
export interface CutoffFormatOptions extends TerminatorOptions {
|
|
12
|
+
/** Cutoff length. */
|
|
13
|
+
maxChars?: number;
|
|
14
|
+
/** Type of terminator */
|
|
15
|
+
style?: CutoffFormatStyle;
|
|
16
|
+
}
|
|
17
|
+
/** Resolved terminator options */
|
|
18
|
+
export interface ResolvedTerminatorOptions extends TerminatorOptions {
|
|
19
|
+
terminator: string | undefined;
|
|
20
|
+
separator: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** Resolved options (after constructor) */
|
|
23
|
+
export interface ResolvedCutoffFormatOptions extends CutoffFormatOptions, ResolvedTerminatorOptions {
|
|
24
|
+
maxChars: number | undefined;
|
|
25
|
+
style: CutoffFormatStyle | undefined;
|
|
26
|
+
terminator: string | undefined;
|
|
27
|
+
separator: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Prepended cutoff list */
|
|
30
|
+
export type PrependedCutoffParts = [string] | [ResolvedTerminatorOptions['terminator'], string] | [
|
|
31
|
+
ResolvedTerminatorOptions['terminator'],
|
|
32
|
+
ResolvedTerminatorOptions['separator'],
|
|
33
|
+
string
|
|
34
|
+
];
|
|
35
|
+
/** Postpended cutoff list */
|
|
36
|
+
export type PostpendedCutoffParts = [string] | [string, ResolvedTerminatorOptions['terminator']] | [
|
|
37
|
+
string,
|
|
38
|
+
ResolvedTerminatorOptions['separator'],
|
|
39
|
+
ResolvedTerminatorOptions['terminator']
|
|
40
|
+
];
|
|
41
|
+
/**
|
|
42
|
+
* Cutoff Class interface
|
|
43
|
+
*/
|
|
44
|
+
export interface CutoffFormat {
|
|
45
|
+
format: (value: string) => string;
|
|
46
|
+
resolvedOptions: () => ResolvedCutoffFormatOptions;
|
|
47
|
+
formatToParts: (value: string) => PrependedCutoffParts | PostpendedCutoffParts;
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|