Skip to content

Instantly share code, notes, and snippets.

@ugate
Created December 31, 2018 22:19
Show Gist options
  • Save ugate/8b749eb8cbe938219c4b4a0989ea8348 to your computer and use it in GitHub Desktop.
Save ugate/8b749eb8cbe938219c4b4a0989ea8348 to your computer and use it in GitHub Desktop.
/**
* `String.prototype.replace` alternative that supports `async` _replacer_ functions
* @private
* @ignore
* @param {String} str The string to perform a replace on
* @param {RegExp} regex The regular expression that the replace will match
* @param {Function} replacer An `async` function that operates the same way as the function passed into `String.prototype.replace`
*/
async function replace(str, regex, replacer) {
const mtchs = [];
str.replace(regex, function asyncReplacer(match) {
mtchs.push({ args: arguments, thiz: this });
return match;
});
var offset = 0, beg, end, rtn;
for (let mtch of mtchs) {
rtn = replacer.apply(mtch.thiz, mtch.args);
if (rtn instanceof Promise) rtn = await rtn;
if (rtn !== mtch.args[0]) {
if (!rtn) rtn = String(rtn); // same as async version
beg = mtch.args[mtch.args.length - 2] + offset;
end = beg + mtch.args[0].length;
str = str.substring(0, beg) + rtn + str.substring(end);
offset += rtn.length - mtch.args[0].length;
}
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment