Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 1x 1x 20x 21x 6x 6x 6x 2x 2x 6x 6x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 79x 79x 79x 1x 1x 1x 1x 1x 1x 1x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 79x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { getAST, states } from './key-ast.util.js' import { calculatePriority } from './priority-calculator.js' import { getMatcher } from './template-matcher.js' /** * @param {Token} token - parsed key AST * @returns {string} normalized text based on AST token */ const normalizeDefaultToken = (token) => token.text /** * @param {Token} token - parsed key AST * @returns {string} normalized text based on AST token */ function normalizedCaptureToken (token) { const { childTokens } = token if (childTokens.length <= 0) { return '{}' } let normalizedCapture = normalizeToken(childTokens[0]) for (let i = 1, e = childTokens.length; i < e; i++) { const previousToken = childTokens[i - 1] const token = childTokens[i] if (previousToken.type === states.capture_expr && token.type === states.capture_expr) { normalizedCapture += ' ' } normalizedCapture += normalizeToken(token) } return `{${normalizedCapture}}` } /** * Uses the target abstract syntax tree token to normalize the key * @param {Token} token - parsed key AST * @returns {string} normalized text based on AST token */ const normalizeToken = (token) => token.type === states.capture ? normalizedCaptureToken(token) : normalizeDefaultToken(token) /** * Uses the abstract syntax tree to normalize the key * @param {AST} ast - parsed key AST * @returns {string} normalized text based on AST */ function getNormalizedKey (ast) { return ast.tokens.map(normalizeToken).join('') } /** * Parses i18n key * @param {string} key - target key to parse * @returns {ParseResult} - parse result */ export function parseKey (key) { const ast = getAST(key) const { priority, priorityAsNumber } = calculatePriority(ast) const match = getMatcher(ast) const normalizedKey = getNormalizedKey(ast) return { priority, priorityAsNumber, key, ast, match, normalizedKey, matches: (text) => match(text).isMatch, } } /// types /** * @typedef {object} ParseResult * Represents the result of parsing a defined key * @property {[number, number]} priority * Defines the key priority. * * When finding conflicting keys the one with the hightest priority is chosen * * The priority is defined by specificity, the more specific, the highest * priority it has. * * The specificity is defined by 2 factors: * - the lesser number of parameter * - the specificity of each parameter * * That is what those 2 values means in the priority key, the first value is * the number of parameters in the key, and the sum of each parameter specificity value * @property {number} priorityAsNumber * `ParseResult.priority` represented value as a number, to simplify comparing keys priorities, see {@link ParseResult.priority} * @property {string} key - target key used to parse * @property {string} normalizedKey - normalized target key used to parse * @property {MatchPredicate} matches - Predicate to check if a defined text matches the key * @property {ReturnType<typeof getMatcher>} match - matcher of target key * @property {AST} ast - Abstract syntax tree generated as result from parsing the key */ /** * @callback MatchPredicate * Predicate to check if a defined text matches the key * @param {string} text - target text to parse * @returns {boolean} */ /** @typedef {import('./key-ast.util.js').Token} Token */ /** @typedef {import('./key-ast.util.js').AST} AST */ |