@codexcentral/get-text-with-replacements
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

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

Attribute Type Mandatory
data object true
key string true
replacements object false
notFoundText string false (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.

Package Sidebar

Install

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

Weekly Downloads

1

Version

1.3.0

License

Apache-2.0

Unpacked Size

21.3 kB

Total Files

15

Last publish

Collaborators

  • roberto.silva