Last active
September 7, 2019 21:38
-
-
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
This file contains hidden or 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 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