-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add helper class and write test for regex creator
- Loading branch information
1 parent
7e9fcce
commit 988b5f5
Showing
3 changed files
with
76 additions
and
47 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
export function createRegexForWords(wordList) { | ||
|
||
// This function builds a case-insensitive regex pattern for a given word. | ||
|
||
|
||
const part1List = []; | ||
const part2List = []; | ||
|
||
wordList.forEach(word => { | ||
const part1 = createRegexToCheckIfWordLookAlike(word); | ||
const part2 = createFallbackRegex(word); | ||
part1List.push(part1); | ||
part2List.push(part2); | ||
}); | ||
|
||
return `(${part1List.join('|')})|(${part2List.join('')})`; | ||
} | ||
|
||
function getCaseInsensitiveRegexForChar(c){ | ||
return `[${c.toUpperCase()}${c.toLowerCase()}]` | ||
} | ||
|
||
function createCaseInsensitiveRegexForWord(word) { | ||
return word | ||
.match(/[A-Z][a-z]+/g) | ||
.join(".?") | ||
.split("") | ||
.map(c => /[a-zA-Z]/g.test(c) ?getCaseInsensitiveRegexForChar(c) : c) | ||
.join(''); | ||
} | ||
|
||
function transformToUnderscoreUppercase(word) { | ||
return word | ||
.split(/(?=[A-Z])/) // Split at uppercase letters without removing them | ||
.join('_') | ||
.toUpperCase(); | ||
} | ||
|
||
function getWordRegexWithOptionalLetters(word) { | ||
return word.replace(/(\[[^\[\]]+\])(?![.?])/g, "$1?"); // Puts a "?" between the case-insensitive braces if there is no "?" or "." already | ||
} | ||
|
||
function createRegexToCheckIfWordLookAlike(word) { | ||
let wordLooksLikeRegex = createCaseInsensitiveRegexForWord(word); | ||
wordLooksLikeRegex = getWordRegexWithOptionalLetters(wordLooksLikeRegex) | ||
const wordCorrectRegex = getCaseInsensitiveRegexForChar(word[0]) + word.slice(1); | ||
const wordInUpperCase = transformToUnderscoreUppercase(word) | ||
return `(?=.*${wordLooksLikeRegex}.*)(.*${wordCorrectRegex}.*|[A-Z_]*${wordInUpperCase}[A-Z_]*)`; | ||
} | ||
|
||
function createFallbackRegex(word) { | ||
const caseInsensitiveWordRegex = createCaseInsensitiveRegexForWord(word); | ||
return `(?=^(?!.*${caseInsensitiveWordRegex}).*)`; | ||
} | ||
|
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,18 @@ | ||
import * as eslintHelper from "./eslintHelper.mjs" | ||
|
||
describe("eslintHelper", () => { | ||
const combinedRegex:string = "((?=.*[Kk]?[Ee]?[Yy].?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*)|(?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk].?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*))|((?=^(?!.*[Kk][Ee][Yy].?[Rr][Ee][Ss][Uu][Ll][Tt]).*)(?=^(?!.*[Cc][Hh][Ee][Cc][Kk].?[Ii][Nn]).*))" | ||
const keyResultRegex:string = "((?=.*[Kk]?[Ee]?[Yy]?[Rr]?[Ee]?[Ss]?[Uu]?[Ll]?[Tt]?.*)(.*[Kk]eyResult.*|[A-Z_]*KEY_RESULT[A-Z_]*))|((?=^(?!.*[Kk][Ee][Yy][Rr][Ee][Ss][Uu][Ll][Tt]).*))" | ||
const CheckInRegex:string = "((?=.*[Cc]?[Hh]?[Ee]?[Cc]?[Kk]?[Ii]?[Nn]?.*)(.*[Cc]heckIn.*|[A-Z_]*CHECK_IN[A-Z_]*))|((?=^(?!.*[Cc][Hh][Ee][Cc][Kk][Ii][Nn]).*))" | ||
|
||
|
||
it.each([ | ||
// [["KeyResult"], /KeyResult/], | ||
// [["CheckIn"], /CheckIn/], | ||
[["KeyResult", "CheckIn"], /KeyResult|CheckIn/], | ||
])("should return regex %p", (wordToRegex) => { | ||
expect(eslintHelper.createRegexForWords(wordToRegex)).toEqual(combinedRegex); | ||
|
||
}); | ||
}) | ||
|