diff --git a/scripts/verify/keyed-first-match.mjs b/scripts/verify/keyed-first-match.mjs index 6f11bc13c56..b70e9d7f3ab 100644 --- a/scripts/verify/keyed-first-match.mjs +++ b/scripts/verify/keyed-first-match.mjs @@ -13,14 +13,10 @@ const DESTRUCTURED = String.raw`\(\s*\{[^}]*?\b(${KEY})\b(?!\s*:)[^}]*\}\s*(?::[ const FIND = String.raw`\.find(?:Last)?\(\s*`; const FILTER = String.raw`\.filter\(\s*`; -const FIND_PATTERNS = [ - new RegExp(FIND + PLAIN, "gu"), - new RegExp(FIND + DESTRUCTURED, "gu"), -]; -const FILTER_PATTERNS = [ - new RegExp(FILTER + PLAIN, "gu"), - new RegExp(FILTER + DESTRUCTURED, "gu"), -]; +const PATTERNS = [PLAIN, DESTRUCTURED].map((predicate) => ({ + find: new RegExp(FIND + predicate, "gu"), + filter: new RegExp(FILTER + predicate, "gu"), +})); /** Index of the quote that closes the literal opening at `start`, or -1. */ const literalEnd = (content, start) => { @@ -72,20 +68,14 @@ const indexesFirstResult = (content, open) => { return content.startsWith("[0]", end); }; -const count = (content, pattern, accept) => - [...content.matchAll(pattern)].filter(accept).length; - /** Number of first-match lookups by key field in `content`. */ -export const countKeyedFirstMatches = (content) => - FIND_PATTERNS.reduce( - (total, pattern) => total + count(content, pattern, () => true), - 0, - ) + - FILTER_PATTERNS.reduce( - (total, pattern) => - total + - count(content, pattern, (match) => - indexesFirstResult(content, match.index + ".filter".length), - ), - 0, - ); +export const countKeyedFirstMatches = (content) => { + let total = 0; + for (const { find, filter } of PATTERNS) { + for (const _match of content.matchAll(find)) total += 1; + for (const match of content.matchAll(filter)) + if (indexesFirstResult(content, match.index + ".filter".length)) + total += 1; + } + return total; +};