1.0.1 • Published 3 years ago

data-replacer v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Data replacer

This module makes it easier to replace words in a string.

Usage

  • Create a new instance of DataReplacer class
    Options :
PARAMETERTYPEOPTIONALDEFAULTDESCRIPTION
caseInsensitivebooleantrueReplacement with case insensitive
requiredbooleanfalseThrow an error if string missing field
multipleReplacesbooleantrueMultiple words replacement

replace

Replace word(s) in a string.

Params

  • text : The text to replace. (string)
  • replace : The object with the string to be replaced in key and the new value in value. (Object)

Return

The text with the replaced values. (string)

const text = "Hello {{missingWord}}";
const replace = {
  "{{missingWord}}": "world !",
};
const result = Replacer.replace(text, replace);

console.log(result);

Example

const { DataReplacer } = require("data-replacer");

const Replacer = new DataReplacer({
  caseInsensitive: true,
  multipleReplaces: true,
  required: false,
});

const str = `
  Hi {{username}} ! 
  Welcome to the website {{domain}}
  You can access to your account with the login page :
  {{domain}}/login
  `;
const replace = {
  "{{username}}": "Smaug",
  "{{website}}": "https://exemple-website.com",
  "{{domain}}": "https://exemple-website.com",
};

const replacedStr = Replacer.replace(str, replace);
console.log(replacedStr);