@ts-intl/dictionary
Version:
I18n dictionary/messages generator
34 lines (33 loc) • 1.94 kB
JavaScript
import { buildTrieByNSPath } from '@ts-intl/shared';
export const extractDictionary = (dict, fallbackDict, include, exclude, logger) => {
const path = [];
const dfs = (base, target, includeTrie, excludeTrie) => {
const resDict = {};
Object.entries(base).forEach(([key, baseChild]) => {
const includeTrieChild = includeTrie === null || includeTrie === void 0 ? void 0 : includeTrie.get(key);
const excludeTrieChild = excludeTrie === null || excludeTrie === void 0 ? void 0 : excludeTrie.get(key);
if ((includeTrie && !includeTrieChild) || (excludeTrieChild === null || excludeTrieChild === void 0 ? void 0 : excludeTrieChild.isLeaf))
return;
path.push(key);
let targetChild = target === null || target === void 0 ? void 0 : target[key];
if (typeof baseChild === 'string') {
// endpoint
if (!targetChild || typeof targetChild !== 'string') {
targetChild = baseChild;
logger === null || logger === void 0 ? void 0 : logger(`missed value of ${path.join('/')}`);
}
resDict[key] = targetChild;
}
else {
if (!targetChild || typeof targetChild === 'string') {
targetChild = undefined;
logger === null || logger === void 0 ? void 0 : logger(`missed key of ${path.join('/')}`);
}
resDict[key] = dfs(baseChild, targetChild, (includeTrieChild === null || includeTrieChild === void 0 ? void 0 : includeTrieChild.isLeaf) ? undefined : includeTrieChild, excludeTrieChild);
}
path.pop();
});
return resDict;
};
return dfs(fallbackDict, dict, include instanceof Array ? buildTrieByNSPath(include) : include, exclude instanceof Array ? buildTrieByNSPath(exclude) : exclude);
};