@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
46 lines (44 loc) • 1.65 kB
JavaScript
import configuration from "@intlayer/config/built";
//#region src/dictionaryManipulator/orderDictionaries.ts
/**
* Orders dictionaries based on the dictionary priority strategy.
*
* @param dictionaries - Array of dictionaries to order
* @param priorityStrategy - The priority strategy ('local_first' or 'distant_first')
* @returns Ordered array of dictionaries
*/
const orderDictionaries = (dictionaries, configuration$1 = configuration) => {
const { editor } = configuration$1;
const { dictionaryPriorityStrategy } = editor;
if (dictionaries.length <= 1) return dictionaries;
const withIndex = dictionaries.map((dictionary, index) => ({
dictionary,
index
}));
const getPriority = (dictionary) => {
const p = dictionary.priority ?? 0;
return Number.isFinite(p) ? p : 0;
};
const getLocationWeight = (d) => {
const location = d.location;
if (location === void 0) return 2;
if (dictionaryPriorityStrategy === "distant_first") return location === "remote" ? 0 : 1;
return location === "local" ? 0 : 1;
};
withIndex.sort((a, b) => {
const aAuto = a.dictionary.filled ? 1 : 0;
const bAuto = b.dictionary.filled ? 1 : 0;
if (aAuto !== bAuto) return aAuto - bAuto;
const aP = getPriority(a.dictionary);
const bP = getPriority(b.dictionary);
if (aP !== bP) return bP - aP;
const aLocation = getLocationWeight(a.dictionary);
const bLocation = getLocationWeight(b.dictionary);
if (aLocation !== bLocation) return aLocation - bLocation;
return a.index - b.index;
});
return withIndex.map(({ dictionary }) => dictionary);
};
//#endregion
export { orderDictionaries };
//# sourceMappingURL=orderDictionaries.mjs.map