generaltranslation 8.2.13 → 8.2.14
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 +11 -0
- package/dist/{ApiError-IYfaOR30.mjs → ApiError-0DxxIHLp.mjs} +1 -1
- package/dist/{ApiError-CZ45tkW6.cjs.map → ApiError-0DxxIHLp.mjs.map} +1 -1
- package/dist/{ApiError-CZ45tkW6.cjs → ApiError-D-IBuHj6.cjs} +1 -1
- package/dist/{ApiError-IYfaOR30.mjs.map → ApiError-D-IBuHj6.cjs.map} +1 -1
- package/dist/LocaleConfig.d.ts +1 -59
- package/dist/LocaleConfig.js +1 -225
- package/dist/{base64-2fu94Klt.cjs → base64-C6BHySOc.cjs} +17 -1
- package/dist/base64-C6BHySOc.cjs.map +1 -0
- package/dist/{base64-DH0STixb.mjs → base64-CWITCfhU.mjs} +6 -2
- package/dist/base64-CWITCfhU.mjs.map +1 -0
- package/dist/core.cjs +9 -8
- package/dist/core.d.cts +1 -2
- package/dist/core.d.mts +1 -2
- package/dist/core.d.ts +1 -128
- package/dist/core.js +1 -137
- package/dist/core.mjs +2 -2
- package/dist/derive/indexVars.d.ts +1 -1
- package/dist/errors.cjs +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/id/types.d.ts +1 -1
- package/dist/{id-CyiXsQrY.cjs → id-C2orn1MA.cjs} +2 -2
- package/dist/{id-CyiXsQrY.cjs.map → id-C2orn1MA.cjs.map} +1 -1
- package/dist/{id-DbD7K-HL.mjs → id-DEaFhGqX.mjs} +2 -2
- package/dist/{id-DbD7K-HL.mjs.map → id-DEaFhGqX.mjs.map} +1 -1
- package/dist/id.cjs +1 -1
- package/dist/id.d.cts +1 -1
- package/dist/id.d.mts +1 -1
- package/dist/id.mjs +1 -1
- package/dist/index.cjs +368 -385
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -242
- package/dist/index.d.mts +4 -242
- package/dist/index.d.ts +5 -238
- package/dist/index.js +3 -322
- package/dist/index.mjs +215 -352
- package/dist/index.mjs.map +1 -1
- package/dist/internal.cjs +6 -6
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +6 -5
- package/dist/internal.d.mts +6 -5
- package/dist/internal.mjs +4 -4
- package/dist/internal.mjs.map +1 -1
- package/dist/{isVariable-B08mggBy.cjs → isVariable-Ba1gLXdB.cjs} +1 -1
- package/dist/{isVariable-B08mggBy.cjs.map → isVariable-Ba1gLXdB.cjs.map} +1 -1
- package/dist/{isVariable-CYsKFHvR.mjs → isVariable-fAKEB7gF.mjs} +1 -1
- package/dist/{isVariable-CYsKFHvR.mjs.map → isVariable-fAKEB7gF.mjs.map} +1 -1
- package/dist/locales/getPluralForm.js +2 -2
- package/dist/logging/logger.d.ts +0 -3
- package/dist/logging/logger.js +0 -3
- package/dist/{types-AHtYZIP-.d.mts → types-73XFwmhH.d.mts} +7 -106
- package/dist/{types-Bf8_Apq_.d.cts → types-YrrGRHBP.d.cts} +7 -106
- package/dist/types-dir/api/enqueueEntries.d.ts +1 -1
- package/dist/types-dir/api/enqueueFiles.d.ts +1 -1
- package/dist/types-dir/api/fetchTranslations.d.ts +1 -1
- package/dist/types-dir/api/file.d.ts +1 -1
- package/dist/types-dir/api/translate.d.ts +1 -1
- package/dist/types-dir/api/uploadFiles.d.ts +1 -1
- package/dist/types.cjs +7 -16
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +10 -12
- package/dist/types.js +1 -2
- package/dist/types.mjs +1 -15
- package/package.json +3 -2
- package/dist/IntlCache-CAW8tKhd.cjs +0 -212
- package/dist/IntlCache-CAW8tKhd.cjs.map +0 -1
- package/dist/IntlCache-WZk0rKvj.mjs +0 -195
- package/dist/IntlCache-WZk0rKvj.mjs.map +0 -1
- package/dist/base64-2fu94Klt.cjs.map +0 -1
- package/dist/base64-DH0STixb.mjs.map +0 -1
- package/dist/cache/IntlCache.d.ts +0 -26
- package/dist/cache/IntlCache.js +0 -84
- package/dist/cache/types.d.ts +0 -32
- package/dist/cache/types.js +0 -1
- package/dist/core-7RP541eY.cjs +0 -1677
- package/dist/core-7RP541eY.cjs.map +0 -1
- package/dist/core-I9pWGafA.d.mts +0 -209
- package/dist/core-TLJoDpJP.d.cts +0 -209
- package/dist/core-isLphYAZ.mjs +0 -1498
- package/dist/core-isLphYAZ.mjs.map +0 -1
- package/dist/errors/formattingErrors.d.ts +0 -1
- package/dist/errors/formattingErrors.js +0 -3
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.d.ts +0 -59
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.js +0 -147
- package/dist/formatting/custom-formats/CutoffFormat/constants.d.ts +0 -4
- package/dist/formatting/custom-formats/CutoffFormat/constants.js +0 -30
- package/dist/formatting/custom-formats/CutoffFormat/types.d.ts +0 -48
- package/dist/formatting/custom-formats/CutoffFormat/types.js +0 -2
- package/dist/formatting/format.d.ts +0 -1
- package/dist/formatting/format.js +0 -257
- package/dist/locales/customLocaleMapping.d.ts +0 -11
- package/dist/locales/customLocaleMapping.js +0 -23
- package/dist/locales/determineLocale.d.ts +0 -1
- package/dist/locales/determineLocale.js +0 -72
- package/dist/locales/getLocaleDirection.d.ts +0 -1
- package/dist/locales/getLocaleDirection.js +0 -89
- package/dist/locales/getLocaleEmoji.d.ts +0 -2
- package/dist/locales/getLocaleEmoji.js +0 -319
- package/dist/locales/getLocaleName.d.ts +0 -1
- package/dist/locales/getLocaleName.js +0 -45
- package/dist/locales/getLocaleProperties.d.ts +0 -32
- package/dist/locales/getLocaleProperties.js +0 -220
- package/dist/locales/getRegionProperties.d.ts +0 -7
- package/dist/locales/getRegionProperties.js +0 -61
- package/dist/locales/isSameDialect.d.ts +0 -1
- package/dist/locales/isSameDialect.js +0 -41
- package/dist/locales/isSameLanguage.d.ts +0 -1
- package/dist/locales/isSameLanguage.js +0 -20
- package/dist/locales/isSupersetLocale.d.ts +0 -1
- package/dist/locales/isSupersetLocale.js +0 -22
- package/dist/locales/isValidLocale.d.ts +0 -1
- package/dist/locales/isValidLocale.js +0 -75
- package/dist/locales/requiresTranslation.d.ts +0 -1
- package/dist/locales/requiresTranslation.js +0 -32
- package/dist/locales/resolveAliasLocale.d.ts +0 -8
- package/dist/locales/resolveAliasLocale.js +0 -21
- package/dist/locales/resolveCanonicalLocale.d.ts +0 -8
- package/dist/locales/resolveCanonicalLocale.js +0 -13
- package/dist/logging/warnings.d.ts +0 -2
- package/dist/logging/warnings.js +0 -2
- package/dist/types-dir/jsx/content.d.ts +0 -61
- package/dist/types-dir/jsx/content.js +0 -11
- package/dist/types-dir/jsx/variables.d.ts +0 -9
- package/dist/types-dir/jsx/variables.js +0 -1
- package/dist/types.cjs.map +0 -1
- package/dist/types.mjs.map +0 -1
package/dist/core.js
CHANGED
|
@@ -1,137 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { _isValidLocale, _standardizeLocale } from './locales/isValidLocale';
|
|
3
|
-
import { _resolveCanonicalLocale } from './locales/resolveCanonicalLocale';
|
|
4
|
-
export { LocaleConfig, } from './LocaleConfig';
|
|
5
|
-
/**
|
|
6
|
-
* Core formatting and locale helpers.
|
|
7
|
-
*
|
|
8
|
-
* This entry point exposes deterministic locale and formatting primitives. It
|
|
9
|
-
* does not export the GT service client, project credentials, network
|
|
10
|
-
* translation methods, file APIs, or other server/service concerns from the
|
|
11
|
-
* root `generaltranslation` facade.
|
|
12
|
-
*
|
|
13
|
-
* This entry point is intended for framework and shared packages that need
|
|
14
|
-
* locale metadata or formatting behavior without pulling in the full
|
|
15
|
-
* translation API surface.
|
|
16
|
-
*/
|
|
17
|
-
/**
|
|
18
|
-
* Formats a string with cutoff behavior, applying a terminator when the string exceeds the maximum character limit.
|
|
19
|
-
*
|
|
20
|
-
* This standalone function provides cutoff formatting functionality without requiring a GT instance.
|
|
21
|
-
* The locales parameter is required for proper terminator selection based on the target language.
|
|
22
|
-
*
|
|
23
|
-
* @param {string} value - The string value to format with cutoff behavior.
|
|
24
|
-
* @param {Object} [options] - Configuration options for cutoff formatting.
|
|
25
|
-
* @param {string | string[]} [options.locales] - The locales to use for terminator selection.
|
|
26
|
-
* @param {number} [options.maxChars] - The maximum number of characters to display.
|
|
27
|
-
* - Undefined values are treated as no cutoff.
|
|
28
|
-
* - Negative values follow .slice() behavior and terminator will be added before the value.
|
|
29
|
-
* - 0 will result in an empty string.
|
|
30
|
-
* - If cutoff results in an empty string, no terminator is added.
|
|
31
|
-
* @param {CutoffFormatStyle} [options.style='ellipsis'] - The style of the terminator.
|
|
32
|
-
* @param {string} [options.terminator] - Optional override the terminator to use.
|
|
33
|
-
* @param {string} [options.separator] - Optional override the separator to use between the terminator and the value.
|
|
34
|
-
* - If no terminator is provided, then separator is ignored.
|
|
35
|
-
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* formatCutoff('Hello, world!', { locales: 'en-US', maxChars: 8 });
|
|
39
|
-
* // Returns: 'Hello, …'
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* formatCutoff('Hello, world!', { locales: 'en-US', maxChars: -3 });
|
|
43
|
-
* // Returns: '…d!'
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* formatCutoff('Very long text that needs cutting', {
|
|
47
|
-
* locales: 'en-US',
|
|
48
|
-
* maxChars: 15,
|
|
49
|
-
* style: 'ellipsis',
|
|
50
|
-
* separator: ' '
|
|
51
|
-
* });
|
|
52
|
-
* // Returns: 'Very long tex …'
|
|
53
|
-
*/
|
|
54
|
-
export function formatCutoff(value, options) {
|
|
55
|
-
return _formatCutoff({ value: value, locales: options === null || options === void 0 ? void 0 : options.locales, options: options });
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Formats a message according to the specified locales and options.
|
|
59
|
-
*
|
|
60
|
-
* @param {string} message - The message to format.
|
|
61
|
-
* @param {Object} [options] - Configuration options for message formatting.
|
|
62
|
-
* @param {string | string[]} [options.locales] - The locales to use for formatting.
|
|
63
|
-
* @param {FormatVariables} [options.variables] - The variables to use for formatting.
|
|
64
|
-
* @param {StringFormat} [options.dataFormat='ICU'] - The format of the message. When STRING, the message is returned as is.
|
|
65
|
-
* @returns {string} The formatted message.
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* formatMessage('Hello {name}', { variables: { name: 'John' } });
|
|
69
|
-
* // Returns: "Hello John"
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* formatMessage('Hello {name}', {
|
|
73
|
-
* locales: ['fr'],
|
|
74
|
-
* variables: { name: 'John' }
|
|
75
|
-
* });
|
|
76
|
-
*/
|
|
77
|
-
export function formatMessage(message, options) {
|
|
78
|
-
switch (options === null || options === void 0 ? void 0 : options.dataFormat) {
|
|
79
|
-
case 'STRING':
|
|
80
|
-
return _formatMessageString(message);
|
|
81
|
-
default:
|
|
82
|
-
return _formatMessageICU(message, options === null || options === void 0 ? void 0 : options.locales, options === null || options === void 0 ? void 0 : options.variables);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Checks if a given BCP 47 locale code is valid.
|
|
87
|
-
*
|
|
88
|
-
* @param {string} locale - The BCP 47 locale code to validate.
|
|
89
|
-
* @param {CustomMapping} [customMapping] - The custom mapping to use for validation.
|
|
90
|
-
* @returns {boolean} True if the BCP 47 code is valid, false otherwise.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* isValidLocale('en-US');
|
|
94
|
-
* // Returns: true
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* isValidLocale('en_US');
|
|
98
|
-
* // Returns: false
|
|
99
|
-
*/
|
|
100
|
-
export function isValidLocale(locale, customMapping) {
|
|
101
|
-
return _isValidLocale(locale, customMapping);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Resolves the canonical locale for a given locale.
|
|
105
|
-
*
|
|
106
|
-
* @param {string} locale - The locale to resolve the canonical locale for.
|
|
107
|
-
* @param {CustomMapping} [customMapping] - The custom mapping to use for resolving the canonical locale.
|
|
108
|
-
* @returns {string} The canonical locale, or the input locale when no canonical mapping exists.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* resolveCanonicalLocale('en-US');
|
|
112
|
-
* // Returns: 'en-US'
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* resolveCanonicalLocale('en', { en: 'en-US' });
|
|
116
|
-
* // Returns: 'en-US'
|
|
117
|
-
*/
|
|
118
|
-
export function resolveCanonicalLocale(locale, customMapping) {
|
|
119
|
-
return _resolveCanonicalLocale(locale, customMapping);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Standardizes a BCP 47 locale code to ensure correct formatting.
|
|
123
|
-
*
|
|
124
|
-
* @param {string} locale - The BCP 47 locale code to standardize.
|
|
125
|
-
* @returns {string} The standardized BCP 47 locale code, or the input string if it cannot be standardized.
|
|
126
|
-
*
|
|
127
|
-
* @example
|
|
128
|
-
* standardizeLocale('en-us');
|
|
129
|
-
* // Returns: 'en-US'
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* standardizeLocale('not a locale');
|
|
133
|
-
* // Returns: 'not a locale'
|
|
134
|
-
*/
|
|
135
|
-
export function standardizeLocale(locale) {
|
|
136
|
-
return _standardizeLocale(locale);
|
|
137
|
-
}
|
|
1
|
+
export * from '@generaltranslation/format';
|
package/dist/core.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
1
|
+
export * from "@generaltranslation/format";
|
|
2
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IcuMessage } from '
|
|
1
|
+
import type { IcuMessage } from '@generaltranslation/format/types';
|
|
2
2
|
/**
|
|
3
3
|
* Given an ICU string adds identifiers to each _gt_ placeholder
|
|
4
4
|
* indexVars('Hello {_gt_} {_gt_} World') => 'Hello {_gt_1_} {_gt_2_} World'
|
package/dist/errors.cjs
CHANGED
package/dist/errors.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as ApiError } from "./ApiError-
|
|
1
|
+
import { t as ApiError } from "./ApiError-0DxxIHLp.mjs";
|
|
2
2
|
export { ApiError };
|
package/dist/id/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_isVariable = require("./isVariable-
|
|
1
|
+
const require_isVariable = require("./isVariable-Ba1gLXdB.cjs");
|
|
2
2
|
//#region src/id/hashSource.ts
|
|
3
3
|
/**
|
|
4
4
|
* Calculates a unique hash for a given string using SHA-256.
|
|
@@ -86,4 +86,4 @@ Object.defineProperty(exports, "hashTemplate", {
|
|
|
86
86
|
}
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
-
//# sourceMappingURL=id-
|
|
89
|
+
//# sourceMappingURL=id-C2orn1MA.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id-
|
|
1
|
+
{"version":3,"file":"id-C2orn1MA.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"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as stableStringify, t as isVariable } from "./isVariable-
|
|
1
|
+
import { n as stableStringify, t as isVariable } from "./isVariable-fAKEB7gF.mjs";
|
|
2
2
|
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
3
|
import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js";
|
|
4
4
|
//#region src/id/hashSource.ts
|
|
@@ -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-DEaFhGqX.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id-
|
|
1
|
+
{"version":3,"file":"id-DEaFhGqX.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-C2orn1MA.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
package/dist/id.d.mts
CHANGED
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-DEaFhGqX.mjs";
|
|
2
2
|
export { hashSource, hashString, hashTemplate };
|