@xogumon/async-replace v1.0.5
async-replace
Asynchronous string replace function for Node.js and browsers (with support for Promise).
Summary
This module provides a function that is similar to String.prototype.replace but works asynchronously.
Motivation
I was working on a project that needed to replace some strings in a text file asynchronously and I didn't find any package that could do this without needing to call the function for each replacement (different results) on the same string, so I decided to create this package.
API - asyncReplace(string, replacers, options)
string (required)
Type:
stringThe string to replace.
replacers (required)
Type:
Array|ObjectThe
replacersargument can be an array of objects or a single object.
replacers.search (required)
Type:
stringorRegExpThe string or regular expression to search for.
replacers.replace (required)
Type:
anyThe value to replace the
replacers.searchwith.
replacers.flags (optional)
Type:
stringThe flags to use with the
replacers.searchstring (not used ifreplacers.searchis aRegExp).The flags are the same as the flags used in the
RegExpconstructor. See MDN for more information.
options (optional)
- Type:
Object
options.flags (optional)
Type:
stringThe flags to use with the
replacers.searchstring (not used ifreplacers.searchis aRegExp).If
replacersobject has areplacers.flagsproperty, theoptions.flagsare ignored for that object and thereplacers.flagsare used instead.
options.debug (optional)
Type:
booleanIf
true, theasyncReplacefunction will log debug messages to the console. Defaults tofalse.
Installation
npm install @xogumon/async-replace --save # or yarn add @xogumon/async-replaceUsage (Node.js)
const asyncReplace = require("@xogumon/async-replace");
asyncReplace("A string with some words to replace", [
{
search: "string",
replace: new Promise((resolve) => setTimeout(() => resolve("text"), 1000)),
},
{
search: "words",
replace: new Promise((resolve) => setTimeout(() => resolve("letters"), 1000)),
},
]).then((result) => {
console.log(result); // A text with some letters to replace
});You can run a test with npm test or yarn test. The test will run with Jest.
Usage (Browser)
<script src="https://unpkg.com/@xogumon/async-replace/index.min.js"></script>
<script>
asyncReplace("A {string} with a {promise} in it.", [
{
search: "{promise}",
replace: async () => {
return await Promise.resolve("promise");
},
},
{
search: "{string}",
replace: "string",
},
{
search: "A",
replace: () => {
return "This is a";
},
},
{
search: "it.",
replace: Promise.resolve("this."),
},
]).then((result) => {
console.log(result); // This is a string with a promise in this.
});
</script>You can find a working example in JSFiddle.
You can use the asyncReplace function through unpkg or jsDelivr CDN:
<script src="https://unpkg.com/@xogumon/async-replace/index.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@xogumon/async-replace/index.min.js"></script>asyncReplace is attached to the window object when used in the browser (e.g. window.asyncReplace).
License
Support
If you like this project, please consider supporting me on GitHub Sponsors.