New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

async-replace-es6

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-replace-es6

Light Weight string.replace working asynchronously

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Run replace on a string with an asynchronous function (promises).

Usage

async-replace-es6 has the same api as String.prototype.replace. Only the callback is an async function.

This is usefull if you need the matched part of a string before running an asynchronous operation.

import { replace } from 'async-replace-es6';
const { replace } = require('async-replace-es6');
const replaced = await replace(originalString, /someregex/g, asyncReplacer);

Or

replace(originalString, /someregex/, asyncReplacer)
    .then(replaced => console.log(replaced));

Example of asynchronous replacer :

async function asyncReplace(match, group1, group2, ...) {
    // here match is an url
    const response = await fetch(match);
    const json = await reponse.json();
    return json['title'];
}

function asyncReplacer(match, ...groups) {
    // match is a file path
    return new Promise((resolve, reject) => {
        fs.readFile(match, (err, data) => {
            if (err) {
                return reject(err);
            }
            resolve(data.toString());
        });
    });
}

If your promises need to be done Sequentially here is a solution:

const { replaceSeq } = require('async-replace-es6');

replaceSeq(originalString, /someregex/g, asyncReplacer)
    .then(replaced => console.log(replaced));

Keywords

FAQs

Package last updated on 30 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc