@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
67 lines (65 loc) • 3.56 kB
JavaScript
import { getMultilingualDictionary } from "../deepTransformPlugins/getMultilingualDictionary.mjs";
import { getNodeType } from "./getNodeType.mjs";
import { colorizeKey, getAppLogger } from "@intlayer/config/client";
import configuration from "@intlayer/config/built";
//#region src/dictionaryManipulator/mergeDictionaries.ts
const checkTypesMatch = (object1, object2, object2LocalId, dictionaryKey, path = []) => {
const appLogger = getAppLogger(configuration);
if (object1 === void 0 || object1 === null || object2 === void 0 || object2 === null) return;
const type1 = getNodeType(object1);
const type2 = getNodeType(object2);
if (type1 === "unknown" || type2 === "unknown") return;
if (type1 !== type2) {
appLogger([`Error: Dictionary ${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 = 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
export { mergeDictionaries };
//# sourceMappingURL=mergeDictionaries.mjs.map