UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

69 lines (67 loc) 4.03 kB
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs'); const require_deepTransformPlugins_getMultilingualDictionary = require('../deepTransformPlugins/getMultilingualDictionary.cjs'); const require_dictionaryManipulator_getNodeType = require('./getNodeType.cjs'); let __intlayer_config_client = require("@intlayer/config/client"); let __intlayer_config_built = require("@intlayer/config/built"); __intlayer_config_built = require_rolldown_runtime.__toESM(__intlayer_config_built); //#region src/dictionaryManipulator/mergeDictionaries.ts const checkTypesMatch = (object1, object2, object2LocalId, dictionaryKey, path = []) => { const appLogger = (0, __intlayer_config_client.getAppLogger)(__intlayer_config_built.default); if (object1 === void 0 || object1 === null || object2 === void 0 || object2 === null) return; const type1 = require_dictionaryManipulator_getNodeType.getNodeType(object1); const type2 = require_dictionaryManipulator_getNodeType.getNodeType(object2); if (type1 === "unknown" || type2 === "unknown") return; if (type1 !== type2) { appLogger([`Error: Dictionary ${(0, __intlayer_config_client.colorizeKey)(dictionaryKey)} has a multiple content files with type mismatch at path "${path.join(".")}": Cannot merge ${type1} with ${type2} while merging ${object2LocalId}`], { level: "error" }); return; } }; const customMerge = (destination, source) => { if (destination === void 0 || destination === null) return source; if (source === void 0 || source === null) return destination; if (typeof destination !== "object" || typeof source !== "object") return destination; if (Array.isArray(destination) && Array.isArray(source)) return arrayMerge(destination, source); if (typeof destination === "object" && typeof source === "object") { const result = {}; const allKeys = new Set([...Object.keys(destination), ...Object.keys(source)]); for (const key of allKeys) result[key] = customMerge(destination[key], source[key]); return result; } return destination; }; const arrayMerge = (destinationArray, sourceArray) => { const destHasOnlyPrimitives = destinationArray.every((item) => typeof item !== "object" || item === null); const sourceHasOnlyPrimitives = sourceArray.every((item) => typeof item !== "object" || item === null); if (destHasOnlyPrimitives && sourceHasOnlyPrimitives) return sourceArray; const result = []; const maxLength = Math.max(destinationArray.length, sourceArray.length); for (let i = 0; i < maxLength; i++) { const destItem = destinationArray[i]; const sourceItem = sourceArray[i]; if (destItem === void 0 && sourceItem === void 0) {} else if (destItem === void 0) result.push(sourceItem); else if (sourceItem === void 0) result.push(destItem); else if (typeof destItem === "object" && typeof sourceItem === "object" && destItem !== null && sourceItem !== null) if ("key" in destItem && "key" in sourceItem && destItem.key === sourceItem.key) result.push(customMerge(destItem, sourceItem)); else result.push(customMerge(destItem, sourceItem)); else result.push(destItem); } return result; }; const mergeDictionaries = (dictionaries) => { const localIds = Array.from(new Set(dictionaries.filter((dict) => dict.localId).map((dict) => dict.localId))); const dictionariesKeys = dictionaries.map((dict) => dict.key); if (new Set(dictionariesKeys).size !== 1) throw new Error("All dictionaries must have the same key"); let mergedContent = dictionaries[0].content; for (let i = 1; i < dictionaries.length; i++) { const currentDictionary = require_deepTransformPlugins_getMultilingualDictionary.getMultilingualDictionary(dictionaries[i]); checkTypesMatch(mergedContent, currentDictionary.content, currentDictionary.localId, currentDictionary.key, []); mergedContent = customMerge(mergedContent, currentDictionary.content); } return { key: dictionaries[0].key, content: mergedContent, localIds }; }; //#endregion exports.mergeDictionaries = mergeDictionaries; //# sourceMappingURL=mergeDictionaries.cjs.map