-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Precompile regexes Signed-off-by: Emanuele Feliziani <[email protected]> * Commit compiled files Signed-off-by: Emanuele Feliziani <[email protected]> * Mark generated file as such in git Signed-off-by: Emanuele Feliziani <[email protected]> * Fix unintended changes Signed-off-by: Emanuele Feliziani <[email protected]> * Fix spacing Signed-off-by: Emanuele Feliziani <[email protected]> * Remove safeRegex Signed-off-by: Emanuele Feliziani <[email protected]> * Remove compiled matching config from gitattributes Signed-off-by: Emanuele Feliziani <[email protected]> --------- Signed-off-by: Emanuele Feliziani <[email protected]>
- Loading branch information
1 parent
f3eccad
commit 4a0fd69
Showing
18 changed files
with
1,428 additions
and
2,764 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const {matchingConfiguration} = require('../src/Form/matching-config/matching-config-source.js') | ||
const {writeFileSync} = require('fs') | ||
const {join} = require('path') | ||
const {inspect} = require('util') | ||
|
||
/** | ||
* DDGRegexes are stored as strings so we can annotate them with comments, here we transform them into RegExp | ||
*/ | ||
|
||
/** | ||
* Loop through Object.entries and transform all values to RegExp | ||
* @param {Object} obj | ||
*/ | ||
function convertAllValuesToRegex (obj) { | ||
for (const [key, value] of Object.entries(obj)) { | ||
const source = String(value).normalize('NFKC') | ||
obj[key] = new RegExp(source, 'ui') | ||
} | ||
return obj | ||
} | ||
for (const [key, value] of Object.entries(matchingConfiguration.strategies.ddgMatcher.matchers)) { | ||
matchingConfiguration.strategies.ddgMatcher.matchers[key] = convertAllValuesToRegex(value) | ||
} | ||
|
||
/** | ||
* Prepare CSS rules by concatenating arrays and removing whitespace | ||
*/ | ||
Object.entries(matchingConfiguration.strategies.cssSelector.selectors).forEach(([name, selector]) => { | ||
if (Array.isArray(selector)) { | ||
selector = selector.join(',') | ||
} | ||
matchingConfiguration.strategies.cssSelector.selectors[name] = selector.replace(/\n/g, ' ').replace(/\s{2,}/g, ' ').trim() | ||
}) | ||
|
||
/** | ||
* VendorRules come from different providers, here we merge them all together in one RegEx per inputType | ||
*/ | ||
|
||
/** | ||
* Merge our vendor rules into a single RegEx | ||
* @param {keyof VendorRegexRules} ruleName | ||
* @param {VendorRegexConfiguration["ruleSets"]} ruleSets | ||
* @return {{RULES: Record<keyof VendorRegexRules, RegExp | undefined>}} | ||
*/ | ||
function mergeVendorRules (ruleName, ruleSets) { | ||
let rules = [] | ||
ruleSets.forEach(set => { | ||
if (set[ruleName]) { | ||
rules.push(`(${set[ruleName]?.toLowerCase()})`.normalize('NFKC')) | ||
} | ||
}) | ||
return new RegExp(rules.join('|'), 'iu') | ||
} | ||
const ruleSets = matchingConfiguration.strategies.vendorRegex.ruleSets | ||
for (const ruleName of Object.keys(matchingConfiguration.strategies.vendorRegex.rules)) { | ||
matchingConfiguration.strategies.vendorRegex.rules[ruleName] = mergeVendorRules(ruleName, ruleSets) | ||
} | ||
|
||
/** | ||
* Build the file contents | ||
*/ | ||
const fileContents = [ | ||
`/* DO NOT EDIT, this file was generated by scripts/precompile-regexes.js */\n\n`, | ||
`/** @type {MatchingConfiguration} */\n`, | ||
'const matchingConfiguration = ', | ||
inspect(matchingConfiguration, {maxArrayLength: Infinity, depth: Infinity, maxStringLength: Infinity}), | ||
'\n\nexport { matchingConfiguration }\n' | ||
].join('') | ||
|
||
// Write to file | ||
const outputPath = join(__dirname, '../src/Form/matching-config/__generated__', '/compiled-matching-config.js') | ||
writeFileSync(outputPath, fileContents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.