@intlayer/core 9.0.0-canary.7 → 9.0.0-canary.8
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/dist/cjs/dictionaryManipulator/index.cjs +1 -0
- package/dist/cjs/dictionaryManipulator/mergeQualifiedDictionaries.cjs +1 -5
- package/dist/cjs/dictionaryManipulator/mergeQualifiedDictionaries.cjs.map +1 -1
- package/dist/cjs/dictionaryManipulator/qualifiedDictionary.cjs +48 -74
- package/dist/cjs/dictionaryManipulator/qualifiedDictionary.cjs.map +1 -1
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/interpreter/getDictionary.cjs +5 -5
- package/dist/cjs/interpreter/getDictionary.cjs.map +1 -1
- package/dist/cjs/interpreter/getIntlayer.cjs +1 -1
- package/dist/cjs/interpreter/getIntlayer.cjs.map +1 -1
- package/dist/esm/dictionaryManipulator/index.mjs +2 -2
- package/dist/esm/dictionaryManipulator/mergeQualifiedDictionaries.mjs +1 -5
- package/dist/esm/dictionaryManipulator/mergeQualifiedDictionaries.mjs.map +1 -1
- package/dist/esm/dictionaryManipulator/qualifiedDictionary.mjs +48 -75
- package/dist/esm/dictionaryManipulator/qualifiedDictionary.mjs.map +1 -1
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/interpreter/getDictionary.mjs +5 -5
- package/dist/esm/interpreter/getDictionary.mjs.map +1 -1
- package/dist/esm/interpreter/getIntlayer.mjs +1 -1
- package/dist/esm/interpreter/getIntlayer.mjs.map +1 -1
- package/dist/types/deepTransformPlugins/getFilterMissingTranslationsContent.d.ts +1 -2
- package/dist/types/deepTransformPlugins/getFilterTranslationsOnlyContent.d.ts +1 -2
- package/dist/types/deepTransformPlugins/getFilteredLocalesContent.d.ts +1 -2
- package/dist/types/dictionaryManipulator/index.d.ts +2 -2
- package/dist/types/dictionaryManipulator/mergeQualifiedDictionaries.d.ts +1 -1
- package/dist/types/dictionaryManipulator/mergeQualifiedDictionaries.d.ts.map +1 -1
- package/dist/types/dictionaryManipulator/qualifiedDictionary.d.ts +33 -20
- package/dist/types/dictionaryManipulator/qualifiedDictionary.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/interpreter/getDictionary.d.ts +5 -5
- package/dist/types/interpreter/getIntlayer.d.ts +1 -1
- package/package.json +6 -6
|
@@ -42,4 +42,5 @@ exports.renameContentNodeByKeyPath = require_dictionaryManipulator_renameContent
|
|
|
42
42
|
exports.resolveQualifiedDictionary = require_dictionaryManipulator_qualifiedDictionary.resolveQualifiedDictionary;
|
|
43
43
|
exports.resolveQualifiedDynamicContent = require_dictionaryManipulator_qualifiedDictionary.resolveQualifiedDynamicContent;
|
|
44
44
|
exports.resolveQualifiedDynamicContentAsync = require_dictionaryManipulator_qualifiedDictionary.resolveQualifiedDynamicContentAsync;
|
|
45
|
+
exports.serializeVariant = require_dictionaryManipulator_qualifiedDictionary.serializeVariant;
|
|
45
46
|
exports.updateNodeChildren = require_dictionaryManipulator_updateNodeChildren.updateNodeChildren;
|
|
@@ -13,7 +13,7 @@ let _intlayer_config_logger = require("@intlayer/config/logger");
|
|
|
13
13
|
* `mergeDictionaries` (single merged dictionary).
|
|
14
14
|
* - At least one dictionary declares a qualifier → the group's dimension set is
|
|
15
15
|
* the union of every declared dimension (in canonical order
|
|
16
|
-
* `variant →
|
|
16
|
+
* `variant → item`). Dictionaries are grouped by their composite id
|
|
17
17
|
* (one segment per dimension), merged within each group (locale completion /
|
|
18
18
|
* priority overrides preserved), and a `QualifiedDictionaryGroup` is returned.
|
|
19
19
|
* Unqualified siblings act as shared base content merged into every entry.
|
|
@@ -45,13 +45,10 @@ const mergeQualifiedDictionaries = (dictionaries) => {
|
|
|
45
45
|
entriesDictionaries.set(compositeId, existingEntries);
|
|
46
46
|
});
|
|
47
47
|
const content = {};
|
|
48
|
-
const metaByCompositeId = {};
|
|
49
|
-
const declaresMeta = groupQualifierTypes.includes("meta");
|
|
50
48
|
let importMode;
|
|
51
49
|
for (const [compositeId, qualifiedDictionaries] of entriesDictionaries) {
|
|
52
50
|
content[compositeId] = require_dictionaryManipulator_mergeDictionaries.mergeDictionaries([...qualifiedDictionaries, ...baseDictionaries]).content;
|
|
53
51
|
const [firstQualified] = qualifiedDictionaries;
|
|
54
|
-
if (declaresMeta && firstQualified?.meta !== void 0) metaByCompositeId[compositeId] = firstQualified.meta;
|
|
55
52
|
importMode ??= firstQualified?.importMode;
|
|
56
53
|
}
|
|
57
54
|
const localIds = Array.from(new Set(dictionaries.filter((dictionary) => dictionary.localId).map((dictionary) => dictionary.localId)));
|
|
@@ -59,7 +56,6 @@ const mergeQualifiedDictionaries = (dictionaries) => {
|
|
|
59
56
|
key: dictionaries[0].key,
|
|
60
57
|
qualifierTypes: groupQualifierTypes,
|
|
61
58
|
content,
|
|
62
|
-
...declaresMeta && { meta: metaByCompositeId },
|
|
63
59
|
...importMode !== void 0 && { importMode },
|
|
64
60
|
localIds
|
|
65
61
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeQualifiedDictionaries.cjs","names":["getDictionaryQualifierTypes","QUALIFIER_ORDER","mergeDictionaries","getDictionaryCompositeId"],"sources":["../../../src/dictionaryManipulator/mergeQualifiedDictionaries.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { colorizeKey, getAppLogger } from '@intlayer/config/logger';\nimport type {\n Dictionary,\n
|
|
1
|
+
{"version":3,"file":"mergeQualifiedDictionaries.cjs","names":["getDictionaryQualifierTypes","QUALIFIER_ORDER","mergeDictionaries","getDictionaryCompositeId"],"sources":["../../../src/dictionaryManipulator/mergeQualifiedDictionaries.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { colorizeKey, getAppLogger } from '@intlayer/config/logger';\nimport type {\n Dictionary,\n DictionaryQualifierType,\n QualifiedDictionaryGroup,\n} from '@intlayer/types/dictionary';\nimport { mergeDictionaries } from './mergeDictionaries';\nimport {\n getDictionaryCompositeId,\n getDictionaryQualifierTypes,\n QUALIFIER_ORDER,\n} from './qualifiedDictionary';\n\n/**\n * Merges sibling dictionaries sharing the same key, honouring qualifiers.\n *\n * - No dictionary declares a qualifier → behaves exactly like\n * `mergeDictionaries` (single merged dictionary).\n * - At least one dictionary declares a qualifier → the group's dimension set is\n * the union of every declared dimension (in canonical order\n * `variant → item`). Dictionaries are grouped by their composite id\n * (one segment per dimension), merged within each group (locale completion /\n * priority overrides preserved), and a `QualifiedDictionaryGroup` is returned.\n * Unqualified siblings act as shared base content merged into every entry.\n *\n * Every qualified entry must declare ALL dimensions of the group; an entry that\n * declares only a subset is ambiguous and is rejected with an error log.\n */\nexport const mergeQualifiedDictionaries = (\n dictionaries: Dictionary[]\n): Dictionary | QualifiedDictionaryGroup => {\n const perDictionaryTypes = dictionaries.map(getDictionaryQualifierTypes);\n\n const declaredDimensions = new Set<DictionaryQualifierType>();\n for (const types of perDictionaryTypes) {\n for (const type of types) declaredDimensions.add(type);\n }\n\n // Canonical order, restricted to the dimensions actually declared.\n const groupQualifierTypes = QUALIFIER_ORDER.filter((qualifierType) =>\n declaredDimensions.has(qualifierType)\n );\n\n if (groupQualifierTypes.length === 0) {\n return mergeDictionaries(dictionaries);\n }\n\n const appLogger = getAppLogger({ log });\n\n const baseDictionaries: Dictionary[] = [];\n const entriesDictionaries = new Map<string, Dictionary[]>();\n\n dictionaries.forEach((dictionary, index) => {\n if (perDictionaryTypes[index].length === 0) {\n baseDictionaries.push(dictionary);\n return;\n }\n\n const compositeId = getDictionaryCompositeId(\n dictionary,\n groupQualifierTypes\n );\n\n if (compositeId === undefined) {\n appLogger(\n `Dictionary ${colorizeKey(dictionary.key)} declares (${perDictionaryTypes[index].join(', ')}) but the key's dimensions are (${groupQualifierTypes.join(', ')}); every entry must declare all of them. Entry ignored${dictionary.filePath ? ` - ${dictionary.filePath}` : ''}.`,\n { level: 'error' }\n );\n return;\n }\n\n const existingEntries = entriesDictionaries.get(compositeId) ?? [];\n existingEntries.push(dictionary);\n entriesDictionaries.set(compositeId, existingEntries);\n });\n\n // `content` maps each composite id to its resolved content node directly; the\n // qualifier coordinates live in the key, not in a per-entry wrapper. For an\n // object variant the variant segment is the canonical serialization of the\n // object, so it fully identifies the entry — no side-map is needed.\n const content: Record<string, unknown> = {};\n\n let importMode: Dictionary['importMode'];\n\n for (const [compositeId, qualifiedDictionaries] of entriesDictionaries) {\n // Unqualified siblings act as shared base content: appended last so the\n // qualified entries take precedence (mergeDictionaries prefers first).\n const mergedEntry = mergeDictionaries([\n ...qualifiedDictionaries,\n ...baseDictionaries,\n ]);\n\n content[compositeId] = mergedEntry.content;\n\n const [firstQualified] = qualifiedDictionaries;\n\n importMode ??= firstQualified?.importMode;\n }\n\n const localIds = Array.from(\n new Set(\n dictionaries\n .filter((dictionary) => dictionary.localId)\n .map((dictionary) => dictionary.localId!)\n )\n );\n\n return {\n key: dictionaries[0]!.key,\n qualifierTypes: groupQualifierTypes,\n content,\n ...(importMode !== undefined && { importMode }),\n localIds,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,8BACX,iBAC0C;CAC1C,MAAM,qBAAqB,aAAa,IAAIA,8EAA4B;CAExE,MAAM,qCAAqB,IAAI,KAA8B;AAC7D,MAAK,MAAM,SAAS,mBAClB,MAAK,MAAM,QAAQ,MAAO,oBAAmB,IAAI,KAAK;CAIxD,MAAM,sBAAsBC,kEAAgB,QAAQ,kBAClD,mBAAmB,IAAI,cAAc,CACtC;AAED,KAAI,oBAAoB,WAAW,EACjC,QAAOC,kEAAkB,aAAa;CAGxC,MAAM,sDAAyB,EAAE,iCAAK,CAAC;CAEvC,MAAM,mBAAiC,EAAE;CACzC,MAAM,sCAAsB,IAAI,KAA2B;AAE3D,cAAa,SAAS,YAAY,UAAU;AAC1C,MAAI,mBAAmB,OAAO,WAAW,GAAG;AAC1C,oBAAiB,KAAK,WAAW;AACjC;;EAGF,MAAM,cAAcC,2EAClB,YACA,oBACD;AAED,MAAI,gBAAgB,QAAW;AAC7B,aACE,uDAA0B,WAAW,IAAI,CAAC,aAAa,mBAAmB,OAAO,KAAK,KAAK,CAAC,kCAAkC,oBAAoB,KAAK,KAAK,CAAC,wDAAwD,WAAW,WAAW,MAAM,WAAW,aAAa,GAAG,IAC5Q,EAAE,OAAO,SAAS,CACnB;AACD;;EAGF,MAAM,kBAAkB,oBAAoB,IAAI,YAAY,IAAI,EAAE;AAClE,kBAAgB,KAAK,WAAW;AAChC,sBAAoB,IAAI,aAAa,gBAAgB;GACrD;CAMF,MAAM,UAAmC,EAAE;CAE3C,IAAI;AAEJ,MAAK,MAAM,CAAC,aAAa,0BAA0B,qBAAqB;AAQtE,UAAQ,eALYD,kEAAkB,CACpC,GAAG,uBACH,GAAG,iBACJ,CAEiC,CAAC;EAEnC,MAAM,CAAC,kBAAkB;AAEzB,iBAAe,gBAAgB;;CAGjC,MAAM,WAAW,MAAM,KACrB,IAAI,IACF,aACG,QAAQ,eAAe,WAAW,QAAQ,CAC1C,KAAK,eAAe,WAAW,QAAS,CAC5C,CACF;AAED,QAAO;EACL,KAAK,aAAa,GAAI;EACtB,gBAAgB;EAChB;EACA,GAAI,eAAe,UAAa,EAAE,YAAY;EAC9C;EACD"}
|
|
@@ -2,38 +2,41 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2
2
|
|
|
3
3
|
//#region src/dictionaryManipulator/qualifiedDictionary.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* Canonical order of qualifier dimensions. A key that declares both dimensions
|
|
6
|
+
* always nests them in this order, with `item` innermost so it can act as the
|
|
7
|
+
* collection (array) axis.
|
|
7
8
|
*/
|
|
8
|
-
const
|
|
9
|
-
"locale",
|
|
10
|
-
"item",
|
|
11
|
-
"variant"
|
|
12
|
-
];
|
|
13
|
-
/**
|
|
14
|
-
* Canonical order of qualifier dimensions. A key that declares several
|
|
15
|
-
* dimensions always nests them in this order, with `item` innermost so it can
|
|
16
|
-
* act as the collection (array) axis.
|
|
17
|
-
*/
|
|
18
|
-
const QUALIFIER_ORDER = [
|
|
19
|
-
"variant",
|
|
20
|
-
"meta",
|
|
21
|
-
"item"
|
|
22
|
-
];
|
|
9
|
+
const QUALIFIER_ORDER = ["variant", "item"];
|
|
23
10
|
/**
|
|
24
11
|
* Separator joining per-dimension ids into a composite entry id. Also used as
|
|
25
12
|
* the chunk path separator in dynamic mode.
|
|
26
13
|
*/
|
|
27
14
|
const COMPOSITE_ID_SEPARATOR = "/";
|
|
28
15
|
/**
|
|
16
|
+
* Canonical serialization of a variant value into its identity string — the
|
|
17
|
+
* variant segment of a composite id and the runtime matching key.
|
|
18
|
+
*
|
|
19
|
+
* - `undefined` → `'default'` (the implicit fallback variant)
|
|
20
|
+
* - a string → the string itself (a named variant)
|
|
21
|
+
* - an object → its sorted `key=value` pairs joined by `&`
|
|
22
|
+
* (e.g. `{ userId: '123', id: 'abc' }` → `'id=abc&userId=123'`)
|
|
23
|
+
*
|
|
24
|
+
* Two variants resolve to the same entry iff their serializations are equal, so
|
|
25
|
+
* an object variant in a selector must equal the one declared on the dictionary.
|
|
26
|
+
*/
|
|
27
|
+
const serializeVariant = (variant) => {
|
|
28
|
+
if (variant === void 0) return "default";
|
|
29
|
+
if (typeof variant === "string") return variant;
|
|
30
|
+
return Object.keys(variant).sort().map((field) => `${field}=${variant[field]}`).join("&");
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
29
33
|
* Returns the qualifier dimensions declared on a dictionary, in canonical
|
|
30
|
-
* order (`variant →
|
|
34
|
+
* order (`variant → item`). Empty when the dictionary is unqualified
|
|
31
35
|
* (plain dictionary or shared base content of a qualified group).
|
|
32
36
|
*/
|
|
33
37
|
const getDictionaryQualifierTypes = (dictionary) => {
|
|
34
38
|
const declaredQualifiers = [];
|
|
35
|
-
if (
|
|
36
|
-
if (dictionary.meta !== void 0) declaredQualifiers.push("meta");
|
|
39
|
+
if (dictionary.variant !== void 0) declaredQualifiers.push("variant");
|
|
37
40
|
if (typeof dictionary.item === "number") declaredQualifiers.push("item");
|
|
38
41
|
return declaredQualifiers;
|
|
39
42
|
};
|
|
@@ -41,16 +44,11 @@ const getDictionaryQualifierTypes = (dictionary) => {
|
|
|
41
44
|
* Returns the qualifier identifier of a dictionary for the given qualifier
|
|
42
45
|
* dimension — one segment of the composite entry id.
|
|
43
46
|
*
|
|
44
|
-
* - 'variant' → the variant
|
|
45
|
-
* - 'meta' → the `meta.id` discriminator
|
|
47
|
+
* - 'variant' → the serialized variant (named string or object identity)
|
|
46
48
|
* - 'item' → the item index as string
|
|
47
49
|
*/
|
|
48
50
|
const getDictionaryQualifierId = (dictionary, qualifierType) => {
|
|
49
|
-
if (qualifierType === "variant") return dictionary.variant;
|
|
50
|
-
if (qualifierType === "meta") {
|
|
51
|
-
const metaId = dictionary.meta?.id;
|
|
52
|
-
return metaId === void 0 ? void 0 : String(metaId);
|
|
53
|
-
}
|
|
51
|
+
if (qualifierType === "variant") return dictionary.variant === void 0 ? void 0 : serializeVariant(dictionary.variant);
|
|
54
52
|
return dictionary.item === void 0 ? void 0 : String(dictionary.item);
|
|
55
53
|
};
|
|
56
54
|
/**
|
|
@@ -73,27 +71,13 @@ const getDictionaryQualifierSegments = (dictionary, qualifierTypes) => {
|
|
|
73
71
|
*/
|
|
74
72
|
const getDictionaryCompositeId = (dictionary, qualifierTypes) => getDictionaryQualifierSegments(dictionary, qualifierTypes)?.join("/");
|
|
75
73
|
/**
|
|
76
|
-
* Checks that every declared meta field is provided and equal in the selector.
|
|
77
|
-
* Reserved keys (`locale`, `item`, `variant`) are skipped; `meta.id` is part of
|
|
78
|
-
* the equality check.
|
|
79
|
-
*/
|
|
80
|
-
const metaFieldsMatch = (meta, selector) => {
|
|
81
|
-
if (!meta) return false;
|
|
82
|
-
return Object.entries(meta).every(([metaField, declaredValue]) => {
|
|
83
|
-
if (RESERVED_SELECTOR_KEYS.includes(metaField)) return true;
|
|
84
|
-
const providedValue = selector?.[metaField];
|
|
85
|
-
return providedValue !== void 0 && String(providedValue) === String(declaredValue);
|
|
86
|
-
});
|
|
87
|
-
};
|
|
88
|
-
/**
|
|
89
74
|
* Tests whether a group entry matches a selector across every declared
|
|
90
75
|
* dimension. The `item` dimension matches any value when the selector does not
|
|
91
76
|
* provide one (open collection axis).
|
|
92
77
|
*/
|
|
93
78
|
const entryMatchesSelector = (entry, qualifierTypes, selector) => qualifierTypes.every((qualifierType) => {
|
|
94
|
-
if (qualifierType === "variant") return entry.variant === (selector?.variant
|
|
95
|
-
|
|
96
|
-
return metaFieldsMatch(entry.meta, selector);
|
|
79
|
+
if (qualifierType === "variant") return serializeVariant(entry.variant) === serializeVariant(selector?.variant);
|
|
80
|
+
return selector?.item === void 0 || String(entry.item) === String(selector.item);
|
|
97
81
|
});
|
|
98
82
|
/**
|
|
99
83
|
* Type guard discriminating a `QualifiedDictionaryGroup` (merge output of a
|
|
@@ -104,12 +88,13 @@ const isQualifiedDictionaryGroup = (value) => typeof value === "object" && value
|
|
|
104
88
|
/**
|
|
105
89
|
* Reconstructs a resolvable {@link Dictionary} from a single entry of a
|
|
106
90
|
* qualified group: the content node stored under its composite id, plus the
|
|
107
|
-
* qualifier coordinates decoded from that id (`variant`, `item`)
|
|
108
|
-
* preserved `meta` object for the meta dimension.
|
|
91
|
+
* qualifier coordinates decoded from that id (`variant`, `item`).
|
|
109
92
|
*
|
|
110
93
|
* This keeps the resolver's matching/transform code unchanged: it still sees a
|
|
111
|
-
* `{ key, content, variant?, item
|
|
112
|
-
*
|
|
94
|
+
* `{ key, content, variant?, item? }` shape, even though the stored format no
|
|
95
|
+
* longer duplicates those fields per entry. The `variant` coordinate stays in
|
|
96
|
+
* its serialized form (e.g. `'id=abc&userId=123'`), which round-trips through
|
|
97
|
+
* {@link serializeVariant} during matching.
|
|
113
98
|
*/
|
|
114
99
|
const reconstructQualifiedEntry = (group, compositeId) => {
|
|
115
100
|
const segments = compositeId.split("/");
|
|
@@ -121,10 +106,6 @@ const reconstructQualifiedEntry = (group, compositeId) => {
|
|
|
121
106
|
if (qualifierType === "variant") entry.variant = segments[index];
|
|
122
107
|
else if (qualifierType === "item") entry.item = Number(segments[index]);
|
|
123
108
|
});
|
|
124
|
-
if (group.qualifierTypes.includes("meta")) {
|
|
125
|
-
const metaIndex = group.qualifierTypes.indexOf("meta");
|
|
126
|
-
entry.meta = group.meta?.[compositeId] ?? { id: segments[metaIndex] };
|
|
127
|
-
}
|
|
128
109
|
return entry;
|
|
129
110
|
};
|
|
130
111
|
/**
|
|
@@ -134,8 +115,8 @@ const reconstructQualifiedEntry = (group, compositeId) => {
|
|
|
134
115
|
* - Plain dictionary → returned as-is (selector ignored)
|
|
135
116
|
* - `item` declared but not selected → every matching entry ordered by index
|
|
136
117
|
* - `item` selected → the matching entry or null
|
|
137
|
-
* - `variant` defaults to the `default` entry when not selected
|
|
138
|
-
*
|
|
118
|
+
* - `variant` defaults to the `default` entry when not selected; an object
|
|
119
|
+
* variant resolves only when the selector provides an equal object
|
|
139
120
|
*
|
|
140
121
|
* Dimensions compose: e.g. a variant × item key with `{ variant: 'promo' }`
|
|
141
122
|
* returns every promo item as an array; adding `{ item: 2 }` narrows to one.
|
|
@@ -143,7 +124,6 @@ const reconstructQualifiedEntry = (group, compositeId) => {
|
|
|
143
124
|
const resolveQualifiedDictionary = (dictionaryOrGroup, selector) => {
|
|
144
125
|
if (!isQualifiedDictionaryGroup(dictionaryOrGroup)) return dictionaryOrGroup;
|
|
145
126
|
const { qualifierTypes, content } = dictionaryOrGroup;
|
|
146
|
-
if (qualifierTypes.includes("meta") && selector?.id === void 0) return null;
|
|
147
127
|
const itemAxisOpen = qualifierTypes.includes("item") && selector?.item === void 0;
|
|
148
128
|
const matchedEntries = Object.keys(content).map((compositeId) => reconstructQualifiedEntry(dictionaryOrGroup, compositeId)).filter((entry) => entryMatchesSelector(entry, qualifierTypes, selector));
|
|
149
129
|
if (itemAxisOpen) return matchedEntries.sort((left, right) => (left.item ?? 0) - (right.item ?? 0));
|
|
@@ -166,7 +146,10 @@ const parseDictionarySelector = (localeOrSelector) => {
|
|
|
166
146
|
*/
|
|
167
147
|
const getDictionarySelectorCacheKey = (selector) => {
|
|
168
148
|
if (!selector) return "";
|
|
169
|
-
return Object.keys(selector).filter((selectorKey) => selectorKey !== "locale").sort().map((selectorKey) =>
|
|
149
|
+
return Object.keys(selector).filter((selectorKey) => selectorKey !== "locale").sort().map((selectorKey) => {
|
|
150
|
+
const value = selector[selectorKey];
|
|
151
|
+
return `${selectorKey}:${selectorKey === "variant" ? serializeVariant(value) : String(value)}`;
|
|
152
|
+
}).join("|");
|
|
170
153
|
};
|
|
171
154
|
/**
|
|
172
155
|
* Marker property carrying the ordered qualifier dimensions on a dynamic loader
|
|
@@ -177,7 +160,7 @@ const getDictionarySelectorCacheKey = (selector) => {
|
|
|
177
160
|
const QUALIFIER_DYNAMIC_TYPES_KEY = "__intlayerQualifierTypes";
|
|
178
161
|
/**
|
|
179
162
|
* Type guard discriminating a qualified dynamic loader map (collections /
|
|
180
|
-
* variants
|
|
163
|
+
* variants, possibly combined) from a plain dynamic loader map.
|
|
181
164
|
*/
|
|
182
165
|
const isQualifiedDynamicLoaderMap = (value) => typeof value === "object" && value !== null && "__intlayerQualifierTypes" in value;
|
|
183
166
|
/**
|
|
@@ -194,11 +177,6 @@ const collectQualifiedChunks = (loaderMap, key, locale, selector) => {
|
|
|
194
177
|
missed: true,
|
|
195
178
|
chunks: []
|
|
196
179
|
};
|
|
197
|
-
if (qualifierTypes.includes("meta") && selector?.id === void 0) return {
|
|
198
|
-
itemAxisOpen,
|
|
199
|
-
missed: true,
|
|
200
|
-
chunks: []
|
|
201
|
-
};
|
|
202
180
|
const chunks = [];
|
|
203
181
|
const walk = (node, dimensions, segments) => {
|
|
204
182
|
if (dimensions.length === 0) {
|
|
@@ -214,7 +192,7 @@ const collectQualifiedChunks = (loaderMap, key, locale, selector) => {
|
|
|
214
192
|
for (const segment of Object.keys(tree).sort((left, right) => Number(left) - Number(right))) walk(tree[segment], rest, [...segments, segment]);
|
|
215
193
|
return true;
|
|
216
194
|
}
|
|
217
|
-
const segment = dimension === "variant" ? selector?.variant
|
|
195
|
+
const segment = dimension === "variant" ? serializeVariant(selector?.variant) : String(selector?.item);
|
|
218
196
|
const child = tree[segment];
|
|
219
197
|
if (!child) return false;
|
|
220
198
|
return walk(child, rest, [...segments, segment]);
|
|
@@ -226,20 +204,15 @@ const collectQualifiedChunks = (loaderMap, key, locale, selector) => {
|
|
|
226
204
|
};
|
|
227
205
|
};
|
|
228
206
|
/**
|
|
229
|
-
* Whether a loaded chunk satisfies the selector's meta fields (no-op unless the
|
|
230
|
-
* key declares a `meta` dimension).
|
|
231
|
-
*/
|
|
232
|
-
const chunkMatchesMeta = (loaderMap, dictionary, selector) => !loaderMap["__intlayerQualifierTypes"].includes("meta") || metaFieldsMatch(dictionary.meta, selector);
|
|
233
|
-
/**
|
|
234
207
|
* Resolves the content of a qualified dynamic loader map against a selector,
|
|
235
208
|
* loading only the chunk(s) the selector actually targets.
|
|
236
209
|
*
|
|
237
210
|
* Walks the nested loader tree one dimension at a time (canonical order
|
|
238
|
-
* `variant →
|
|
239
|
-
*
|
|
240
|
-
* given — expands into every sibling chunk (the collection
|
|
241
|
-
*
|
|
242
|
-
*
|
|
211
|
+
* `variant → item`): `variant` defaults to `default` (or descends by the
|
|
212
|
+
* serialized object identity), and `item` either narrows to the selected index
|
|
213
|
+
* or — when no item is given — expands into every sibling chunk (the collection
|
|
214
|
+
* axis). Semantics mirror {@link resolveQualifiedDictionary} so dynamic and
|
|
215
|
+
* static modes behave alike.
|
|
243
216
|
*
|
|
244
217
|
* The Suspense mechanism is injected through `loadChunk` so the same logic
|
|
245
218
|
* serves both the client (suspender cache) and the server (`react.use`). Every
|
|
@@ -257,7 +230,7 @@ const resolveQualifiedDynamicContent = (params) => {
|
|
|
257
230
|
const { loaderMap, key, locale, selector, loadChunk, transform } = params;
|
|
258
231
|
const { itemAxisOpen, missed, chunks } = collectQualifiedChunks(loaderMap, key, locale, selector);
|
|
259
232
|
if (missed) return itemAxisOpen ? [] : null;
|
|
260
|
-
const dictionaries = chunks.map(({ cacheKey, loader }) => loadChunk(cacheKey, loader()))
|
|
233
|
+
const dictionaries = chunks.map(({ cacheKey, loader }) => loadChunk(cacheKey, loader()));
|
|
261
234
|
if (itemAxisOpen) return dictionaries.map(transform);
|
|
262
235
|
const [dictionary] = dictionaries;
|
|
263
236
|
return dictionary ? transform(dictionary) : null;
|
|
@@ -277,7 +250,7 @@ const resolveQualifiedDynamicContentAsync = async (params) => {
|
|
|
277
250
|
const { loaderMap, key, locale, selector, transform } = params;
|
|
278
251
|
const { itemAxisOpen, missed, chunks } = collectQualifiedChunks(loaderMap, key, locale, selector);
|
|
279
252
|
if (missed) return itemAxisOpen ? [] : null;
|
|
280
|
-
const dictionaries =
|
|
253
|
+
const dictionaries = await Promise.all(chunks.map(({ loader }) => loader()));
|
|
281
254
|
if (itemAxisOpen) return dictionaries.map(transform);
|
|
282
255
|
const [dictionary] = dictionaries;
|
|
283
256
|
return dictionary ? transform(dictionary) : null;
|
|
@@ -299,4 +272,5 @@ exports.reconstructQualifiedEntry = reconstructQualifiedEntry;
|
|
|
299
272
|
exports.resolveQualifiedDictionary = resolveQualifiedDictionary;
|
|
300
273
|
exports.resolveQualifiedDynamicContent = resolveQualifiedDynamicContent;
|
|
301
274
|
exports.resolveQualifiedDynamicContentAsync = resolveQualifiedDynamicContentAsync;
|
|
275
|
+
exports.serializeVariant = serializeVariant;
|
|
302
276
|
//# sourceMappingURL=qualifiedDictionary.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"qualifiedDictionary.cjs","names":[],"sources":["../../../src/dictionaryManipulator/qualifiedDictionary.ts"],"sourcesContent":["import type {\n Dictionary,\n DictionaryQualifierType,\n DictionarySelector,\n QualifiedDictionaryGroup,\n} from '@intlayer/types/dictionary';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\n/**\n * Selector keys that are reserved for dictionary resolution and therefore\n * excluded from meta field matching.\n */\nconst RESERVED_SELECTOR_KEYS = ['locale', 'item', 'variant'] as const;\n\n/**\n * Canonical order of qualifier dimensions. A key that declares several\n * dimensions always nests them in this order, with `item` innermost so it can\n * act as the collection (array) axis.\n */\nexport const QUALIFIER_ORDER = [\n 'variant',\n 'meta',\n 'item',\n] as const satisfies readonly DictionaryQualifierType[];\n\n/**\n * Separator joining per-dimension ids into a composite entry id. Also used as\n * the chunk path separator in dynamic mode.\n */\nexport const COMPOSITE_ID_SEPARATOR = '/';\n\n/**\n * Returns the qualifier dimensions declared on a dictionary, in canonical\n * order (`variant → meta → item`). Empty when the dictionary is unqualified\n * (plain dictionary or shared base content of a qualified group).\n */\nexport const getDictionaryQualifierTypes = (\n dictionary: Dictionary\n): DictionaryQualifierType[] => {\n const declaredQualifiers: DictionaryQualifierType[] = [];\n\n if (typeof dictionary.variant === 'string')\n declaredQualifiers.push('variant');\n if (dictionary.meta !== undefined) declaredQualifiers.push('meta');\n if (typeof dictionary.item === 'number') declaredQualifiers.push('item');\n\n return declaredQualifiers;\n};\n\n/**\n * Returns the qualifier identifier of a dictionary for the given qualifier\n * dimension — one segment of the composite entry id.\n *\n * - 'variant' → the variant name\n * - 'meta' → the `meta.id` discriminator\n * - 'item' → the item index as string\n */\nexport const getDictionaryQualifierId = (\n dictionary: Dictionary,\n qualifierType: DictionaryQualifierType\n): string | undefined => {\n if (qualifierType === 'variant') return dictionary.variant;\n if (qualifierType === 'meta') {\n const metaId = dictionary.meta?.id;\n return metaId === undefined ? undefined : String(metaId);\n }\n return dictionary.item === undefined ? undefined : String(dictionary.item);\n};\n\n/**\n * Returns the per-dimension id segments of a dictionary for the given ordered\n * dimension set, or `undefined` when the dictionary does not declare every\n * dimension of the set.\n */\nexport const getDictionaryQualifierSegments = (\n dictionary: Dictionary,\n qualifierTypes: DictionaryQualifierType[]\n): string[] | undefined => {\n const segments: string[] = [];\n\n for (const qualifierType of qualifierTypes) {\n const id = getDictionaryQualifierId(dictionary, qualifierType);\n if (id === undefined) return undefined;\n segments.push(id);\n }\n\n return segments;\n};\n\n/**\n * Builds the composite entry id of a dictionary — its per-dimension id segments\n * joined in canonical order. `undefined` when a dimension is missing.\n */\nexport const getDictionaryCompositeId = (\n dictionary: Dictionary,\n qualifierTypes: DictionaryQualifierType[]\n): string | undefined =>\n getDictionaryQualifierSegments(dictionary, qualifierTypes)?.join(\n COMPOSITE_ID_SEPARATOR\n );\n\n/**\n * Checks that every declared meta field is provided and equal in the selector.\n * Reserved keys (`locale`, `item`, `variant`) are skipped; `meta.id` is part of\n * the equality check.\n */\nconst metaFieldsMatch = (\n meta: Dictionary['meta'] | undefined,\n selector: DictionarySelector | undefined\n): boolean => {\n if (!meta) return false;\n\n return Object.entries(meta).every(([metaField, declaredValue]) => {\n if ((RESERVED_SELECTOR_KEYS as readonly string[]).includes(metaField)) {\n return true;\n }\n\n const providedValue = selector?.[metaField];\n\n return (\n providedValue !== undefined &&\n String(providedValue) === String(declaredValue)\n );\n });\n};\n\n/**\n * Tests whether a group entry matches a selector across every declared\n * dimension. The `item` dimension matches any value when the selector does not\n * provide one (open collection axis).\n */\nconst entryMatchesSelector = (\n entry: Dictionary,\n qualifierTypes: DictionaryQualifierType[],\n selector: DictionarySelector | undefined\n): boolean =>\n qualifierTypes.every((qualifierType) => {\n if (qualifierType === 'variant') {\n return entry.variant === (selector?.variant ?? 'default');\n }\n\n if (qualifierType === 'item') {\n return (\n selector?.item === undefined ||\n String(entry.item) === String(selector.item)\n );\n }\n\n // qualifierType === 'meta'\n return metaFieldsMatch(entry.meta, selector);\n });\n\n/**\n * Type guard discriminating a `QualifiedDictionaryGroup` (merge output of a\n * qualified key) from a plain `Dictionary`. Both carry a `content` field; only\n * the group declares `qualifierTypes`, which is therefore the discriminator.\n */\nexport const isQualifiedDictionaryGroup = (\n value: unknown\n): value is QualifiedDictionaryGroup =>\n typeof value === 'object' &&\n value !== null &&\n 'qualifierTypes' in value &&\n Array.isArray((value as { qualifierTypes: unknown }).qualifierTypes) &&\n 'content' in value;\n\n/**\n * Reconstructs a resolvable {@link Dictionary} from a single entry of a\n * qualified group: the content node stored under its composite id, plus the\n * qualifier coordinates decoded from that id (`variant`, `item`) and the\n * preserved `meta` object for the meta dimension.\n *\n * This keeps the resolver's matching/transform code unchanged: it still sees a\n * `{ key, content, variant?, item?, meta? }` shape, even though the stored\n * format no longer duplicates those fields per entry.\n */\nexport const reconstructQualifiedEntry = (\n group: QualifiedDictionaryGroup,\n compositeId: string\n): Dictionary => {\n const segments = compositeId.split(COMPOSITE_ID_SEPARATOR);\n\n const entry = {\n key: group.key,\n content: group.content[compositeId],\n } as Dictionary;\n\n group.qualifierTypes.forEach((qualifierType, index) => {\n if (qualifierType === 'variant') {\n entry.variant = segments[index];\n } else if (qualifierType === 'item') {\n entry.item = Number(segments[index]);\n }\n });\n\n if (group.qualifierTypes.includes('meta')) {\n const metaIndex = group.qualifierTypes.indexOf('meta');\n entry.meta = group.meta?.[compositeId] ?? { id: segments[metaIndex] };\n }\n\n return entry;\n};\n\n/**\n * Resolves a dictionary (or qualified dictionary group) against a selector,\n * across every declared dimension.\n *\n * - Plain dictionary → returned as-is (selector ignored)\n * - `item` declared but not selected → every matching entry ordered by index\n * - `item` selected → the matching entry or null\n * - `variant` defaults to the `default` entry when not selected\n * - `meta` requires `{ id }` and every declared meta field to match\n *\n * Dimensions compose: e.g. a variant × item key with `{ variant: 'promo' }`\n * returns every promo item as an array; adding `{ item: 2 }` narrows to one.\n */\nexport const resolveQualifiedDictionary = (\n dictionaryOrGroup: Dictionary | QualifiedDictionaryGroup,\n selector?: DictionarySelector\n): Dictionary | Dictionary[] | null => {\n if (!isQualifiedDictionaryGroup(dictionaryOrGroup)) {\n return dictionaryOrGroup;\n }\n\n const { qualifierTypes, content } = dictionaryOrGroup;\n\n // The meta dimension cannot resolve without an id discriminator.\n if (qualifierTypes.includes('meta') && selector?.id === undefined) {\n return null;\n }\n\n const itemAxisOpen =\n qualifierTypes.includes('item') && selector?.item === undefined;\n\n const matchedEntries = Object.keys(content)\n .map((compositeId) =>\n reconstructQualifiedEntry(dictionaryOrGroup, compositeId)\n )\n .filter((entry) => entryMatchesSelector(entry, qualifierTypes, selector));\n\n if (itemAxisOpen) {\n return matchedEntries.sort(\n (left, right) => (left.item ?? 0) - (right.item ?? 0)\n );\n }\n\n return matchedEntries[0] ?? null;\n};\n\n/**\n * Splits the second argument of `getIntlayer` / `getDictionary` into the\n * effective locale and the selector object (if any).\n */\nexport const parseDictionarySelector = <L extends LocalesValues>(\n localeOrSelector?: L | DictionarySelector\n): { locale?: L; selector?: DictionarySelector } => {\n if (typeof localeOrSelector === 'object' && localeOrSelector !== null) {\n return {\n locale: localeOrSelector.locale as L | undefined,\n selector: localeOrSelector,\n };\n }\n\n return { locale: localeOrSelector };\n};\n\n/**\n * Builds a stable string identity of a selector (excluding `locale`), suitable\n * for cache keys and memoization dependencies.\n */\nexport const getDictionarySelectorCacheKey = (\n selector?: DictionarySelector\n): string => {\n if (!selector) return '';\n\n return Object.keys(selector)\n .filter((selectorKey) => selectorKey !== 'locale')\n .sort()\n .map((selectorKey) => `${selectorKey}:${String(selector[selectorKey])}`)\n .join('|');\n};\n\n/**\n * Marker property carrying the ordered qualifier dimensions on a dynamic loader\n * map. Its presence distinguishes a qualified group loader map (a nested tree\n * of chunks) from a plain dynamic loader map (one chunk per `locale`). Prefixed\n * and unlikely to collide with a real locale code.\n */\nexport const QUALIFIER_DYNAMIC_TYPES_KEY = '__intlayerQualifierTypes';\n\n/**\n * A lazily-imported per-locale dictionary chunk loader.\n */\nexport type DynamicDictionaryLoader = () => Promise<Dictionary>;\n\n/**\n * Nested tree of chunk loaders: one nesting level per declared dimension (in\n * canonical order), leaves are loaders.\n */\nexport type QualifiedDynamicLoaderTree = {\n [segment: string]: QualifiedDynamicLoaderTree | DynamicDictionaryLoader;\n};\n\n/**\n * Default export shape of a generated dynamic entry point for a qualified key.\n * One nesting level per dimension under each locale, plus the dimension marker.\n *\n * ```ts\n * {\n * __intlayerQualifierTypes: ['variant', 'item'],\n * en: { promo: { '1': () => import('./json/x/promo/1/en.json'), … }, … },\n * fr: { … },\n * }\n * ```\n */\nexport type QualifiedDynamicLoaderMap = {\n [QUALIFIER_DYNAMIC_TYPES_KEY]: DictionaryQualifierType[];\n [locale: string]: QualifiedDynamicLoaderTree | DictionaryQualifierType[];\n};\n\n/**\n * Type guard discriminating a qualified dynamic loader map (collections /\n * variants / meta records, possibly combined) from a plain dynamic loader map.\n */\nexport const isQualifiedDynamicLoaderMap = (\n value: unknown\n): value is QualifiedDynamicLoaderMap =>\n typeof value === 'object' &&\n value !== null &&\n QUALIFIER_DYNAMIC_TYPES_KEY in value;\n\n/**\n * Resolves the content of a qualified dynamic loader map against a selector,\n * loading only the chunk(s) the selector actually targets.\n *\n * Walks the nested loader tree one dimension at a time (canonical order\n * `variant → meta → item`): `variant` defaults to `default`, `meta` descends by\n * `id`, and `item` either narrows to the selected index or — when no item is\n * given — expands into every sibling chunk (the collection axis). Meta-equality\n * is verified on the loaded chunk. Semantics mirror\n * {@link resolveQualifiedDictionary} so dynamic and static modes behave alike.\n *\n * The Suspense mechanism is injected through `loadChunk` so the same logic\n * serves both the client (suspender cache) and the server (`react.use`). Every\n * targeted loader is started before the first chunk is read, so sibling chunks\n * load in parallel rather than waterfalling.\n *\n * @param loaderMap - The qualified dynamic loader map (entry point default export).\n * @param key - The dictionary key (used to build stable chunk cache keys).\n * @param locale - The resolved locale to load chunks for.\n * @param selector - The selector splitting the qualifier dimensions.\n * @param loadChunk - Reads a started chunk promise, suspending until it resolves.\n * @param transform - Turns a resolved chunk dictionary into final content.\n */\n/** One targeted chunk: its stable cache key and lazy loader. */\ntype CollectedChunk = {\n cacheKey: string;\n loader: DynamicDictionaryLoader;\n};\n\ntype CollectedChunks = {\n /** True when the `item` axis is open (collection result → array). */\n itemAxisOpen: boolean;\n /** True when a required coordinate is absent (result → [] or null). */\n missed: boolean;\n /** The chunks the selector targets (in collection order for the item axis). */\n chunks: CollectedChunk[];\n};\n\n/**\n * Walks the loader tree following the selector and collects the chunk loaders\n * it targets — shared by the sync ({@link resolveQualifiedDynamicContent}) and\n * async ({@link resolveQualifiedDynamicContentAsync}) resolvers.\n */\nconst collectQualifiedChunks = (\n loaderMap: QualifiedDynamicLoaderMap,\n key: string,\n locale: string,\n selector: DictionarySelector | undefined\n): CollectedChunks => {\n const qualifierTypes = loaderMap[QUALIFIER_DYNAMIC_TYPES_KEY];\n const localeTree = loaderMap[locale] as\n | QualifiedDynamicLoaderTree\n | undefined;\n\n const itemAxisOpen =\n qualifierTypes.includes('item') && selector?.item === undefined;\n\n if (!localeTree) return { itemAxisOpen, missed: true, chunks: [] };\n\n // The meta dimension cannot resolve without an id discriminator.\n if (qualifierTypes.includes('meta') && selector?.id === undefined) {\n return { itemAxisOpen, missed: true, chunks: [] };\n }\n\n const chunks: CollectedChunk[] = [];\n\n const walk = (\n node: QualifiedDynamicLoaderTree | DynamicDictionaryLoader,\n dimensions: DictionaryQualifierType[],\n segments: string[]\n ): boolean => {\n if (dimensions.length === 0) {\n chunks.push({\n cacheKey: `${key}.${locale}.${segments.join(COMPOSITE_ID_SEPARATOR)}`,\n loader: node as DynamicDictionaryLoader,\n });\n return true;\n }\n\n const [dimension, ...rest] = dimensions;\n const tree = node as QualifiedDynamicLoaderTree;\n\n if (dimension === 'item' && selector?.item === undefined) {\n // Open collection axis: fan out into every sibling chunk, ordered.\n for (const segment of Object.keys(tree).sort(\n (left, right) => Number(left) - Number(right)\n )) {\n walk(tree[segment]!, rest, [...segments, segment]);\n }\n return true;\n }\n\n const segment =\n dimension === 'variant'\n ? (selector?.variant ?? 'default')\n : dimension === 'meta'\n ? String(selector?.id)\n : String(selector?.item);\n\n const child = tree[segment];\n if (!child) return false;\n\n return walk(child, rest, [...segments, segment]);\n };\n\n const found = walk(localeTree, qualifierTypes, []);\n\n return { itemAxisOpen, missed: !found, chunks };\n};\n\n/**\n * Whether a loaded chunk satisfies the selector's meta fields (no-op unless the\n * key declares a `meta` dimension).\n */\nconst chunkMatchesMeta = (\n loaderMap: QualifiedDynamicLoaderMap,\n dictionary: Dictionary,\n selector: DictionarySelector | undefined\n): boolean =>\n !loaderMap[QUALIFIER_DYNAMIC_TYPES_KEY].includes('meta') ||\n metaFieldsMatch(dictionary.meta, selector);\n\n/**\n * Resolves the content of a qualified dynamic loader map against a selector,\n * loading only the chunk(s) the selector actually targets.\n *\n * Walks the nested loader tree one dimension at a time (canonical order\n * `variant → meta → item`): `variant` defaults to `default`, `meta` descends by\n * `id`, and `item` either narrows to the selected index or — when no item is\n * given — expands into every sibling chunk (the collection axis). Meta-equality\n * is verified on the loaded chunk. Semantics mirror\n * {@link resolveQualifiedDictionary} so dynamic and static modes behave alike.\n *\n * The Suspense mechanism is injected through `loadChunk` so the same logic\n * serves both the client (suspender cache) and the server (`react.use`). Every\n * targeted loader is started before the first chunk is read, so sibling chunks\n * load in parallel rather than waterfalling.\n *\n * @param loaderMap - The qualified dynamic loader map (entry point default export).\n * @param key - The dictionary key (used to build stable chunk cache keys).\n * @param locale - The resolved locale to load chunks for.\n * @param selector - The selector splitting the qualifier dimensions.\n * @param loadChunk - Reads a started chunk promise, suspending until it resolves.\n * @param transform - Turns a resolved chunk dictionary into final content.\n */\nexport const resolveQualifiedDynamicContent = <Content>(params: {\n loaderMap: QualifiedDynamicLoaderMap;\n key: string;\n locale: string;\n selector: DictionarySelector | undefined;\n loadChunk: (cacheKey: string, promise: Promise<Dictionary>) => Dictionary;\n transform: (dictionary: Dictionary) => Content;\n}): Content | Content[] | null => {\n const { loaderMap, key, locale, selector, loadChunk, transform } = params;\n\n const { itemAxisOpen, missed, chunks } = collectQualifiedChunks(\n loaderMap,\n key,\n locale,\n selector\n );\n\n if (missed) return itemAxisOpen ? [] : null;\n\n // Start every loader before reading, so siblings load in parallel.\n const dictionaries = chunks\n .map(({ cacheKey, loader }) => loadChunk(cacheKey, loader()))\n .filter((dictionary) => chunkMatchesMeta(loaderMap, dictionary, selector));\n\n if (itemAxisOpen) return dictionaries.map(transform);\n\n const [dictionary] = dictionaries;\n return dictionary ? transform(dictionary) : null;\n};\n\n/**\n * Async counterpart of {@link resolveQualifiedDynamicContent} for frameworks\n * that load dictionaries with `await` instead of Suspense (Vue, Svelte, Lit,\n * vanilla). Awaits every targeted chunk in parallel, then resolves identically.\n *\n * @param loaderMap - The qualified dynamic loader map.\n * @param key - The dictionary key (used to build stable chunk cache keys).\n * @param locale - The resolved locale to load chunks for.\n * @param selector - The selector splitting the qualifier dimensions.\n * @param transform - Turns a resolved chunk dictionary into final content.\n */\nexport const resolveQualifiedDynamicContentAsync = async <Content>(params: {\n loaderMap: QualifiedDynamicLoaderMap;\n key: string;\n locale: string;\n selector: DictionarySelector | undefined;\n transform: (dictionary: Dictionary) => Content;\n}): Promise<Content | Content[] | null> => {\n const { loaderMap, key, locale, selector, transform } = params;\n\n const { itemAxisOpen, missed, chunks } = collectQualifiedChunks(\n loaderMap,\n key,\n locale,\n selector\n );\n\n if (missed) return itemAxisOpen ? [] : null;\n\n const dictionaries = (\n await Promise.all(chunks.map(({ loader }) => loader()))\n ).filter((dictionary) => chunkMatchesMeta(loaderMap, dictionary, selector));\n\n if (itemAxisOpen) return dictionaries.map(transform);\n\n const [dictionary] = dictionaries;\n return dictionary ? transform(dictionary) : null;\n};\n"],"mappings":";;;;;;;AAYA,MAAM,yBAAyB;CAAC;CAAU;CAAQ;CAAU;;;;;;AAO5D,MAAa,kBAAkB;CAC7B;CACA;CACA;CACD;;;;;AAMD,MAAa,yBAAyB;;;;;;AAOtC,MAAa,+BACX,eAC8B;CAC9B,MAAM,qBAAgD,EAAE;AAExD,KAAI,OAAO,WAAW,YAAY,SAChC,oBAAmB,KAAK,UAAU;AACpC,KAAI,WAAW,SAAS,OAAW,oBAAmB,KAAK,OAAO;AAClE,KAAI,OAAO,WAAW,SAAS,SAAU,oBAAmB,KAAK,OAAO;AAExE,QAAO;;;;;;;;;;AAWT,MAAa,4BACX,YACA,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C,SAAS,OAAO,IACxD,gBAAgB,WAAW,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;AAyB5C,MAAa,kCAA2C,WAOtB;CAChC,MAAM,EAAE,WAAW,KAAK,QAAQ,UAAU,WAAW,cAAc;CAEnE,MAAM,EAAE,cAAc,QAAQ,WAAW,uBACvC,WACA,KACA,QACA,SACD;AAED,KAAI,OAAQ,QAAO,eAAe,EAAE,GAAG;CAGvC,MAAM,eAAe,OAClB,KAAK,EAAE,UAAU,aAAa,UAAU,UAAU,QAAQ,CAAC,CAAC,CAC5D,QAAQ,eAAe,iBAAiB,WAAW,YAAY,SAAS,CAAC;AAE5E,KAAI,aAAc,QAAO,aAAa,IAAI,UAAU;CAEpD,MAAM,CAAC,cAAc;AACrB,QAAO,aAAa,UAAU,WAAW,GAAG;;;;;;;;;;;;;AAc9C,MAAa,sCAAsC,OAAgB,WAMxB;CACzC,MAAM,EAAE,WAAW,KAAK,QAAQ,UAAU,cAAc;CAExD,MAAM,EAAE,cAAc,QAAQ,WAAW,uBACvC,WACA,KACA,QACA,SACD;AAED,KAAI,OAAQ,QAAO,eAAe,EAAE,GAAG;CAEvC,MAAM,gBACJ,MAAM,QAAQ,IAAI,OAAO,KAAK,EAAE,aAAa,QAAQ,CAAC,CAAC,EACvD,QAAQ,eAAe,iBAAiB,WAAW,YAAY,SAAS,CAAC;AAE3E,KAAI,aAAc,QAAO,aAAa,IAAI,UAAU;CAEpD,MAAM,CAAC,cAAc;AACrB,QAAO,aAAa,UAAU,WAAW,GAAG"}
|
|
1
|
+
{"version":3,"file":"qualifiedDictionary.cjs","names":[],"sources":["../../../src/dictionaryManipulator/qualifiedDictionary.ts"],"sourcesContent":["import type {\n Dictionary,\n DictionaryQualifierType,\n DictionarySelector,\n QualifiedDictionaryGroup,\n} from '@intlayer/types/dictionary';\nimport type { LocalesValues } from '@intlayer/types/module_augmentation';\n\n/**\n * Canonical order of qualifier dimensions. A key that declares both dimensions\n * always nests them in this order, with `item` innermost so it can act as the\n * collection (array) axis.\n */\nexport const QUALIFIER_ORDER = [\n 'variant',\n 'item',\n] as const satisfies readonly DictionaryQualifierType[];\n\n/**\n * Separator joining per-dimension ids into a composite entry id. Also used as\n * the chunk path separator in dynamic mode.\n */\nexport const COMPOSITE_ID_SEPARATOR = '/';\n\n/**\n * Canonical serialization of a variant value into its identity string — the\n * variant segment of a composite id and the runtime matching key.\n *\n * - `undefined` → `'default'` (the implicit fallback variant)\n * - a string → the string itself (a named variant)\n * - an object → its sorted `key=value` pairs joined by `&`\n * (e.g. `{ userId: '123', id: 'abc' }` → `'id=abc&userId=123'`)\n *\n * Two variants resolve to the same entry iff their serializations are equal, so\n * an object variant in a selector must equal the one declared on the dictionary.\n */\nexport const serializeVariant = (\n variant: string | Record<string, string | number> | undefined\n): string => {\n if (variant === undefined) return 'default';\n if (typeof variant === 'string') return variant;\n\n return Object.keys(variant)\n .sort()\n .map((field) => `${field}=${variant[field]}`)\n .join('&');\n};\n\n/**\n * Returns the qualifier dimensions declared on a dictionary, in canonical\n * order (`variant → item`). Empty when the dictionary is unqualified\n * (plain dictionary or shared base content of a qualified group).\n */\nexport const getDictionaryQualifierTypes = (\n dictionary: Dictionary\n): DictionaryQualifierType[] => {\n const declaredQualifiers: DictionaryQualifierType[] = [];\n\n if (dictionary.variant !== undefined) declaredQualifiers.push('variant');\n if (typeof dictionary.item === 'number') declaredQualifiers.push('item');\n\n return declaredQualifiers;\n};\n\n/**\n * Returns the qualifier identifier of a dictionary for the given qualifier\n * dimension — one segment of the composite entry id.\n *\n * - 'variant' → the serialized variant (named string or object identity)\n * - 'item' → the item index as string\n */\nexport const getDictionaryQualifierId = (\n dictionary: Dictionary,\n qualifierType: DictionaryQualifierType\n): string | undefined => {\n if (qualifierType === 'variant') {\n return dictionary.variant === undefined\n ? undefined\n : serializeVariant(dictionary.variant);\n }\n return dictionary.item === undefined ? undefined : String(dictionary.item);\n};\n\n/**\n * Returns the per-dimension id segments of a dictionary for the given ordered\n * dimension set, or `undefined` when the dictionary does not declare every\n * dimension of the set.\n */\nexport const getDictionaryQualifierSegments = (\n dictionary: Dictionary,\n qualifierTypes: DictionaryQualifierType[]\n): string[] | undefined => {\n const segments: string[] = [];\n\n for (const qualifierType of qualifierTypes) {\n const id = getDictionaryQualifierId(dictionary, qualifierType);\n if (id === undefined) return undefined;\n segments.push(id);\n }\n\n return segments;\n};\n\n/**\n * Builds the composite entry id of a dictionary — its per-dimension id segments\n * joined in canonical order. `undefined` when a dimension is missing.\n */\nexport const getDictionaryCompositeId = (\n dictionary: Dictionary,\n qualifierTypes: DictionaryQualifierType[]\n): string | undefined =>\n getDictionaryQualifierSegments(dictionary, qualifierTypes)?.join(\n COMPOSITE_ID_SEPARATOR\n );\n\n/**\n * Tests whether a group entry matches a selector across every declared\n * dimension. The `item` dimension matches any value when the selector does not\n * provide one (open collection axis).\n */\nconst entryMatchesSelector = (\n entry: Dictionary,\n qualifierTypes: DictionaryQualifierType[],\n selector: DictionarySelector | undefined\n): boolean =>\n qualifierTypes.every((qualifierType) => {\n if (qualifierType === 'variant') {\n return (\n serializeVariant(entry.variant) === serializeVariant(selector?.variant)\n );\n }\n\n // qualifierType === 'item'\n return (\n selector?.item === undefined ||\n String(entry.item) === String(selector.item)\n );\n });\n\n/**\n * Type guard discriminating a `QualifiedDictionaryGroup` (merge output of a\n * qualified key) from a plain `Dictionary`. Both carry a `content` field; only\n * the group declares `qualifierTypes`, which is therefore the discriminator.\n */\nexport const isQualifiedDictionaryGroup = (\n value: unknown\n): value is QualifiedDictionaryGroup =>\n typeof value === 'object' &&\n value !== null &&\n 'qualifierTypes' in value &&\n Array.isArray((value as { qualifierTypes: unknown }).qualifierTypes) &&\n 'content' in value;\n\n/**\n * Reconstructs a resolvable {@link Dictionary} from a single entry of a\n * qualified group: the content node stored under its composite id, plus the\n * qualifier coordinates decoded from that id (`variant`, `item`).\n *\n * This keeps the resolver's matching/transform code unchanged: it still sees a\n * `{ key, content, variant?, item? }` shape, even though the stored format no\n * longer duplicates those fields per entry. The `variant` coordinate stays in\n * its serialized form (e.g. `'id=abc&userId=123'`), which round-trips through\n * {@link serializeVariant} during matching.\n */\nexport const reconstructQualifiedEntry = (\n group: QualifiedDictionaryGroup,\n compositeId: string\n): Dictionary => {\n const segments = compositeId.split(COMPOSITE_ID_SEPARATOR);\n\n const entry = {\n key: group.key,\n content: group.content[compositeId],\n } as Dictionary;\n\n group.qualifierTypes.forEach((qualifierType, index) => {\n if (qualifierType === 'variant') {\n entry.variant = segments[index];\n } else if (qualifierType === 'item') {\n entry.item = Number(segments[index]);\n }\n });\n\n return entry;\n};\n\n/**\n * Resolves a dictionary (or qualified dictionary group) against a selector,\n * across every declared dimension.\n *\n * - Plain dictionary → returned as-is (selector ignored)\n * - `item` declared but not selected → every matching entry ordered by index\n * - `item` selected → the matching entry or null\n * - `variant` defaults to the `default` entry when not selected; an object\n * variant resolves only when the selector provides an equal object\n *\n * Dimensions compose: e.g. a variant × item key with `{ variant: 'promo' }`\n * returns every promo item as an array; adding `{ item: 2 }` narrows to one.\n */\nexport const resolveQualifiedDictionary = (\n dictionaryOrGroup: Dictionary | QualifiedDictionaryGroup,\n selector?: DictionarySelector\n): Dictionary | Dictionary[] | null => {\n if (!isQualifiedDictionaryGroup(dictionaryOrGroup)) {\n return dictionaryOrGroup;\n }\n\n const { qualifierTypes, content } = dictionaryOrGroup;\n\n const itemAxisOpen =\n qualifierTypes.includes('item') && selector?.item === undefined;\n\n const matchedEntries = Object.keys(content)\n .map((compositeId) =>\n reconstructQualifiedEntry(dictionaryOrGroup, compositeId)\n )\n .filter((entry) => entryMatchesSelector(entry, qualifierTypes, selector));\n\n if (itemAxisOpen) {\n return matchedEntries.sort(\n (left, right) => (left.item ?? 0) - (right.item ?? 0)\n );\n }\n\n return matchedEntries[0] ?? null;\n};\n\n/**\n * Splits the second argument of `getIntlayer` / `getDictionary` into the\n * effective locale and the selector object (if any).\n */\nexport const parseDictionarySelector = <L extends LocalesValues>(\n localeOrSelector?: L | DictionarySelector\n): { locale?: L; selector?: DictionarySelector } => {\n if (typeof localeOrSelector === 'object' && localeOrSelector !== null) {\n return {\n locale: localeOrSelector.locale as L | undefined,\n selector: localeOrSelector,\n };\n }\n\n return { locale: localeOrSelector };\n};\n\n/**\n * Builds a stable string identity of a selector (excluding `locale`), suitable\n * for cache keys and memoization dependencies.\n */\nexport const getDictionarySelectorCacheKey = (\n selector?: DictionarySelector\n): string => {\n if (!selector) return '';\n\n return Object.keys(selector)\n .filter((selectorKey) => selectorKey !== 'locale')\n .sort()\n .map((selectorKey) => {\n const value = selector[selectorKey as keyof DictionarySelector];\n const serialized =\n selectorKey === 'variant'\n ? serializeVariant(value as Parameters<typeof serializeVariant>[0])\n : String(value);\n return `${selectorKey}:${serialized}`;\n })\n .join('|');\n};\n\n/**\n * Marker property carrying the ordered qualifier dimensions on a dynamic loader\n * map. Its presence distinguishes a qualified group loader map (a nested tree\n * of chunks) from a plain dynamic loader map (one chunk per `locale`). Prefixed\n * and unlikely to collide with a real locale code.\n */\nexport const QUALIFIER_DYNAMIC_TYPES_KEY = '__intlayerQualifierTypes';\n\n/**\n * A lazily-imported per-locale dictionary chunk loader.\n */\nexport type DynamicDictionaryLoader = () => Promise<Dictionary>;\n\n/**\n * Nested tree of chunk loaders: one nesting level per declared dimension (in\n * canonical order), leaves are loaders.\n */\nexport type QualifiedDynamicLoaderTree = {\n [segment: string]: QualifiedDynamicLoaderTree | DynamicDictionaryLoader;\n};\n\n/**\n * Default export shape of a generated dynamic entry point for a qualified key.\n * One nesting level per dimension under each locale, plus the dimension marker.\n *\n * ```ts\n * {\n * __intlayerQualifierTypes: ['variant', 'item'],\n * en: { promo: { '1': () => import('./json/x/promo/1/en.json'), … }, … },\n * fr: { … },\n * }\n * ```\n */\nexport type QualifiedDynamicLoaderMap = {\n [QUALIFIER_DYNAMIC_TYPES_KEY]: DictionaryQualifierType[];\n [locale: string]: QualifiedDynamicLoaderTree | DictionaryQualifierType[];\n};\n\n/**\n * Type guard discriminating a qualified dynamic loader map (collections /\n * variants, possibly combined) from a plain dynamic loader map.\n */\nexport const isQualifiedDynamicLoaderMap = (\n value: unknown\n): value is QualifiedDynamicLoaderMap =>\n typeof value === 'object' &&\n value !== null &&\n QUALIFIER_DYNAMIC_TYPES_KEY in value;\n\n/**\n/** One targeted chunk: its stable cache key and lazy loader. */\ntype CollectedChunk = {\n cacheKey: string;\n loader: DynamicDictionaryLoader;\n};\n\ntype CollectedChunks = {\n /** True when the `item` axis is open (collection result → array). */\n itemAxisOpen: boolean;\n /** True when a required coordinate is absent (result → [] or null). */\n missed: boolean;\n /** The chunks the selector targets (in collection order for the item axis). */\n chunks: CollectedChunk[];\n};\n\n/**\n * Walks the loader tree following the selector and collects the chunk loaders\n * it targets — shared by the sync ({@link resolveQualifiedDynamicContent}) and\n * async ({@link resolveQualifiedDynamicContentAsync}) resolvers.\n */\nconst collectQualifiedChunks = (\n loaderMap: QualifiedDynamicLoaderMap,\n key: string,\n locale: string,\n selector: DictionarySelector | undefined\n): CollectedChunks => {\n const qualifierTypes = loaderMap[QUALIFIER_DYNAMIC_TYPES_KEY];\n const localeTree = loaderMap[locale] as\n | QualifiedDynamicLoaderTree\n | undefined;\n\n const itemAxisOpen =\n qualifierTypes.includes('item') && selector?.item === undefined;\n\n if (!localeTree) return { itemAxisOpen, missed: true, chunks: [] };\n\n const chunks: CollectedChunk[] = [];\n\n const walk = (\n node: QualifiedDynamicLoaderTree | DynamicDictionaryLoader,\n dimensions: DictionaryQualifierType[],\n segments: string[]\n ): boolean => {\n if (dimensions.length === 0) {\n chunks.push({\n cacheKey: `${key}.${locale}.${segments.join(COMPOSITE_ID_SEPARATOR)}`,\n loader: node as DynamicDictionaryLoader,\n });\n return true;\n }\n\n const [dimension, ...rest] = dimensions;\n const tree = node as QualifiedDynamicLoaderTree;\n\n if (dimension === 'item' && selector?.item === undefined) {\n // Open collection axis: fan out into every sibling chunk, ordered.\n for (const segment of Object.keys(tree).sort(\n (left, right) => Number(left) - Number(right)\n )) {\n walk(tree[segment]!, rest, [...segments, segment]);\n }\n return true;\n }\n\n const segment =\n dimension === 'variant'\n ? serializeVariant(selector?.variant)\n : String(selector?.item);\n\n const child = tree[segment];\n if (!child) return false;\n\n return walk(child, rest, [...segments, segment]);\n };\n\n const found = walk(localeTree, qualifierTypes, []);\n\n return { itemAxisOpen, missed: !found, chunks };\n};\n\n/**\n * Resolves the content of a qualified dynamic loader map against a selector,\n * loading only the chunk(s) the selector actually targets.\n *\n * Walks the nested loader tree one dimension at a time (canonical order\n * `variant → item`): `variant` defaults to `default` (or descends by the\n * serialized object identity), and `item` either narrows to the selected index\n * or — when no item is given — expands into every sibling chunk (the collection\n * axis). Semantics mirror {@link resolveQualifiedDictionary} so dynamic and\n * static modes behave alike.\n *\n * The Suspense mechanism is injected through `loadChunk` so the same logic\n * serves both the client (suspender cache) and the server (`react.use`). Every\n * targeted loader is started before the first chunk is read, so sibling chunks\n * load in parallel rather than waterfalling.\n *\n * @param loaderMap - The qualified dynamic loader map (entry point default export).\n * @param key - The dictionary key (used to build stable chunk cache keys).\n * @param locale - The resolved locale to load chunks for.\n * @param selector - The selector splitting the qualifier dimensions.\n * @param loadChunk - Reads a started chunk promise, suspending until it resolves.\n * @param transform - Turns a resolved chunk dictionary into final content.\n */\nexport const resolveQualifiedDynamicContent = <Content>(params: {\n loaderMap: QualifiedDynamicLoaderMap;\n key: string;\n locale: string;\n selector: DictionarySelector | undefined;\n loadChunk: (cacheKey: string, promise: Promise<Dictionary>) => Dictionary;\n transform: (dictionary: Dictionary) => Content;\n}): Content | Content[] | null => {\n const { loaderMap, key, locale, selector, loadChunk, transform } = params;\n\n const { itemAxisOpen, missed, chunks } = collectQualifiedChunks(\n loaderMap,\n key,\n locale,\n selector\n );\n\n if (missed) return itemAxisOpen ? [] : null;\n\n // Start every loader before reading, so siblings load in parallel.\n const dictionaries = chunks.map(({ cacheKey, loader }) =>\n loadChunk(cacheKey, loader())\n );\n\n if (itemAxisOpen) return dictionaries.map(transform);\n\n const [dictionary] = dictionaries;\n return dictionary ? transform(dictionary) : null;\n};\n\n/**\n * Async counterpart of {@link resolveQualifiedDynamicContent} for frameworks\n * that load dictionaries with `await` instead of Suspense (Vue, Svelte, Lit,\n * vanilla). Awaits every targeted chunk in parallel, then resolves identically.\n *\n * @param loaderMap - The qualified dynamic loader map.\n * @param key - The dictionary key (used to build stable chunk cache keys).\n * @param locale - The resolved locale to load chunks for.\n * @param selector - The selector splitting the qualifier dimensions.\n * @param transform - Turns a resolved chunk dictionary into final content.\n */\nexport const resolveQualifiedDynamicContentAsync = async <Content>(params: {\n loaderMap: QualifiedDynamicLoaderMap;\n key: string;\n locale: string;\n selector: DictionarySelector | undefined;\n transform: (dictionary: Dictionary) => Content;\n}): Promise<Content | Content[] | null> => {\n const { loaderMap, key, locale, selector, transform } = params;\n\n const { itemAxisOpen, missed, chunks } = collectQualifiedChunks(\n loaderMap,\n key,\n locale,\n selector\n );\n\n if (missed) return itemAxisOpen ? [] : null;\n\n const dictionaries = await Promise.all(chunks.map(({ loader }) => loader()));\n\n if (itemAxisOpen) return dictionaries.map(transform);\n\n const [dictionary] = dictionaries;\n return dictionary ? transform(dictionary) : null;\n};\n"],"mappings":";;;;;;;;AAaA,MAAa,kBAAkB,CAC7B,WACA,OACD;;;;;AAMD,MAAa,yBAAyB;;;;;;;;;;;;;AActC,MAAa,oBACX,YACW;AACX,KAAI,YAAY,OAAW,QAAO;AAClC,KAAI,OAAO,YAAY,SAAU,QAAO;AAExC,QAAO,OAAO,KAAK,QAAQ,CACxB,MAAM,CACN,KAAK,UAAU,GAAG,MAAM,GAAG,QAAQ,SAAS,CAC5C,KAAK,IAAI;;;;;;;AAQd,MAAa,+BACX,eAC8B;CAC9B,MAAM,qBAAgD,EAAE;AAExD,KAAI,WAAW,YAAY,OAAW,oBAAmB,KAAK,UAAU;AACxE,KAAI,OAAO,WAAW,SAAS,SAAU,oBAAmB,KAAK,OAAO;AAExE,QAAO;;;;;;;;;AAUT,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QAAQ,CAAC,CAAC;AAE5E,KAAI,aAAc,QAAO,aAAa,IAAI,UAAU;CAEpD,MAAM,CAAC,cAAc;AACrB,QAAO,aAAa,UAAU,WAAW,GAAG"}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -342,6 +342,7 @@ exports.resolveQualifiedDictionary = require_dictionaryManipulator_qualifiedDict
|
|
|
342
342
|
exports.resolveQualifiedDynamicContent = require_dictionaryManipulator_qualifiedDictionary.resolveQualifiedDynamicContent;
|
|
343
343
|
exports.resolveQualifiedDynamicContentAsync = require_dictionaryManipulator_qualifiedDictionary.resolveQualifiedDynamicContentAsync;
|
|
344
344
|
exports.sanitizer = require_markdown_utils.sanitizer;
|
|
345
|
+
exports.serializeVariant = require_dictionaryManipulator_qualifiedDictionary.serializeVariant;
|
|
345
346
|
exports.setLocaleInStorage = require_utils_localeStorage.setLocaleInStorage;
|
|
346
347
|
exports.setLocaleInStorageClient = require_utils_localeStorage.setLocaleInStorageClient;
|
|
347
348
|
exports.setLocaleInStorageServer = require_utils_localeStorage.setLocaleInStorageServer;
|
|
@@ -6,14 +6,14 @@ const require_interpreter_getContent_getContent = require('./getContent/getConte
|
|
|
6
6
|
/**
|
|
7
7
|
* Transforms a dictionary in a single pass, applying each plugin as needed.
|
|
8
8
|
*
|
|
9
|
-
* Also accepts a `QualifiedDictionaryGroup` (collections, variants
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
* Also accepts a `QualifiedDictionaryGroup` (collections, variants) together
|
|
10
|
+
* with a selector as second argument — the group is resolved to a single entry
|
|
11
|
+
* (or an ordered array of entries for collections without an `item` selector)
|
|
12
|
+
* before transformation.
|
|
13
13
|
*
|
|
14
14
|
* @param dictionary The dictionary (or qualified dictionary group) to transform.
|
|
15
15
|
* @param localeOrSelector The locale, or a selector object (`{ item }`,
|
|
16
|
-
* `{ variant }`,
|
|
16
|
+
* `{ variant }`, optionally with `locale`).
|
|
17
17
|
* @param plugins An array of NodeTransformer that define how to transform recognized nodes.
|
|
18
18
|
* If omitted, we’ll use a default set of plugins.
|
|
19
19
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDictionary.cjs","names":["parseDictionarySelector","getBasePlugins","resolveQualifiedDictionary","getContent"],"sources":["../../../src/interpreter/getDictionary.ts"],"sourcesContent":["import type {\n Dictionary,\n DictionarySelector,\n QualifiedDictionaryGroup,\n ResolveQualifiedDictionaryContent,\n} from '@intlayer/types/dictionary';\nimport type {\n DeclaredLocales,\n ExtractSelectorLocale,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport {\n parseDictionarySelector,\n resolveQualifiedDictionary,\n} from '../dictionaryManipulator/qualifiedDictionary';\nimport type {\n DeepTransformContent,\n IInterpreterPluginState,\n NodeProps,\n Plugins,\n} from './getContent';\nimport { getBasePlugins, getContent } from './getContent/getContent';\n\n/**\n * Transforms a dictionary in a single pass, applying each plugin as needed.\n *\n * Also accepts a `QualifiedDictionaryGroup` (collections, variants
|
|
1
|
+
{"version":3,"file":"getDictionary.cjs","names":["parseDictionarySelector","getBasePlugins","resolveQualifiedDictionary","getContent"],"sources":["../../../src/interpreter/getDictionary.ts"],"sourcesContent":["import type {\n Dictionary,\n DictionarySelector,\n QualifiedDictionaryGroup,\n ResolveQualifiedDictionaryContent,\n} from '@intlayer/types/dictionary';\nimport type {\n DeclaredLocales,\n ExtractSelectorLocale,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport {\n parseDictionarySelector,\n resolveQualifiedDictionary,\n} from '../dictionaryManipulator/qualifiedDictionary';\nimport type {\n DeepTransformContent,\n IInterpreterPluginState,\n NodeProps,\n Plugins,\n} from './getContent';\nimport { getBasePlugins, getContent } from './getContent/getContent';\n\n/**\n * Transforms a dictionary in a single pass, applying each plugin as needed.\n *\n * Also accepts a `QualifiedDictionaryGroup` (collections, variants) together\n * with a selector as second argument — the group is resolved to a single entry\n * (or an ordered array of entries for collections without an `item` selector)\n * before transformation.\n *\n * @param dictionary The dictionary (or qualified dictionary group) to transform.\n * @param localeOrSelector The locale, or a selector object (`{ item }`,\n * `{ variant }`, optionally with `locale`).\n * @param plugins An array of NodeTransformer that define how to transform recognized nodes.\n * If omitted, we’ll use a default set of plugins.\n */\nexport const getDictionary = <\n const T extends Dictionary | QualifiedDictionaryGroup,\n const A extends LocalesValues | DictionarySelector = DeclaredLocales,\n>(\n dictionary: T,\n localeOrSelector?: A,\n plugins?: Plugins[]\n): DeepTransformContent<\n ResolveQualifiedDictionaryContent<T, A>,\n IInterpreterPluginState,\n ExtractSelectorLocale<A>\n> => {\n const { locale, selector } = parseDictionarySelector(localeOrSelector);\n const appliedPlugins = plugins ?? getBasePlugins(locale);\n\n const resolved = resolveQualifiedDictionary(dictionary, selector);\n\n const transformDictionary = (resolvedDictionary: Dictionary) => {\n const props: NodeProps = {\n dictionaryKey: resolvedDictionary.key,\n dictionaryPath: resolvedDictionary.filePath,\n keyPath: [],\n plugins: appliedPlugins,\n };\n\n return getContent(resolvedDictionary.content, props, appliedPlugins);\n };\n\n if (resolved === null) return null as any;\n\n if (Array.isArray(resolved)) {\n return resolved.map(transformDictionary) as any;\n }\n\n return transformDictionary(resolved) as any;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAqCA,MAAa,iBAIX,YACA,kBACA,YAKG;CACH,MAAM,EAAE,QAAQ,aAAaA,0EAAwB,iBAAiB;CACtE,MAAM,iBAAiB,WAAWC,yDAAe,OAAO;CAExD,MAAM,WAAWC,6EAA2B,YAAY,SAAS;CAEjE,MAAM,uBAAuB,uBAAmC;EAC9D,MAAM,QAAmB;GACvB,eAAe,mBAAmB;GAClC,gBAAgB,mBAAmB;GACnC,SAAS,EAAE;GACX,SAAS;GACV;AAED,SAAOC,qDAAW,mBAAmB,SAAS,OAAO,eAAe;;AAGtE,KAAI,aAAa,KAAM,QAAO;AAE9B,KAAI,MAAM,QAAQ,SAAS,CACzB,QAAO,SAAS,IAAI,oBAAoB;AAG1C,QAAO,oBAAoB,SAAS"}
|
|
@@ -30,7 +30,7 @@ const warnedMissingDictionaries = /* @__PURE__ */ new Set();
|
|
|
30
30
|
* The second argument is either a locale (`'fr'`) or a selector object:
|
|
31
31
|
* - `{ item: 2 }` — collection item (omit `item` to get every item as array)
|
|
32
32
|
* - `{ variant: 'black-friday' }` — named variant (omit for the `default` one)
|
|
33
|
-
* - `{ id: 'prod_abc',
|
|
33
|
+
* - `{ variant: { id: 'prod_abc', userId: '123' } }` — structured variant
|
|
34
34
|
* - `locale` can be combined with any selector: `{ item: 2, locale: 'fr' }`
|
|
35
35
|
*/
|
|
36
36
|
const getIntlayer = (key, localeOrSelector, plugins) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getIntlayer.cjs","names":["parseDictionarySelector","getDictionarySelectorCacheKey","getDictionary"],"sources":["../../../src/interpreter/getIntlayer.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { colorizeKey, getAppLogger } from '@intlayer/config/logger';\nimport { getDictionaries } from '@intlayer/dictionaries-entry';\nimport type { DictionarySelector } from '@intlayer/types/dictionary';\nimport type {\n DeclaredLocales,\n DictionaryKeys,\n DictionaryRegistryResult,\n ExtractSelectorLocale,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport {\n getDictionarySelectorCacheKey,\n parseDictionarySelector,\n} from '../dictionaryManipulator/qualifiedDictionary';\nimport type {\n DeepTransformContent,\n IInterpreterPluginState,\n Plugins,\n} from './getContent';\nimport { getDictionary } from './getDictionary';\n\n/**\n * Creates a Recursive Proxy that returns the path of the accessed key\n * stringified. This prevents the app from crashing on undefined access.\n */\nconst createSafeFallback = (path = ''): any => {\n return new Proxy({} as Record<string | symbol, unknown>, {\n get: (_target, prop) => {\n if (\n prop === 'toJSON' ||\n prop === Symbol.toPrimitive ||\n prop === 'toString' ||\n prop === 'valueOf'\n ) {\n return () => path;\n }\n if (prop === 'then') {\n return undefined; // Prevent it from being treated as a Promise\n }\n if (prop === Symbol.iterator) {\n return function* () {\n yield path;\n };\n }\n\n // Recursively build the path (e.g., \"myDictionary.home.title\")\n const nextPath = path ? `${path}.${String(prop)}` : String(prop);\n return createSafeFallback(nextPath);\n },\n });\n};\n\nconst dictionaryCache = new Map<string, any>();\nconst warnedMissingDictionaries = new Set<string>();\n\n/**\n * Picks one dictionary by its key and returns its content for the given\n * locale or selector.\n *\n * The second argument is either a locale (`'fr'`) or a selector object:\n * - `{ item: 2 }` — collection item (omit `item` to get every item as array)\n * - `{ variant: 'black-friday' }` — named variant (omit for the `default` one)\n * - `{ id: 'prod_abc',
|
|
1
|
+
{"version":3,"file":"getIntlayer.cjs","names":["parseDictionarySelector","getDictionarySelectorCacheKey","getDictionary"],"sources":["../../../src/interpreter/getIntlayer.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { colorizeKey, getAppLogger } from '@intlayer/config/logger';\nimport { getDictionaries } from '@intlayer/dictionaries-entry';\nimport type { DictionarySelector } from '@intlayer/types/dictionary';\nimport type {\n DeclaredLocales,\n DictionaryKeys,\n DictionaryRegistryResult,\n ExtractSelectorLocale,\n LocalesValues,\n} from '@intlayer/types/module_augmentation';\nimport {\n getDictionarySelectorCacheKey,\n parseDictionarySelector,\n} from '../dictionaryManipulator/qualifiedDictionary';\nimport type {\n DeepTransformContent,\n IInterpreterPluginState,\n Plugins,\n} from './getContent';\nimport { getDictionary } from './getDictionary';\n\n/**\n * Creates a Recursive Proxy that returns the path of the accessed key\n * stringified. This prevents the app from crashing on undefined access.\n */\nconst createSafeFallback = (path = ''): any => {\n return new Proxy({} as Record<string | symbol, unknown>, {\n get: (_target, prop) => {\n if (\n prop === 'toJSON' ||\n prop === Symbol.toPrimitive ||\n prop === 'toString' ||\n prop === 'valueOf'\n ) {\n return () => path;\n }\n if (prop === 'then') {\n return undefined; // Prevent it from being treated as a Promise\n }\n if (prop === Symbol.iterator) {\n return function* () {\n yield path;\n };\n }\n\n // Recursively build the path (e.g., \"myDictionary.home.title\")\n const nextPath = path ? `${path}.${String(prop)}` : String(prop);\n return createSafeFallback(nextPath);\n },\n });\n};\n\nconst dictionaryCache = new Map<string, any>();\nconst warnedMissingDictionaries = new Set<string>();\n\n/**\n * Picks one dictionary by its key and returns its content for the given\n * locale or selector.\n *\n * The second argument is either a locale (`'fr'`) or a selector object:\n * - `{ item: 2 }` — collection item (omit `item` to get every item as array)\n * - `{ variant: 'black-friday' }` — named variant (omit for the `default` one)\n * - `{ variant: { id: 'prod_abc', userId: '123' } }` — structured variant\n * - `locale` can be combined with any selector: `{ item: 2, locale: 'fr' }`\n */\nexport const getIntlayer = <\n const T extends DictionaryKeys,\n const A extends LocalesValues | DictionarySelector = DeclaredLocales,\n>(\n key: T,\n localeOrSelector?: A,\n plugins?: Plugins[]\n): DeepTransformContent<\n DictionaryRegistryResult<T, A>,\n IInterpreterPluginState,\n ExtractSelectorLocale<A>\n> => {\n const dictionaries = getDictionaries();\n const dictionary = dictionaries[key as T];\n\n if (!dictionary && process.env.NODE_ENV === 'development') {\n if (!warnedMissingDictionaries.has(key as string)) {\n // Log a warning instead of throwing (so developers know it's missing)\n const logger = getAppLogger({ log });\n logger(\n typeof window === 'undefined'\n ? `Dictionary ${colorizeKey(key)} was not found. Using fallback proxy.`\n : `Dictionary ${key} was not found. Using fallback proxy.`,\n {\n level: 'warn',\n }\n );\n warnedMissingDictionaries.add(key as string);\n }\n\n return createSafeFallback(key as string);\n }\n\n let locale: LocalesValues | undefined;\n let selectorCacheKey = '';\n\n if (process.env.INTLAYER_DICTIONARY_SELECTOR !== 'false') {\n const parsed = parseDictionarySelector(localeOrSelector);\n locale = parsed.locale;\n selectorCacheKey = getDictionarySelectorCacheKey(parsed.selector);\n } else {\n // Selectors are unused in this project (build-time flag): the second\n // argument can only be a locale, so the selector parsing is dead code.\n locale = localeOrSelector as LocalesValues | undefined;\n }\n\n const cacheKey = `${key}_${locale ?? 'default'}_${selectorCacheKey}_${plugins ? 'custom_plugins' : 'default_plugins'}`;\n\n if (dictionaryCache.has(cacheKey)) {\n return dictionaryCache.get(cacheKey);\n }\n\n const result = getDictionary(dictionary, localeOrSelector, plugins);\n\n dictionaryCache.set(cacheKey, result);\n\n return result as any;\n};\n"],"mappings":";;;;;;;;;;;;;AA0BA,MAAM,sBAAsB,OAAO,OAAY;AAC7C,QAAO,IAAI,MAAM,EAAE,EAAsC,EACvD,MAAM,SAAS,SAAS;AACtB,MACE,SAAS,YACT,SAAS,OAAO,eAChB,SAAS,cACT,SAAS,UAET,cAAa;AAEf,MAAI,SAAS,OACX;AAEF,MAAI,SAAS,OAAO,SAClB,QAAO,aAAa;AAClB,SAAM;;AAMV,SAAO,mBADU,OAAO,GAAG,KAAK,GAAG,OAAO,KAAK,KAAK,OAAO,KAAK,CAC7B;IAEtC,CAAC;;AAGJ,MAAM,kCAAkB,IAAI,KAAkB;AAC9C,MAAM,4CAA4B,IAAI,KAAa;;;;;;;;;;;AAYnD,MAAa,eAIX,KACA,kBACA,YAKG;CAEH,MAAM,gEAAyB,CAAC;AAEhC,KAAI,CAAC,cAAc,QAAQ,IAAI,aAAa,eAAe;AACzD,MAAI,CAAC,0BAA0B,IAAI,IAAc,EAAE;AAGjD,6CAD4B,EAAE,iCAAK,CAC7B,CACJ,OAAO,WAAW,cACd,uDAA0B,IAAI,CAAC,yCAC/B,cAAc,IAAI,wCACtB,EACE,OAAO,QACR,CACF;AACD,6BAA0B,IAAI,IAAc;;AAG9C,SAAO,mBAAmB,IAAc;;CAG1C,IAAI;CACJ,IAAI,mBAAmB;AAEvB,KAAI,QAAQ,IAAI,iCAAiC,SAAS;EACxD,MAAM,SAASA,0EAAwB,iBAAiB;AACxD,WAAS,OAAO;AAChB,qBAAmBC,gFAA8B,OAAO,SAAS;OAIjE,UAAS;CAGX,MAAM,WAAW,GAAG,IAAI,GAAG,UAAU,UAAU,GAAG,iBAAiB,GAAG,UAAU,mBAAmB;AAEnG,KAAI,gBAAgB,IAAI,SAAS,CAC/B,QAAO,gBAAgB,IAAI,SAAS;CAGtC,MAAM,SAASC,gDAAc,YAAY,kBAAkB,QAAQ;AAEnE,iBAAgB,IAAI,UAAU,OAAO;AAErC,QAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { COMPOSITE_ID_SEPARATOR, QUALIFIER_DYNAMIC_TYPES_KEY, QUALIFIER_ORDER, getDictionaryCompositeId, getDictionaryQualifierId, getDictionaryQualifierSegments, getDictionaryQualifierTypes, getDictionarySelectorCacheKey, isQualifiedDictionaryGroup, isQualifiedDynamicLoaderMap, parseDictionarySelector, reconstructQualifiedEntry, resolveQualifiedDictionary, resolveQualifiedDynamicContent, resolveQualifiedDynamicContentAsync } from "./qualifiedDictionary.mjs";
|
|
1
|
+
import { COMPOSITE_ID_SEPARATOR, QUALIFIER_DYNAMIC_TYPES_KEY, QUALIFIER_ORDER, getDictionaryCompositeId, getDictionaryQualifierId, getDictionaryQualifierSegments, getDictionaryQualifierTypes, getDictionarySelectorCacheKey, isQualifiedDictionaryGroup, isQualifiedDynamicLoaderMap, parseDictionarySelector, reconstructQualifiedEntry, resolveQualifiedDictionary, resolveQualifiedDynamicContent, resolveQualifiedDynamicContentAsync, serializeVariant } from "./qualifiedDictionary.mjs";
|
|
2
2
|
import { editDictionaryByKeyPath } from "./editDictionaryByKeyPath.mjs";
|
|
3
3
|
import { getContentNodeByKeyPath } from "./getContentNodeByKeyPath.mjs";
|
|
4
4
|
import { getDefaultNode } from "./getDefaultNode.mjs";
|
|
@@ -13,4 +13,4 @@ import { removeContentNodeByKeyPath } from "./removeContentNodeByKeyPath.mjs";
|
|
|
13
13
|
import { renameContentNodeByKeyPath } from "./renameContentNodeByKeyPath.mjs";
|
|
14
14
|
import { updateNodeChildren } from "./updateNodeChildren.mjs";
|
|
15
15
|
|
|
16
|
-
export { COMPOSITE_ID_SEPARATOR, QUALIFIER_DYNAMIC_TYPES_KEY, QUALIFIER_ORDER, editDictionaryByKeyPath, getContentNodeByKeyPath, getDefaultNode, getDictionaryCompositeId, getDictionaryQualifierId, getDictionaryQualifierSegments, getDictionaryQualifierTypes, getDictionarySelectorCacheKey, getEmptyNode, getNodeChildren, getNodeType, isQualifiedDictionaryGroup, isQualifiedDynamicLoaderMap, mergeDictionaries, mergeQualifiedDictionaries, normalizeDictionaries, normalizeDictionary, orderDictionaries, parseDictionarySelector, reconstructQualifiedEntry, removeContentNodeByKeyPath, renameContentNodeByKeyPath, resolveQualifiedDictionary, resolveQualifiedDynamicContent, resolveQualifiedDynamicContentAsync, updateNodeChildren };
|
|
16
|
+
export { COMPOSITE_ID_SEPARATOR, QUALIFIER_DYNAMIC_TYPES_KEY, QUALIFIER_ORDER, editDictionaryByKeyPath, getContentNodeByKeyPath, getDefaultNode, getDictionaryCompositeId, getDictionaryQualifierId, getDictionaryQualifierSegments, getDictionaryQualifierTypes, getDictionarySelectorCacheKey, getEmptyNode, getNodeChildren, getNodeType, isQualifiedDictionaryGroup, isQualifiedDynamicLoaderMap, mergeDictionaries, mergeQualifiedDictionaries, normalizeDictionaries, normalizeDictionary, orderDictionaries, parseDictionarySelector, reconstructQualifiedEntry, removeContentNodeByKeyPath, renameContentNodeByKeyPath, resolveQualifiedDictionary, resolveQualifiedDynamicContent, resolveQualifiedDynamicContentAsync, serializeVariant, updateNodeChildren };
|
|
@@ -11,7 +11,7 @@ import { colorizeKey, getAppLogger } from "@intlayer/config/logger";
|
|
|
11
11
|
* `mergeDictionaries` (single merged dictionary).
|
|
12
12
|
* - At least one dictionary declares a qualifier → the group's dimension set is
|
|
13
13
|
* the union of every declared dimension (in canonical order
|
|
14
|
-
* `variant →
|
|
14
|
+
* `variant → item`). Dictionaries are grouped by their composite id
|
|
15
15
|
* (one segment per dimension), merged within each group (locale completion /
|
|
16
16
|
* priority overrides preserved), and a `QualifiedDictionaryGroup` is returned.
|
|
17
17
|
* Unqualified siblings act as shared base content merged into every entry.
|
|
@@ -43,13 +43,10 @@ const mergeQualifiedDictionaries = (dictionaries) => {
|
|
|
43
43
|
entriesDictionaries.set(compositeId, existingEntries);
|
|
44
44
|
});
|
|
45
45
|
const content = {};
|
|
46
|
-
const metaByCompositeId = {};
|
|
47
|
-
const declaresMeta = groupQualifierTypes.includes("meta");
|
|
48
46
|
let importMode;
|
|
49
47
|
for (const [compositeId, qualifiedDictionaries] of entriesDictionaries) {
|
|
50
48
|
content[compositeId] = mergeDictionaries([...qualifiedDictionaries, ...baseDictionaries]).content;
|
|
51
49
|
const [firstQualified] = qualifiedDictionaries;
|
|
52
|
-
if (declaresMeta && firstQualified?.meta !== void 0) metaByCompositeId[compositeId] = firstQualified.meta;
|
|
53
50
|
importMode ??= firstQualified?.importMode;
|
|
54
51
|
}
|
|
55
52
|
const localIds = Array.from(new Set(dictionaries.filter((dictionary) => dictionary.localId).map((dictionary) => dictionary.localId)));
|
|
@@ -57,7 +54,6 @@ const mergeQualifiedDictionaries = (dictionaries) => {
|
|
|
57
54
|
key: dictionaries[0].key,
|
|
58
55
|
qualifierTypes: groupQualifierTypes,
|
|
59
56
|
content,
|
|
60
|
-
...declaresMeta && { meta: metaByCompositeId },
|
|
61
57
|
...importMode !== void 0 && { importMode },
|
|
62
58
|
localIds
|
|
63
59
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mergeQualifiedDictionaries.mjs","names":[],"sources":["../../../src/dictionaryManipulator/mergeQualifiedDictionaries.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { colorizeKey, getAppLogger } from '@intlayer/config/logger';\nimport type {\n Dictionary,\n
|
|
1
|
+
{"version":3,"file":"mergeQualifiedDictionaries.mjs","names":[],"sources":["../../../src/dictionaryManipulator/mergeQualifiedDictionaries.ts"],"sourcesContent":["import { log } from '@intlayer/config/built';\nimport { colorizeKey, getAppLogger } from '@intlayer/config/logger';\nimport type {\n Dictionary,\n DictionaryQualifierType,\n QualifiedDictionaryGroup,\n} from '@intlayer/types/dictionary';\nimport { mergeDictionaries } from './mergeDictionaries';\nimport {\n getDictionaryCompositeId,\n getDictionaryQualifierTypes,\n QUALIFIER_ORDER,\n} from './qualifiedDictionary';\n\n/**\n * Merges sibling dictionaries sharing the same key, honouring qualifiers.\n *\n * - No dictionary declares a qualifier → behaves exactly like\n * `mergeDictionaries` (single merged dictionary).\n * - At least one dictionary declares a qualifier → the group's dimension set is\n * the union of every declared dimension (in canonical order\n * `variant → item`). Dictionaries are grouped by their composite id\n * (one segment per dimension), merged within each group (locale completion /\n * priority overrides preserved), and a `QualifiedDictionaryGroup` is returned.\n * Unqualified siblings act as shared base content merged into every entry.\n *\n * Every qualified entry must declare ALL dimensions of the group; an entry that\n * declares only a subset is ambiguous and is rejected with an error log.\n */\nexport const mergeQualifiedDictionaries = (\n dictionaries: Dictionary[]\n): Dictionary | QualifiedDictionaryGroup => {\n const perDictionaryTypes = dictionaries.map(getDictionaryQualifierTypes);\n\n const declaredDimensions = new Set<DictionaryQualifierType>();\n for (const types of perDictionaryTypes) {\n for (const type of types) declaredDimensions.add(type);\n }\n\n // Canonical order, restricted to the dimensions actually declared.\n const groupQualifierTypes = QUALIFIER_ORDER.filter((qualifierType) =>\n declaredDimensions.has(qualifierType)\n );\n\n if (groupQualifierTypes.length === 0) {\n return mergeDictionaries(dictionaries);\n }\n\n const appLogger = getAppLogger({ log });\n\n const baseDictionaries: Dictionary[] = [];\n const entriesDictionaries = new Map<string, Dictionary[]>();\n\n dictionaries.forEach((dictionary, index) => {\n if (perDictionaryTypes[index].length === 0) {\n baseDictionaries.push(dictionary);\n return;\n }\n\n const compositeId = getDictionaryCompositeId(\n dictionary,\n groupQualifierTypes\n );\n\n if (compositeId === undefined) {\n appLogger(\n `Dictionary ${colorizeKey(dictionary.key)} declares (${perDictionaryTypes[index].join(', ')}) but the key's dimensions are (${groupQualifierTypes.join(', ')}); every entry must declare all of them. Entry ignored${dictionary.filePath ? ` - ${dictionary.filePath}` : ''}.`,\n { level: 'error' }\n );\n return;\n }\n\n const existingEntries = entriesDictionaries.get(compositeId) ?? [];\n existingEntries.push(dictionary);\n entriesDictionaries.set(compositeId, existingEntries);\n });\n\n // `content` maps each composite id to its resolved content node directly; the\n // qualifier coordinates live in the key, not in a per-entry wrapper. For an\n // object variant the variant segment is the canonical serialization of the\n // object, so it fully identifies the entry — no side-map is needed.\n const content: Record<string, unknown> = {};\n\n let importMode: Dictionary['importMode'];\n\n for (const [compositeId, qualifiedDictionaries] of entriesDictionaries) {\n // Unqualified siblings act as shared base content: appended last so the\n // qualified entries take precedence (mergeDictionaries prefers first).\n const mergedEntry = mergeDictionaries([\n ...qualifiedDictionaries,\n ...baseDictionaries,\n ]);\n\n content[compositeId] = mergedEntry.content;\n\n const [firstQualified] = qualifiedDictionaries;\n\n importMode ??= firstQualified?.importMode;\n }\n\n const localIds = Array.from(\n new Set(\n dictionaries\n .filter((dictionary) => dictionary.localId)\n .map((dictionary) => dictionary.localId!)\n )\n );\n\n return {\n key: dictionaries[0]!.key,\n qualifierTypes: groupQualifierTypes,\n content,\n ...(importMode !== undefined && { importMode }),\n localIds,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,8BACX,iBAC0C;CAC1C,MAAM,qBAAqB,aAAa,IAAI,4BAA4B;CAExE,MAAM,qCAAqB,IAAI,KAA8B;AAC7D,MAAK,MAAM,SAAS,mBAClB,MAAK,MAAM,QAAQ,MAAO,oBAAmB,IAAI,KAAK;CAIxD,MAAM,sBAAsB,gBAAgB,QAAQ,kBAClD,mBAAmB,IAAI,cAAc,CACtC;AAED,KAAI,oBAAoB,WAAW,EACjC,QAAO,kBAAkB,aAAa;CAGxC,MAAM,YAAY,aAAa,EAAE,KAAK,CAAC;CAEvC,MAAM,mBAAiC,EAAE;CACzC,MAAM,sCAAsB,IAAI,KAA2B;AAE3D,cAAa,SAAS,YAAY,UAAU;AAC1C,MAAI,mBAAmB,OAAO,WAAW,GAAG;AAC1C,oBAAiB,KAAK,WAAW;AACjC;;EAGF,MAAM,cAAc,yBAClB,YACA,oBACD;AAED,MAAI,gBAAgB,QAAW;AAC7B,aACE,cAAc,Y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