generated from mmisty/cypress-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[patch] fix pseudo regexp cases (#24)
- Loading branch information
Showing
3 changed files
with
112 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,83 @@ | ||
// when experession !(<expression>) | ||
const transformReversed = (isInversed: boolean, s: string) => { | ||
s = isInversed ? s.slice(2) : s; | ||
s = isInversed ? s.slice(0, s.length - 2) : s; | ||
type Replacement = { mapName: string; exp: string; inverse: boolean }; | ||
|
||
return s; | ||
}; | ||
/** | ||
* replace all parenthesis groups with placeholder | ||
* @param input | ||
* @param replacements | ||
* @param num | ||
*/ | ||
const replaceParenthesisGroups = (input: string, replacements: Replacement[], num = 1): string => { | ||
let replaced = input; | ||
const groupsNeg = input.match(/!\(([^()]*)\)/); | ||
const groups = input.match(/\(([^()]*)\)/); | ||
|
||
export const selectionTestGrep = (str: string): RegExp => { | ||
if (str.startsWith('=')) { | ||
// expressions like '=/hello$/i' | ||
const beg = str.slice(1); | ||
const endOfExpr = beg.lastIndexOf('/'); | ||
const flags = endOfExpr < beg.length - 1 ? beg.slice(endOfExpr + 1) : ''; | ||
const expr = beg.slice(beg.indexOf('/') + 1, endOfExpr); | ||
if (!groupsNeg && !groups) { | ||
return replaced; | ||
} | ||
|
||
return new RegExp(expr, flags); | ||
const replaceExpression = (expression: string, group: string, inverse: boolean) => { | ||
const mapName = `##R${num}##`; | ||
replacements.push({ mapName, exp: group, inverse }); | ||
replaced = replaced.replace(expression, mapName); | ||
|
||
return replaceParenthesisGroups(replaced, replacements, num + 1); | ||
}; | ||
|
||
if (groupsNeg) { | ||
return replaceExpression(groupsNeg[0], groupsNeg[1], true); | ||
} else if (groups) { | ||
return replaceExpression(groups[0], groups[1], false); | ||
} | ||
|
||
const isInverse = str.startsWith('!('); | ||
const transformed = transformReversed(isInverse, str); | ||
return replaced; | ||
}; | ||
|
||
const reg = transformed | ||
/** | ||
* no parenthesis in the group | ||
* @param exp | ||
* @param inverse | ||
*/ | ||
const convertOneGroup = (exp: string, inverse: boolean): string => { | ||
const reg = exp | ||
.split('/') | ||
.map(t => (t.startsWith('!') ? t.replace(/^!(.*)/, '^(?!.*$1.*)') : t)) | ||
.map(t => | ||
t.indexOf('&') !== -1 | ||
? `${t | ||
.split('&') | ||
.map(nd => (nd.startsWith('!') ? nd.replace(/^!(.*)/, '^(?!.*$1)') : nd.replace(/^(.*)/, '(?=.*$1)'))) | ||
.map(nd => (nd.startsWith('!') ? nd.replace(/^!(.*)/, '^(?!.*$1.*)') : nd.replace(/^(.*)/, '(?=.*$1)'))) | ||
.join('+')}+` | ||
: t, | ||
) | ||
.join('|'); | ||
|
||
return new RegExp(isInverse ? `^(?!.*${reg}).*` : `${reg}.*`, 'i'); | ||
const res = reg.indexOf('|') !== -1 ? `(${reg})` : reg; | ||
|
||
return inverse ? `^(?!.*${res}.*)` : `${res}`; | ||
}; | ||
|
||
export const selectionTestGrep = (str: string): RegExp => { | ||
if (str.startsWith('=')) { | ||
// expressions like '=/hello$/i' | ||
const beg = str.slice(1); | ||
const endOfExpr = beg.lastIndexOf('/'); | ||
const flags = endOfExpr < beg.length - 1 ? beg.slice(endOfExpr + 1) : ''; | ||
const expr = beg.slice(beg.indexOf('/') + 1, endOfExpr); | ||
|
||
return new RegExp(expr, flags); | ||
} | ||
|
||
const replacements: Replacement[] = []; | ||
const replacedString = replaceParenthesisGroups(str, replacements); | ||
let convertedString = convertOneGroup(replacedString, false); | ||
const groups = replacements.map(t => ({ ...t, reg: convertOneGroup(t.exp, t.inverse) })); | ||
|
||
// last group should be converted first | ||
groups | ||
.sort((a, b) => (a.mapName > b.mapName ? -1 : 1)) | ||
.forEach(r => { | ||
convertedString = convertedString.replace(r.mapName, r.reg); | ||
}); | ||
|
||
return new RegExp(`${convertedString}.*`, 'i'); | ||
}; |
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