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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 20x 20x 16x 16x 16x 16x 16x 20x 4x 4x 23x 1x 1x 1x 1x 1x 24x 24x 1x 1x 23x 23x 23x 24x 29x 29x 26x 26x 29x 29x 2x 2x 2x 2x 2x 2x 2x 2x 29x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 24x 22x 22x 22x 22x 22x 22x 22x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 24x 22x 24x 3x 3x 19x 19x 19x 19x 24x 20x 20x 4x 4x 16x 16x 15x 15x 15x 15x 15x 15x 15x 15x 15x 1x 1x 1x 1x 1x 82x 82x 82x 82x 59x 59x 59x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 82x 82x 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 { states } from './key-ast.util.js' import { escape } from '../utils/algorithms/regex.utils.js' import { captureExpressions } from './capture-expression-values.js' const falsePredicate = () => false /** @type {Readonly<[]> } */ const emptyArray = Object.freeze([]) /** @type {ParameterMatchResult} */ const anyMatchExpression = Object.freeze({ isMatch: true, expressionInfo: captureExpressions.special.any, }) const noMatchExpression = Object.freeze({ isMatch: false, }) const anyMatchCaptureExpressionsInfo = { matchPredicate: () => anyMatchExpression, } /** @type {MatchResult} */ const noMatch = Object.freeze({ isMatch: false, parameters: emptyArray, defaultFormatters: emptyArray, }) /** @type {MatchResult} */ const emptyYesMatch = Object.freeze({ isMatch: true, parameters: emptyArray, defaultFormatters: emptyArray, }) /** * @param {string} textToMatch - target text to match * @returns {Matcher} - match result */ const exactStringMatcher = (textToMatch) => (text) => (textToMatch === text) ? emptyYesMatch : noMatch /** * @param {CaptureExpressionsInfoDetail[]} details - target details * @returns {CaptureExpressionInfo} expression info from details */ const captureExpressionInfoFromDetails = (details) => ({ matchPredicate: (text) => { for (const expressionPart of details) { if (expressionPart.matches(text)) { return { isMatch: true, expressionInfo: expressionPart.expressionInfo, } } } return noMatchExpression }, }) /** * @param {import('./key-ast.util.js').Token} captureToken - capture token from AST of parsed key * @returns {CaptureExpressionInfo} its respective expression info */ function captureExpressionInfoFromToken (captureToken) { if (captureToken.childTokens.length === 0) { return anyMatchCaptureExpressionsInfo } /** @type {CaptureExpressionsInfoDetail[]} */ const fragmentedCaptureExpressionsInfo = [] let currentExpression = '' for (const token of captureToken.childTokens) { switch (token.type) { case states.capture_expr: currentExpression = currentExpression ? `${currentExpression} ${token.text}` : token.text continue case states.capture_expr_sep: fragmentedCaptureExpressionsInfo.push({ type: 'expression', text: currentExpression, expressionInfo: captureExpressions.named[currentExpression], matches: captureExpressions.named[currentExpression]?.matchPredicate() ?? falsePredicate, }) currentExpression = '' continue case states.regex: fragmentedCaptureExpressionsInfo.push({ type: 'regex', text: token.text, expressionInfo: captureExpressions.special.regex, matches: captureExpressions.special.regex.matchPredicate(token.text.slice(1, -1)), }) continue case states.sq_string: case states.dq_string: case states.bt_string: fragmentedCaptureExpressionsInfo.push({ type: 'string', text: token.text, expressionInfo: captureExpressions.special.string, matches: captureExpressions.special.string.matchPredicate(token.text), }) continue } } if (currentExpression) { fragmentedCaptureExpressionsInfo.push({ type: 'expression', text: currentExpression, expressionInfo: captureExpressions.named[currentExpression], matches: captureExpressions.named[currentExpression]?.matchPredicate() ?? falsePredicate, }) } return captureExpressionInfoFromDetails(fragmentedCaptureExpressionsInfo) } /** * @param {RegExp} regex - used to validate for `text` match * @param {CaptureExpressionInfo[]} captureExpressionsInfo - to validate each capture token match * @returns {Matcher} matcher function */ const expressionMatcher = (regex, captureExpressionsInfo) => (text) => { if (typeof text !== 'string') return noMatch const matches = text.match(regex) if (matches == null) { return noMatch } const parameters = matches.slice(1) const paramMatchInfo = [] for (const [index, text] of parameters.entries()) { const matchResult = captureExpressionsInfo[index].matchPredicate(text) if (!matchResult.isMatch) { return noMatch } paramMatchInfo.push(matchResult.expressionInfo) } const defaultFormatters = paramMatchInfo.map((info) => info.defaultFormat) return { isMatch: true, parameters, defaultFormatters, } } /** * @param {import('./key-ast.util.js').Token[]} tokens - tokens from AST of parsed key * @returns {Matcher} matcher function */ function getMatcherFromTokens (tokens) { const captureTokens = tokens.filter((token) => token.type === states.capture) if (captureTokens.length <= 0) { const textToMatch = tokens.map((token) => token.text).join('') return exactStringMatcher(textToMatch) } const captureExpressionsInfo = captureTokens.map(captureExpressionInfoFromToken) const regexStr = tokens.map((token) => token.type === states.capture ? '(.*)' : escape(token.text)).join('') const regex = new RegExp('^' + regexStr + '$') return expressionMatcher(regex, captureExpressionsInfo) } /** * * @param {import('./key-ast.util.js').AST} ast - AST of parsed key * @returns {Matcher} matcher function */ export function getMatcher (ast) { return getMatcherFromTokens(ast.tokens) } /** * @typedef {object} CaptureExpressionInfo * @property { (text: string) => ParameterMatchResult} matchPredicate - match predicate */ /** * @typedef {object} CaptureExpressionsInfoDetail * @property {'expression' | 'regex' | 'string' | 'any'} type - detail type * @property {string} text - respective text * @property {import('./capture-expression-values.js').CaptureExpressionInfo} expressionInfo - capture expression information * @property {(text: string) => boolean} matches - predicate that verifies if `text` matches the defined `expressionInfo` */ /** * @typedef {(text: string, locale: Intl.Locale) => string} Formatter */ /** * @typedef {Readonly<{ isMatch: false } | {isMatch: true, expressionInfo: import('./capture-expression-values.js').CaptureExpressionInfo}>} ParameterMatchResult */ /** * @typedef {(text: string) => MatchResult} Matcher */ /** * @typedef {object} MatchResult * @property {boolean} isMatch - indicates if the text matches i18n key * @property {Readonly<string[]>} parameters - captured parameter text list * @property {Readonly<Formatter[]>} defaultFormatters - default formatters of parsed i18n key */ |