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.
npm install @codexcentral/get-text-with-replacements
import { getText } from '@codexcentral/get-text-with-replacements';
Define between { }
the keywords that will be replaced
const data = {
"hello_world": "Hello World {name}!"
};
const text = getText({
data,
key: "hello_world",
replacements: { name: "Roberto" },
notFoundText: "not_found"
});
console.log(text);
// Hello World Roberto!
Attribute | Type | Mandatory |
---|---|---|
data | object |
true |
key | string |
true |
replacements | object |
false |
notFoundText | string |
false (default: <key__not_found> ) |
{
"data": {
"hello_world": "Hello World {name}!"
},
"key": "hello_world",
"replacements": { "name": "Roberto" },
"notFoundText": "not_found"
}
import { getText, TGetText } from '@codexcentral/get-text-with-replacements';
const getTextReplaced = <T extends Record<string, any>>({
data,
key,
replacements,
}: TGetText<T>) => {
return getText({
data,
key,
replacements,
});
};
These code was written by Roberto Silva Z.