1.0.0 • Published 2 years ago

@eodomius/data-replacer v1.0.0

Weekly downloads
-
License
Apache-2.0 Licens...
Repository
github
Last release
2 years ago

Eodomius utilities : Data replacer

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

Installation

NPM:

npm i @eodomius/data-replacer

Yarn:

yarn add @eodomius/data-replacer

Importation

CommonJS:

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

EcmaScript modules:

import { DataReplacer } from "@eodomius/data-replacer";

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);