Skip to content

Instantly share code, notes, and snippets.

@ugate
Created December 31, 2018 22:19
Show Gist options
  • Select an option

  • Save ugate/8b749eb8cbe938219c4b4a0989ea8348 to your computer and use it in GitHub Desktop.

Select an option

Save ugate/8b749eb8cbe938219c4b4a0989ea8348 to your computer and use it in GitHub Desktop.

Revisions

  1. ugate created this gist Dec 31, 2018.
    28 changes: 28 additions & 0 deletions asyncReplace.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /**
    * `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;
    }