1.3.0 • Published 5 months ago

@codexcentral/get-text-with-replacements v1.3.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

get-text-with-replacements

This library aims to retrieve a specific text message from a data object and replace placeholders within the text with provided values.

It allows for dynamic text generation by substituting placeholders in the original text with corresponding replacement values, providing flexibility in generating customized messages or content.

Installation

npm install @codexcentral/get-text-with-replacements

Usage

1. Import the function

import { getText } from '@codexcentral/get-text-with-replacements';

2. Create the data object

Define between { } the keywords that will be replaced

const data = {
  "hello_world": "Hello World {name}!"
};

3. Call the function

const text = getText({
  data,
  key: "hello_world",
  replacements: { name: "Roberto" },
  notFoundText: "not_found"
});

Result

console.log(text); 
// Hello World Roberto!

Attributes

AttributeTypeMandatory
dataobjecttrue
keystringtrue
replacementsobjectfalse
notFoundTextstringfalse (default: <key__not_found>)

Example of attributes

{
  "data": {
    "hello_world": "Hello World {name}!"
  },
  "key": "hello_world",
  "replacements": { "name": "Roberto" },
  "notFoundText": "not_found"
}

Wrapper function as a Helper (optional)

1. Import the function

import { getText, TGetText } from '@codexcentral/get-text-with-replacements';

2. Create the Helper function

const getTextReplaced = <T extends Record<string, any>>({
  data,
  key,
  replacements,
}: TGetText<T>) => {
  return getText({
    data,
    key,
    replacements,
  });
};

Credits

These code was written by Roberto Silva Z.

1.3.0

5 months ago

1.2.0

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago