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,257 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
import { intlCache } from '../cache/IntlCache';
|
|
13
|
+
import { libraryDefaultLocale } from '../settings/settings';
|
|
14
|
+
import { IntlMessageFormat } from 'intl-messageformat';
|
|
15
|
+
import { formatI18nextWarning, formatJsxWarning } from '../logging/warnings';
|
|
16
|
+
import { formattingLogger } from '../logging/logger';
|
|
17
|
+
/**
|
|
18
|
+
* Formats a string value with cutoff behavior according to the specified locales and options.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} params - The parameters for the cutoff formatting.
|
|
21
|
+
* @param {string} params.value - The string value to format with cutoff behavior.
|
|
22
|
+
* @param {string | string[]} [params.locales='en'] - The locales to use for formatting.
|
|
23
|
+
* @param {CutoffFormatOptions} [params.options={}] - Additional options for cutoff formatting.
|
|
24
|
+
* @param {number} [params.options.maxChars] - The maximum number of characters to display.
|
|
25
|
+
* @param {CutoffFormatStyle} [params.options.style='ellipsis'] - The style of the terminator.
|
|
26
|
+
* @param {string} [params.options.terminator] - Optional override for the terminator to use.
|
|
27
|
+
* @param {string} [params.options.separator] - Optional override for the separator between terminator and value.
|
|
28
|
+
*
|
|
29
|
+
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
30
|
+
* @internal
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* _formatCutoff({ value: 'Hello, world!', options: { maxChars: 8 } }); // Returns 'Hello, w...'
|
|
34
|
+
*/
|
|
35
|
+
export function _formatCutoff(_a) {
|
|
36
|
+
var value = _a.value, _b = _a.locales, locales = _b === void 0 ? libraryDefaultLocale : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
37
|
+
return intlCache.get('CutoffFormat', locales, options).format(value);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Formats a message according to the specified locales and options.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} message - The message to format.
|
|
43
|
+
* @param {string | string[]} [locales='en'] - The locales to use for formatting.
|
|
44
|
+
* @param {Record<string, any>} [variables={}] - The variables to use for formatting.
|
|
45
|
+
* @returns {string} The formatted message.
|
|
46
|
+
* @internal
|
|
47
|
+
*
|
|
48
|
+
* Returns an empty string if IntlMessageFormat produces no output.
|
|
49
|
+
* TODO: Add this to custom formats.
|
|
50
|
+
*/
|
|
51
|
+
export function _formatMessageICU(message, locales, variables) {
|
|
52
|
+
var _a, _b;
|
|
53
|
+
if (locales === void 0) { locales = libraryDefaultLocale; }
|
|
54
|
+
if (variables === void 0) { variables = {}; }
|
|
55
|
+
var messageFormat = new IntlMessageFormat(message, locales);
|
|
56
|
+
return (_b = (_a = messageFormat.format(variables)) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '';
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns the message as-is without any formatting.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} message - The message to return.
|
|
62
|
+
* @returns {string} The original message, unchanged.
|
|
63
|
+
* @internal
|
|
64
|
+
*
|
|
65
|
+
* TODO: Add this to custom formats.
|
|
66
|
+
*/
|
|
67
|
+
export function _formatMessageString(message) {
|
|
68
|
+
return message;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Formats a number according to the specified locales and options.
|
|
72
|
+
*
|
|
73
|
+
* @param {Object} params - The parameters for the number formatting.
|
|
74
|
+
* @param {number} params.value - The number to format.
|
|
75
|
+
* @param {string | string[]} [params.locales=['en']] - The locales to use for formatting.
|
|
76
|
+
* @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
|
|
77
|
+
*
|
|
78
|
+
* @returns {string} The formatted number.
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
export function _formatNum(_a) {
|
|
82
|
+
var value = _a.value, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
83
|
+
var res = intlCache
|
|
84
|
+
.get('NumberFormat', locales, __assign({ numberingSystem: 'latn' }, options))
|
|
85
|
+
.format(value);
|
|
86
|
+
return res;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Formats a date according to the specified locales and options.
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} params - The parameters for the date formatting.
|
|
92
|
+
* @param {Date} params.value - The date to format.
|
|
93
|
+
* @param {string | string[]} [params.locales='en'] - The locales to use for formatting.
|
|
94
|
+
* @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
|
|
95
|
+
*
|
|
96
|
+
* @returns {string} The formatted date.
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
export function _formatDateTime(_a) {
|
|
100
|
+
var value = _a.value, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
101
|
+
return intlCache
|
|
102
|
+
.get('DateTimeFormat', locales, __assign({ calendar: 'gregory', numberingSystem: 'latn' }, options))
|
|
103
|
+
.format(value);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Formats a currency value according to the specified locales, currency, and options.
|
|
107
|
+
*
|
|
108
|
+
* @param {Object} params - The parameters for the currency formatting.
|
|
109
|
+
* @param {number} params.value - The currency value to format.
|
|
110
|
+
* @param {string} params.currency - The currency code (e.g., 'USD').
|
|
111
|
+
* @param {string | string[]} [params.locales=['en']] - The locales to use for formatting.
|
|
112
|
+
* @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
|
|
113
|
+
*
|
|
114
|
+
* @returns {string} The formatted currency value.
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
export function _formatCurrency(_a) {
|
|
118
|
+
var value = _a.value, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.currency, currency = _c === void 0 ? 'USD' : _c, _d = _a.options, options = _d === void 0 ? {} : _d;
|
|
119
|
+
return intlCache
|
|
120
|
+
.get('NumberFormat', locales, __assign({ style: 'currency', currency: currency, numberingSystem: 'latn' }, options))
|
|
121
|
+
.format(value);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Formats a list of items according to the specified locales and options.
|
|
125
|
+
*
|
|
126
|
+
* @param {Object} params - The parameters for the list formatting.
|
|
127
|
+
* @param {Array<string | number>} params.value - The list of items to format.
|
|
128
|
+
* @param {string | string[]} [params.locales=['en']] - The locales to use for formatting.
|
|
129
|
+
* @param {Intl.ListFormatOptions} [params.options={}] - Additional options for list formatting.
|
|
130
|
+
*
|
|
131
|
+
* @returns {string} The formatted list.
|
|
132
|
+
* @internal
|
|
133
|
+
*/
|
|
134
|
+
export function _formatList(_a) {
|
|
135
|
+
var value = _a.value, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
136
|
+
return intlCache
|
|
137
|
+
.get('ListFormat', locales, __assign({ type: 'conjunction', style: 'long' }, options))
|
|
138
|
+
.format(value.map(String));
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Formats a list of items according to the specified locales and options.
|
|
142
|
+
* @param {Object} params - The parameters for the list formatting.
|
|
143
|
+
* @param {Array<T>} params.value - The list of items to format.
|
|
144
|
+
* @param {string | string[]} [params.locales=['en']] - The locales to use for formatting.
|
|
145
|
+
* @param {Intl.ListFormatOptions} [params.options={}] - Additional options for list formatting.
|
|
146
|
+
* @returns {Array<T | string>} The formatted list parts.
|
|
147
|
+
* @internal
|
|
148
|
+
*/
|
|
149
|
+
export function _formatListToParts(_a) {
|
|
150
|
+
var value = _a.value, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
151
|
+
var formatListParts = intlCache
|
|
152
|
+
.get('ListFormat', locales, __assign({ type: 'conjunction', style: 'long' }, options))
|
|
153
|
+
.formatToParts(value.map(function () { return '1'; }));
|
|
154
|
+
var partIndex = 0;
|
|
155
|
+
return formatListParts.map(function (part) {
|
|
156
|
+
if (part.type === 'element')
|
|
157
|
+
return value[partIndex++];
|
|
158
|
+
return part.value;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Selects the best unit and computes the value for relative time formatting
|
|
163
|
+
* based on the difference between a date and a base date.
|
|
164
|
+
* @param {Date} date - The target date.
|
|
165
|
+
* @param {Date} baseDate - The base date to compute relative time from. Must be provided by the caller for hydration safety.
|
|
166
|
+
* @returns {{ value: number, unit: Intl.RelativeTimeFormatUnit }} The computed value and unit.
|
|
167
|
+
* @internal
|
|
168
|
+
*/
|
|
169
|
+
export function _selectRelativeTimeUnit(date, baseDate) {
|
|
170
|
+
var now = baseDate.getTime();
|
|
171
|
+
var diffMs = date.getTime() - now;
|
|
172
|
+
var absDiffMs = Math.abs(diffMs);
|
|
173
|
+
var sign = diffMs < 0 ? -1 : 1;
|
|
174
|
+
// Use Math.floor to avoid confusing jumps near boundaries
|
|
175
|
+
// (e.g. 3.5 days rounding to "1 week ago" instead of "3 days ago")
|
|
176
|
+
var seconds = Math.floor(absDiffMs / 1000);
|
|
177
|
+
var minutes = Math.floor(absDiffMs / (1000 * 60));
|
|
178
|
+
var hours = Math.floor(absDiffMs / (1000 * 60 * 60));
|
|
179
|
+
var days = Math.floor(absDiffMs / (1000 * 60 * 60 * 24));
|
|
180
|
+
var weeks = Math.floor(absDiffMs / (1000 * 60 * 60 * 24 * 7));
|
|
181
|
+
var months = Math.floor(absDiffMs / (1000 * 60 * 60 * 24 * 30));
|
|
182
|
+
var years = Math.floor(absDiffMs / (1000 * 60 * 60 * 24 * 365));
|
|
183
|
+
if (seconds < 60)
|
|
184
|
+
return { value: sign * seconds, unit: 'second' };
|
|
185
|
+
if (minutes < 60)
|
|
186
|
+
return { value: sign * minutes, unit: 'minute' };
|
|
187
|
+
if (hours < 24)
|
|
188
|
+
return { value: sign * hours, unit: 'hour' };
|
|
189
|
+
if (days < 7)
|
|
190
|
+
return { value: sign * days, unit: 'day' };
|
|
191
|
+
if (days < 28)
|
|
192
|
+
return { value: sign * weeks, unit: 'week' };
|
|
193
|
+
if (months < 1)
|
|
194
|
+
return { value: sign * weeks, unit: 'week' };
|
|
195
|
+
if (months < 12)
|
|
196
|
+
return { value: sign * months, unit: 'month' };
|
|
197
|
+
if (years < 1)
|
|
198
|
+
return { value: sign * months, unit: 'month' };
|
|
199
|
+
return { value: sign * years, unit: 'year' };
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Formats a relative time from a Date, automatically selecting the best unit.
|
|
203
|
+
* @internal
|
|
204
|
+
*/
|
|
205
|
+
export function _formatRelativeTimeFromDate(_a) {
|
|
206
|
+
var date = _a.date, baseDate = _a.baseDate, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
207
|
+
var _d = _selectRelativeTimeUnit(date, baseDate), value = _d.value, unit = _d.unit;
|
|
208
|
+
return _formatRelativeTime({ value: value, unit: unit, locales: locales, options: options });
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Formats a relative time value according to the specified locales and options.
|
|
212
|
+
*
|
|
213
|
+
* @param {Object} params - The parameters for the relative time formatting.
|
|
214
|
+
* @param {number} params.value - The relative time value to format.
|
|
215
|
+
* @param {Intl.RelativeTimeFormatUnit} params.unit - The unit of time (e.g., 'second', 'minute', 'hour', 'day', 'week', 'month', 'year').
|
|
216
|
+
* @param {string | string[]} [params.locales=['en']] - The locales to use for formatting.
|
|
217
|
+
* @param {Intl.RelativeTimeFormatOptions} [params.options={}] - Additional options for relative time formatting.
|
|
218
|
+
*
|
|
219
|
+
* @returns {string} The formatted relative time string.
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
export function _formatRelativeTime(_a) {
|
|
223
|
+
var value = _a.value, unit = _a.unit, _b = _a.locales, locales = _b === void 0 ? [libraryDefaultLocale] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
|
224
|
+
return intlCache
|
|
225
|
+
.get('RelativeTimeFormat', locales, __assign({ style: 'long', numeric: 'auto' }, options))
|
|
226
|
+
.format(value, unit);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* @experimental This function is not currently supported but will be implemented in a future version.
|
|
230
|
+
* Use {@link _formatMessageICU} for current ICU message format support.
|
|
231
|
+
* Formats an I18next message according to the specified locales and options.
|
|
232
|
+
*
|
|
233
|
+
* @param message - The I18next message to format.
|
|
234
|
+
* @param variables - The variables to use for formatting.
|
|
235
|
+
* @returns The formatted I18next message.
|
|
236
|
+
* @internal
|
|
237
|
+
*/
|
|
238
|
+
export function _formatI18next(message, _variables) {
|
|
239
|
+
if (_variables === void 0) { _variables = {}; }
|
|
240
|
+
formattingLogger.warn(formatI18nextWarning);
|
|
241
|
+
return message;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* @experimental This function is not currently supported but will be implemented in a future version.
|
|
245
|
+
* Use {@link _formatMessageICU} for current ICU message format support.
|
|
246
|
+
* Formats a JSX message according to the specified locales and options.
|
|
247
|
+
*
|
|
248
|
+
* @param message - The JSX message to format.
|
|
249
|
+
* @param variables - The variables to use for formatting.
|
|
250
|
+
* @returns The formatted JSX message.
|
|
251
|
+
* @internal
|
|
252
|
+
*/
|
|
253
|
+
export function _formatJsx(message, _variables) {
|
|
254
|
+
if (_variables === void 0) { _variables = {}; }
|
|
255
|
+
formattingLogger.warn(formatJsxWarning);
|
|
256
|
+
return message;
|
|
257
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { JsxChildren } from '../types';
|
|
2
|
+
import { HashMetadata } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Calculates a unique hash for a given string using SHA-256.
|
|
5
|
+
*
|
|
6
|
+
* First 16 characters of hash, hex encoded.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} string - The string to be hashed.
|
|
9
|
+
* @returns {string} The resulting hash as a hexadecimal string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function hashString(string: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
|
|
14
|
+
*
|
|
15
|
+
* @param {any} childrenAsObjects - The children objects to be hashed.
|
|
16
|
+
* @param {string} [context] - The context for the children.
|
|
17
|
+
* @param {string} [id] - The ID for the JSX children object.
|
|
18
|
+
* @param {number} [maxChars] - The maxChars limit for the JSX children object.
|
|
19
|
+
* @param {string} [dataFormat] - The data format of the sources.
|
|
20
|
+
* @param {function} [hashFunction] - Custom hash function.
|
|
21
|
+
* @returns {string} - The unique hash of the children.
|
|
22
|
+
*/
|
|
23
|
+
export declare function hashSource({ source, context, id, maxChars, dataFormat, }: {
|
|
24
|
+
source: JsxChildren | string;
|
|
25
|
+
} & HashMetadata, hashFunction?: (string: string) => string): string;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Functions provided to other GT libraries
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
import { stableStringify as stringify } from '../utils/stableStringify';
|
|
14
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
15
|
+
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';
|
|
16
|
+
import isVariable from '../utils/isVariable';
|
|
17
|
+
// ----- FUNCTIONS ----- //
|
|
18
|
+
/**
|
|
19
|
+
* Calculates a unique hash for a given string using SHA-256.
|
|
20
|
+
*
|
|
21
|
+
* First 16 characters of hash, hex encoded.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} string - The string to be hashed.
|
|
24
|
+
* @returns {string} The resulting hash as a hexadecimal string.
|
|
25
|
+
*/
|
|
26
|
+
export function hashString(string) {
|
|
27
|
+
return bytesToHex(sha256(utf8ToBytes(string))).slice(0, 16);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
|
|
31
|
+
*
|
|
32
|
+
* @param {any} childrenAsObjects - The children objects to be hashed.
|
|
33
|
+
* @param {string} [context] - The context for the children.
|
|
34
|
+
* @param {string} [id] - The ID for the JSX children object.
|
|
35
|
+
* @param {number} [maxChars] - The maxChars limit for the JSX children object.
|
|
36
|
+
* @param {string} [dataFormat] - The data format of the sources.
|
|
37
|
+
* @param {function} [hashFunction] - Custom hash function.
|
|
38
|
+
* @returns {string} - The unique hash of the children.
|
|
39
|
+
*/
|
|
40
|
+
export function hashSource(_a, hashFunction) {
|
|
41
|
+
var source = _a.source, context = _a.context, id = _a.id, maxChars = _a.maxChars, dataFormat = _a.dataFormat;
|
|
42
|
+
if (hashFunction === void 0) { hashFunction = hashString; }
|
|
43
|
+
var sanitizedSource;
|
|
44
|
+
if (dataFormat === 'JSX') {
|
|
45
|
+
sanitizedSource = sanitizeJsxChildren(source);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
sanitizedSource = source;
|
|
49
|
+
}
|
|
50
|
+
var sanitizedData = __assign(__assign(__assign(__assign({ source: sanitizedSource }, (id && { id: id })), (context && { context: context })), (maxChars != null && { maxChars: Math.abs(maxChars) })), (dataFormat && { dataFormat: dataFormat }));
|
|
51
|
+
var stringifiedData = stringify(sanitizedData);
|
|
52
|
+
return hashFunction(stringifiedData);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Sanitizes a child object by removing the data-_gt attribute and its branches.
|
|
56
|
+
*
|
|
57
|
+
* @param child - The child object to sanitize.
|
|
58
|
+
* @returns The sanitized child object.
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
var sanitizeChild = function (child) {
|
|
62
|
+
if (child && typeof child === 'object') {
|
|
63
|
+
var newChild = {};
|
|
64
|
+
if ('c' in child && child.c) {
|
|
65
|
+
newChild.c = sanitizeJsxChildren(child.c);
|
|
66
|
+
}
|
|
67
|
+
if ('d' in child) {
|
|
68
|
+
var generaltranslation = child === null || child === void 0 ? void 0 : child.d;
|
|
69
|
+
if (generaltranslation === null || generaltranslation === void 0 ? void 0 : generaltranslation.b) {
|
|
70
|
+
// The only thing that prevents sanitizeJsx from being stable is
|
|
71
|
+
// the order of the keys in the branches object.
|
|
72
|
+
// We don't sort them because stable-stringify sorts them anyways
|
|
73
|
+
newChild.b = Object.fromEntries(Object.entries(generaltranslation.b).map(function (_a) {
|
|
74
|
+
var key = _a[0], value = _a[1];
|
|
75
|
+
return [
|
|
76
|
+
key,
|
|
77
|
+
sanitizeJsxChildren(value),
|
|
78
|
+
];
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
if (generaltranslation === null || generaltranslation === void 0 ? void 0 : generaltranslation.t) {
|
|
82
|
+
newChild.t = generaltranslation.t;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (isVariable(child)) {
|
|
86
|
+
return __assign({ k: child.k }, (child.v && {
|
|
87
|
+
v: child.v,
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
return newChild;
|
|
91
|
+
}
|
|
92
|
+
return child;
|
|
93
|
+
};
|
|
94
|
+
function sanitizeJsxChildren(childrenAsObjects) {
|
|
95
|
+
return Array.isArray(childrenAsObjects)
|
|
96
|
+
? childrenAsObjects.map(sanitizeChild)
|
|
97
|
+
: sanitizeChild(childrenAsObjects);
|
|
98
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { hashString } from './hashSource';
|
|
2
|
+
import { stableStringify as stringify } from '../utils/stableStringify';
|
|
3
|
+
export default function hashTemplate(template, hashFunction) {
|
|
4
|
+
if (hashFunction === void 0) { hashFunction = hashString; }
|
|
5
|
+
return hashFunction(stringify(template));
|
|
6
|
+
}
|
package/dist/id/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const require_isVariable = require("./isVariable-B08mggBy.cjs");
|
|
2
2
|
//#region src/id/hashSource.ts
|
|
3
3
|
/**
|
|
4
|
-
* Calculates a unique hash for a given string using
|
|
4
|
+
* Calculates a unique hash for a given string using SHA-256.
|
|
5
5
|
*
|
|
6
6
|
* First 16 characters of hash, hex encoded.
|
|
7
7
|
*
|
|
8
8
|
* @param {string} string - The string to be hashed.
|
|
9
|
-
* @returns {string}
|
|
9
|
+
* @returns {string} The resulting hash as a hexadecimal string.
|
|
10
10
|
*/
|
|
11
11
|
function hashString(string) {
|
|
12
12
|
return require_isVariable.bytesToHex(require_isVariable.sha256(require_isVariable.utf8ToBytes(string))).slice(0, 16);
|
|
@@ -15,12 +15,12 @@ function hashString(string) {
|
|
|
15
15
|
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
|
|
16
16
|
*
|
|
17
17
|
* @param {any} childrenAsObjects - The children objects to be hashed.
|
|
18
|
-
* @param {string} [context] - The context for the children
|
|
19
|
-
* @param {string} [id] - The
|
|
20
|
-
* @param {number} [maxChars] - The maxChars for the JSX
|
|
21
|
-
* @param {string} [dataFormat] - The data format of the sources
|
|
22
|
-
* @param {function} [hashFunction]
|
|
23
|
-
* @returns {string} - The unique
|
|
18
|
+
* @param {string} [context] - The context for the children.
|
|
19
|
+
* @param {string} [id] - The ID for the JSX children object.
|
|
20
|
+
* @param {number} [maxChars] - The maxChars limit for the JSX children object.
|
|
21
|
+
* @param {string} [dataFormat] - The data format of the sources.
|
|
22
|
+
* @param {function} [hashFunction] - Custom hash function.
|
|
23
|
+
* @returns {string} - The unique hash of the children.
|
|
24
24
|
*/
|
|
25
25
|
function hashSource({ source, context, id, maxChars, dataFormat }, hashFunction = hashString) {
|
|
26
26
|
let sanitizedSource;
|
|
@@ -86,4 +86,4 @@ Object.defineProperty(exports, "hashTemplate", {
|
|
|
86
86
|
}
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
-
//# sourceMappingURL=id-
|
|
89
|
+
//# sourceMappingURL=id-CyiXsQrY.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-CyiXsQrY.cjs","names":["bytesToHex","sha256","utf8ToBytes","stringify","isVariable","stringify"],"sources":["../src/id/hashSource.ts","../src/id/hashTemplate.ts"],"sourcesContent":["// Functions provided to other GT libraries\n\nimport { JsxChild, JsxChildren, Variable } from '../types';\nimport { stableStringify as stringify } from '../utils/stableStringify';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';\nimport isVariable from '../utils/isVariable';\nimport { HashMetadata } from './types';\n\n// ----- FUNCTIONS ----- //\n/**\n * Calculates a unique hash for a given string using SHA-256.\n *\n * First 16 characters of hash, hex encoded.\n *\n * @param {string} string - The string to be hashed.\n * @returns {string} The resulting hash as a hexadecimal string.\n */\nexport function hashString(string: string): string {\n return bytesToHex(sha256(utf8ToBytes(string))).slice(0, 16);\n}\n\n/**\n * Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.\n *\n * @param {any} childrenAsObjects - The children objects to be hashed.\n * @param {string} [context] - The context for the children.\n * @param {string} [id] - The ID for the JSX children object.\n * @param {number} [maxChars] - The maxChars limit for the JSX children object.\n * @param {string} [dataFormat] - The data format of the sources.\n * @param {function} [hashFunction] - Custom hash function.\n * @returns {string} - The unique hash of the children.\n */\nexport function hashSource(\n {\n source,\n context,\n id,\n maxChars,\n dataFormat,\n }: {\n source: JsxChildren | string;\n } & HashMetadata,\n hashFunction: (string: string) => string = hashString\n): string {\n let sanitizedSource: SanitizedChildren | string;\n if (dataFormat === 'JSX') {\n sanitizedSource = sanitizeJsxChildren(source);\n } else {\n sanitizedSource = source as string;\n }\n const sanitizedData: {\n source?: SanitizedChildren;\n } & HashMetadata = {\n source: sanitizedSource,\n ...(id && { id }),\n ...(context && { context }),\n ...(maxChars != null && { maxChars: Math.abs(maxChars) }),\n ...(dataFormat && { dataFormat }),\n };\n const stringifiedData = stringify(sanitizedData);\n return hashFunction(stringifiedData);\n}\n\ntype SanitizedVariable = Omit<Variable, 'i'>;\n\ntype SanitizedElement = {\n b?: {\n [k: string]: SanitizedChildren; // Branches\n };\n c?: SanitizedChildren; // Children\n t?: string; // Branch Transformation\n};\ntype SanitizedChild = SanitizedElement | SanitizedVariable | string;\ntype SanitizedChildren = SanitizedChild | SanitizedChild[];\n\n/**\n * Sanitizes a child object by removing the data-_gt attribute and its branches.\n *\n * @param child - The child object to sanitize.\n * @returns The sanitized child object.\n *\n */\nconst sanitizeChild = (child: JsxChild): SanitizedChild => {\n if (child && typeof child === 'object') {\n const newChild: SanitizedChild = {};\n if ('c' in child && child.c) {\n newChild.c = sanitizeJsxChildren(child.c);\n }\n if ('d' in child) {\n const generaltranslation = child?.d;\n if (generaltranslation?.b) {\n // The only thing that prevents sanitizeJsx from being stable is\n // the order of the keys in the branches object.\n // We don't sort them because stable-stringify sorts them anyways\n newChild.b = Object.fromEntries(\n Object.entries(generaltranslation.b).map(([key, value]) => [\n key,\n sanitizeJsxChildren(value as JsxChildren),\n ])\n );\n }\n if (generaltranslation?.t) {\n newChild.t = generaltranslation.t;\n }\n }\n if (isVariable(child)) {\n return {\n k: child.k,\n ...(child.v && {\n v: child.v,\n }),\n };\n }\n return newChild;\n }\n return child;\n};\n\nfunction sanitizeJsxChildren(\n childrenAsObjects: JsxChildren\n): SanitizedChildren {\n return Array.isArray(childrenAsObjects)\n ? childrenAsObjects.map(sanitizeChild)\n : sanitizeChild(childrenAsObjects);\n}\n","import { hashString } from './hashSource';\nimport { stableStringify as stringify } from '../utils/stableStringify';\n\nexport default function hashTemplate(\n template: {\n [key: string]: string;\n },\n hashFunction = hashString\n): string {\n return hashFunction(stringify(template));\n}\n"],"mappings":";;;;;;;;;;AAkBA,SAAgB,WAAW,QAAwB;AACjD,QAAOA,mBAAAA,WAAWC,mBAAAA,OAAOC,mBAAAA,YAAY,OAAO,CAAC,CAAC,CAAC,MAAM,GAAG,GAAG;;;;;;;;;;;;;AAc7D,SAAgB,WACd,EACE,QACA,SACA,IACA,UACA,cAIF,eAA2C,YACnC;CACR,IAAI;AACJ,KAAI,eAAe,MACjB,mBAAkB,oBAAoB,OAAO;KAE7C,mBAAkB;AAYpB,QAAO,aADiBC,mBAAAA,gBAAU;EANhC,QAAQ;EACR,GAAI,MAAM,EAAE,IAAI;EAChB,GAAI,WAAW,EAAE,SAAS;EAC1B,GAAI,YAAY,QAAQ,EAAE,UAAU,KAAK,IAAI,SAAS,EAAE;EACxD,GAAI,cAAc,EAAE,YAAY;EAEa,CACZ,CAAC;;;;;;;;;AAsBtC,MAAM,iBAAiB,UAAoC;AACzD,KAAI,SAAS,OAAO,UAAU,UAAU;EACtC,MAAM,WAA2B,EAAE;AACnC,MAAI,OAAO,SAAS,MAAM,EACxB,UAAS,IAAI,oBAAoB,MAAM,EAAE;AAE3C,MAAI,OAAO,OAAO;GAChB,MAAM,qBAAqB,OAAO;AAClC,OAAI,oBAAoB,EAItB,UAAS,IAAI,OAAO,YAClB,OAAO,QAAQ,mBAAmB,EAAE,CAAC,KAAK,CAAC,KAAK,WAAW,CACzD,KACA,oBAAoB,MAAqB,CAC1C,CAAC,CACH;AAEH,OAAI,oBAAoB,EACtB,UAAS,IAAI,mBAAmB;;AAGpC,MAAIC,mBAAAA,WAAW,MAAM,CACnB,QAAO;GACL,GAAG,MAAM;GACT,GAAI,MAAM,KAAK,EACb,GAAG,MAAM,GACV;GACF;AAEH,SAAO;;AAET,QAAO;;AAGT,SAAS,oBACP,mBACmB;AACnB,QAAO,MAAM,QAAQ,kBAAkB,GACnC,kBAAkB,IAAI,cAAc,GACpC,cAAc,kBAAkB;;;;ACzHtC,SAAwB,aACtB,UAGA,eAAe,YACP;AACR,QAAO,aAAaC,mBAAAA,gBAAU,SAAS,CAAC"}
|
|
@@ -3,12 +3,12 @@ import { sha256 } from "@noble/hashes/sha2.js";
|
|
|
3
3
|
import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js";
|
|
4
4
|
//#region src/id/hashSource.ts
|
|
5
5
|
/**
|
|
6
|
-
* Calculates a unique hash for a given string using
|
|
6
|
+
* Calculates a unique hash for a given string using SHA-256.
|
|
7
7
|
*
|
|
8
8
|
* First 16 characters of hash, hex encoded.
|
|
9
9
|
*
|
|
10
10
|
* @param {string} string - The string to be hashed.
|
|
11
|
-
* @returns {string}
|
|
11
|
+
* @returns {string} The resulting hash as a hexadecimal string.
|
|
12
12
|
*/
|
|
13
13
|
function hashString(string) {
|
|
14
14
|
return bytesToHex(sha256(utf8ToBytes(string))).slice(0, 16);
|
|
@@ -17,12 +17,12 @@ function hashString(string) {
|
|
|
17
17
|
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
|
|
18
18
|
*
|
|
19
19
|
* @param {any} childrenAsObjects - The children objects to be hashed.
|
|
20
|
-
* @param {string} [context] - The context for the children
|
|
21
|
-
* @param {string} [id] - The
|
|
22
|
-
* @param {number} [maxChars] - The maxChars for the JSX
|
|
23
|
-
* @param {string} [dataFormat] - The data format of the sources
|
|
24
|
-
* @param {function} [hashFunction]
|
|
25
|
-
* @returns {string} - The unique
|
|
20
|
+
* @param {string} [context] - The context for the children.
|
|
21
|
+
* @param {string} [id] - The ID for the JSX children object.
|
|
22
|
+
* @param {number} [maxChars] - The maxChars limit for the JSX children object.
|
|
23
|
+
* @param {string} [dataFormat] - The data format of the sources.
|
|
24
|
+
* @param {function} [hashFunction] - Custom hash function.
|
|
25
|
+
* @returns {string} - The unique hash of the children.
|
|
26
26
|
*/
|
|
27
27
|
function hashSource({ source, context, id, maxChars, dataFormat }, hashFunction = hashString) {
|
|
28
28
|
let sanitizedSource;
|
|
@@ -71,4 +71,4 @@ function hashTemplate(template, hashFunction = hashString) {
|
|
|
71
71
|
//#endregion
|
|
72
72
|
export { hashSource as n, hashString as r, hashTemplate as t };
|
|
73
73
|
|
|
74
|
-
//# sourceMappingURL=id-
|
|
74
|
+
//# sourceMappingURL=id-DbD7K-HL.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id-DbD7K-HL.mjs","names":["stringify","stringify"],"sources":["../src/id/hashSource.ts","../src/id/hashTemplate.ts"],"sourcesContent":["// Functions provided to other GT libraries\n\nimport { JsxChild, JsxChildren, Variable } from '../types';\nimport { stableStringify as stringify } from '../utils/stableStringify';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';\nimport isVariable from '../utils/isVariable';\nimport { HashMetadata } from './types';\n\n// ----- FUNCTIONS ----- //\n/**\n * Calculates a unique hash for a given string using SHA-256.\n *\n * First 16 characters of hash, hex encoded.\n *\n * @param {string} string - The string to be hashed.\n * @returns {string} The resulting hash as a hexadecimal string.\n */\nexport function hashString(string: string): string {\n return bytesToHex(sha256(utf8ToBytes(string))).slice(0, 16);\n}\n\n/**\n * Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.\n *\n * @param {any} childrenAsObjects - The children objects to be hashed.\n * @param {string} [context] - The context for the children.\n * @param {string} [id] - The ID for the JSX children object.\n * @param {number} [maxChars] - The maxChars limit for the JSX children object.\n * @param {string} [dataFormat] - The data format of the sources.\n * @param {function} [hashFunction] - Custom hash function.\n * @returns {string} - The unique hash of the children.\n */\nexport function hashSource(\n {\n source,\n context,\n id,\n maxChars,\n dataFormat,\n }: {\n source: JsxChildren | string;\n } & HashMetadata,\n hashFunction: (string: string) => string = hashString\n): string {\n let sanitizedSource: SanitizedChildren | string;\n if (dataFormat === 'JSX') {\n sanitizedSource = sanitizeJsxChildren(source);\n } else {\n sanitizedSource = source as string;\n }\n const sanitizedData: {\n source?: SanitizedChildren;\n } & HashMetadata = {\n source: sanitizedSource,\n ...(id && { id }),\n ...(context && { context }),\n ...(maxChars != null && { maxChars: Math.abs(maxChars) }),\n ...(dataFormat && { dataFormat }),\n };\n const stringifiedData = stringify(sanitizedData);\n return hashFunction(stringifiedData);\n}\n\ntype SanitizedVariable = Omit<Variable, 'i'>;\n\ntype SanitizedElement = {\n b?: {\n [k: string]: SanitizedChildren; // Branches\n };\n c?: SanitizedChildren; // Children\n t?: string; // Branch Transformation\n};\ntype SanitizedChild = SanitizedElement | SanitizedVariable | string;\ntype SanitizedChildren = SanitizedChild | SanitizedChild[];\n\n/**\n * Sanitizes a child object by removing the data-_gt attribute and its branches.\n *\n * @param child - The child object to sanitize.\n * @returns The sanitized child object.\n *\n */\nconst sanitizeChild = (child: JsxChild): SanitizedChild => {\n if (child && typeof child === 'object') {\n const newChild: SanitizedChild = {};\n if ('c' in child && child.c) {\n newChild.c = sanitizeJsxChildren(child.c);\n }\n if ('d' in child) {\n const generaltranslation = child?.d;\n if (generaltranslation?.b) {\n // The only thing that prevents sanitizeJsx from being stable is\n // the order of the keys in the branches object.\n // We don't sort them because stable-stringify sorts them anyways\n newChild.b = Object.fromEntries(\n Object.entries(generaltranslation.b).map(([key, value]) => [\n key,\n sanitizeJsxChildren(value as JsxChildren),\n ])\n );\n }\n if (generaltranslation?.t) {\n newChild.t = generaltranslation.t;\n }\n }\n if (isVariable(child)) {\n return {\n k: child.k,\n ...(child.v && {\n v: child.v,\n }),\n };\n }\n return newChild;\n }\n return child;\n};\n\nfunction sanitizeJsxChildren(\n childrenAsObjects: JsxChildren\n): SanitizedChildren {\n return Array.isArray(childrenAsObjects)\n ? childrenAsObjects.map(sanitizeChild)\n : sanitizeChild(childrenAsObjects);\n}\n","import { hashString } from './hashSource';\nimport { stableStringify as stringify } from '../utils/stableStringify';\n\nexport default function hashTemplate(\n template: {\n [key: string]: string;\n },\n hashFunction = hashString\n): string {\n return hashFunction(stringify(template));\n}\n"],"mappings":";;;;;;;;;;;;AAkBA,SAAgB,WAAW,QAAwB;AACjD,QAAO,WAAW,OAAO,YAAY,OAAO,CAAC,CAAC,CAAC,MAAM,GAAG,GAAG;;;;;;;;;;;;;AAc7D,SAAgB,WACd,EACE,QACA,SACA,IACA,UACA,cAIF,eAA2C,YACnC;CACR,IAAI;AACJ,KAAI,eAAe,MACjB,mBAAkB,oBAAoB,OAAO;KAE7C,mBAAkB;AAYpB,QAAO,aADiBA,gBAAU;EANhC,QAAQ;EACR,GAAI,MAAM,EAAE,IAAI;EAChB,GAAI,WAAW,EAAE,SAAS;EAC1B,GAAI,YAAY,QAAQ,EAAE,UAAU,KAAK,IAAI,SAAS,EAAE;EACxD,GAAI,cAAc,EAAE,YAAY;EAEa,CACZ,CAAC;;;;;;;;;AAsBtC,MAAM,iBAAiB,UAAoC;AACzD,KAAI,SAAS,OAAO,UAAU,UAAU;EACtC,MAAM,WAA2B,EAAE;AACnC,MAAI,OAAO,SAAS,MAAM,EACxB,UAAS,IAAI,oBAAoB,MAAM,EAAE;AAE3C,MAAI,OAAO,OAAO;GAChB,MAAM,qBAAqB,OAAO;AAClC,OAAI,oBAAoB,EAItB,UAAS,IAAI,OAAO,YAClB,OAAO,QAAQ,mBAAmB,EAAE,CAAC,KAAK,CAAC,KAAK,WAAW,CACzD,KACA,oBAAoB,MAAqB,CAC1C,CAAC,CACH;AAEH,OAAI,oBAAoB,EACtB,UAAS,IAAI,mBAAmB;;AAGpC,MAAI,WAAW,MAAM,CACnB,QAAO;GACL,GAAG,MAAM;GACT,GAAI,MAAM,KAAK,EACb,GAAG,MAAM,GACV;GACF;AAEH,SAAO;;AAET,QAAO;;AAGT,SAAS,oBACP,mBACmB;AACnB,QAAO,MAAM,QAAQ,kBAAkB,GACnC,kBAAkB,IAAI,cAAc,GACpC,cAAc,kBAAkB;;;;ACzHtC,SAAwB,aACtB,UAGA,eAAe,YACP;AACR,QAAO,aAAaC,gBAAU,SAAS,CAAC"}
|
package/dist/id.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_id = require("./id-
|
|
2
|
+
const require_id = require("./id-CyiXsQrY.cjs");
|
|
3
3
|
exports.hashSource = require_id.hashSource;
|
|
4
4
|
exports.hashString = require_id.hashString;
|
|
5
5
|
exports.hashTemplate = require_id.hashTemplate;
|
package/dist/id.d.cts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Ot as JsxChildren, pt as HashMetadata } from "./types-Bf8_Apq_.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/id/hashSource.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Calculates a unique hash for a given string using
|
|
5
|
+
* Calculates a unique hash for a given string using SHA-256.
|
|
6
6
|
*
|
|
7
7
|
* First 16 characters of hash, hex encoded.
|
|
8
8
|
*
|
|
9
9
|
* @param {string} string - The string to be hashed.
|
|
10
|
-
* @returns {string}
|
|
10
|
+
* @returns {string} The resulting hash as a hexadecimal string.
|
|
11
11
|
*/
|
|
12
12
|
declare function hashString(string: string): string;
|
|
13
13
|
/**
|
|
14
14
|
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
|
|
15
15
|
*
|
|
16
16
|
* @param {any} childrenAsObjects - The children objects to be hashed.
|
|
17
|
-
* @param {string} [context] - The context for the children
|
|
18
|
-
* @param {string} [id] - The
|
|
19
|
-
* @param {number} [maxChars] - The maxChars for the JSX
|
|
20
|
-
* @param {string} [dataFormat] - The data format of the sources
|
|
21
|
-
* @param {function} [hashFunction]
|
|
22
|
-
* @returns {string} - The unique
|
|
17
|
+
* @param {string} [context] - The context for the children.
|
|
18
|
+
* @param {string} [id] - The ID for the JSX children object.
|
|
19
|
+
* @param {number} [maxChars] - The maxChars limit for the JSX children object.
|
|
20
|
+
* @param {string} [dataFormat] - The data format of the sources.
|
|
21
|
+
* @param {function} [hashFunction] - Custom hash function.
|
|
22
|
+
* @returns {string} - The unique hash of the children.
|
|
23
23
|
*/
|
|
24
24
|
declare function hashSource({
|
|
25
25
|
source,
|
package/dist/id.d.mts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Ot as JsxChildren, pt as HashMetadata } from "./types-AHtYZIP-.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/id/hashSource.d.ts
|
|
4
4
|
/**
|
|
5
|
-
* Calculates a unique hash for a given string using
|
|
5
|
+
* Calculates a unique hash for a given string using SHA-256.
|
|
6
6
|
*
|
|
7
7
|
* First 16 characters of hash, hex encoded.
|
|
8
8
|
*
|
|
9
9
|
* @param {string} string - The string to be hashed.
|
|
10
|
-
* @returns {string}
|
|
10
|
+
* @returns {string} The resulting hash as a hexadecimal string.
|
|
11
11
|
*/
|
|
12
12
|
declare function hashString(string: string): string;
|
|
13
13
|
/**
|
|
14
14
|
* Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.
|
|
15
15
|
*
|
|
16
16
|
* @param {any} childrenAsObjects - The children objects to be hashed.
|
|
17
|
-
* @param {string} [context] - The context for the children
|
|
18
|
-
* @param {string} [id] - The
|
|
19
|
-
* @param {number} [maxChars] - The maxChars for the JSX
|
|
20
|
-
* @param {string} [dataFormat] - The data format of the sources
|
|
21
|
-
* @param {function} [hashFunction]
|
|
22
|
-
* @returns {string} - The unique
|
|
17
|
+
* @param {string} [context] - The context for the children.
|
|
18
|
+
* @param {string} [id] - The ID for the JSX children object.
|
|
19
|
+
* @param {number} [maxChars] - The maxChars limit for the JSX children object.
|
|
20
|
+
* @param {string} [dataFormat] - The data format of the sources.
|
|
21
|
+
* @param {function} [hashFunction] - Custom hash function.
|
|
22
|
+
* @returns {string} - The unique hash of the children.
|
|
23
23
|
*/
|
|
24
24
|
declare function hashSource({
|
|
25
25
|
source,
|
package/dist/id.d.ts
ADDED
package/dist/id.js
ADDED
package/dist/id.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as hashSource, r as hashString, t as hashTemplate } from "./id-
|
|
1
|
+
import { n as hashSource, r as hashString, t as hashTemplate } from "./id-DbD7K-HL.mjs";
|
|
2
2
|
export { hashSource, hashString, hashTemplate };
|