Skip to content

Instantly share code, notes, and snippets.

@bitops
Last active September 7, 2019 21:38
Show Gist options
  • Save bitops/245d0b9229728337e4cbc34ab6ec9c22 to your computer and use it in GitHub Desktop.
Save bitops/245d0b9229728337e4cbc34ab6ec9c22 to your computer and use it in GitHub Desktop.
A javascript implementation of the `map-query-replace-regexp` command from Emacs, using Ramda
const mapQueryReplaceRegex = (regex, data, tokens) => {
const token = R.head(tokens)
const transform = R.replace(regex, token, data)
if(transform === data) {
return transform
} else {
const nextTokens = R.concat(R.tail(tokens), [token])
return mapQueryReplaceRegex(regex, transform, nextTokens)
}
}
const data = "foo bar foo baz foo wibble foo"
const desiredResult = "qux bar bux baz qux wibble bux";
const regex = "foo"
const transformationTokens = ["qux", "bux"]
const actualResult = mapQueryReplaceRegex(regex, data, transformationTokens);
actualResult == desiredResult // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment