@eodomius/data-replacer
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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 :
PARAMETER TYPE OPTIONAL DEFAULT DESCRIPTION
caseInsensitive boolean true Replacement with case insensitive
required boolean false Throw an error if string missing field
multipleReplaces boolean true Multiple 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);

/@eodomius/data-replacer/

    Package Sidebar

    Install

    npm i @eodomius/data-replacer

    Weekly Downloads

    1

    Version

    1.0.0

    License

    Apache-2.0 License

    Unpacked Size

    6.84 kB

    Total Files

    10

    Last publish

    Collaborators

    • smaug-6739