Skip to content

Codex-Central/get-text-with-replacements

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Function to allow you to get a text with replacements from a text with placeholders

Topics

Resources

License

Stars

Watchers

Forks