UNPKG

@jsverse/transloco-keys-manager

Version:

Extract translatable keys from projects that uses Transloco

28 lines 1.14 kB
import { messages } from '../messages.js'; import { getLogger } from './logger.js'; import { isObject } from './validators.utils.js'; export function countKeys(obj) { return Object.keys(obj).reduce((acc, curr) => (isObject(obj[curr]) ? acc + countKeys(obj[curr]) : ++acc), 0); } export function checkForProblematicUnflatKeys(obj) { const sortedKeys = Object.keys(obj).sort(); const problematicKeys = []; const lastKeyIndex = sortedKeys.length - 1; for (let i = 0; i < lastKeyIndex;) { const key = sortedKeys[i]; const prefix = `${key}.`; let isChildKey = sortedKeys[++i].startsWith(prefix); if (isChildKey) { problematicKeys.push(key); while (isChildKey && i <= lastKeyIndex) { problematicKeys.push(sortedKeys[i]); isChildKey = i < lastKeyIndex && sortedKeys[++i].startsWith(prefix); } } } if (problematicKeys.length) { const logger = getLogger(); logger.log('\x1b[31m%s\x1b[0m', '⚠️', messages.problematicKeysForUnflat(problematicKeys)); } } //# sourceMappingURL=keys.utils.js.map