Created
February 27, 2019 19:35
-
-
Save jeffskelton3/a90b30835ed2e9e27823b32acd71a7cc to your computer and use it in GitHub Desktop.
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
const contents = ` | |
afder is bar and baz is {{biz}} {shouldnt find}{{but_this_should}} | |
{{blah}} | |
`; | |
const parseDatabaseFields = (input) => { | |
const pattern = '\{\{(.*?)\}\}'; | |
const regex = new RegExp(pattern, 'gm'); | |
let match; | |
const matches = []; | |
while ((match = regex.exec(input)) !== null) { | |
matches.push({ | |
name: match[0], | |
value: match[1], | |
startIndex: match.index, | |
endIndex: match.index + match[0].length, | |
}); | |
} | |
return matches; | |
}; | |
const selectionIsNextToDatabaseField = (selectionIndex, dbFields) => | |
dbFields | |
.map(dbField => dbField.endIndex) | |
.find(index => selectionIndex === (index + 1)) !== undefined; | |
console.log(parseDatabaseFields(contents)); | |
console.log(selectionIsNextToDatabaseField(33, parseDatabaseFields(contents))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment