Skip to content

Instantly share code, notes, and snippets.

@jeffskelton3
Created February 27, 2019 19:35
Show Gist options
  • Save jeffskelton3/a90b30835ed2e9e27823b32acd71a7cc to your computer and use it in GitHub Desktop.
Save jeffskelton3/a90b30835ed2e9e27823b32acd71a7cc to your computer and use it in GitHub Desktop.
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