https://github.com/codex-central/get-text-with-replacements
Function to allow you to get a text with replacements from a text with placeholders
https://github.com/codex-central/get-text-with-replacements
javascript nodejs npm replace-text text typescript util
Last synced: 12 months ago
JSON representation
Function to allow you to get a text with replacements from a text with placeholders
- Host: GitHub
- URL: https://github.com/codex-central/get-text-with-replacements
- Owner: Codex-Central
- License: apache-2.0
- Created: 2023-12-21T14:50:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-21T22:25:20.000Z (over 2 years ago)
- Last Synced: 2024-10-30T03:01:00.873Z (over 1 year ago)
- Topics: javascript, nodejs, npm, replace-text, text, typescript, util
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@codexcentral/get-text-with-replacements
- Size: 26.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```javascript
import { getText } from '@codexcentral/get-text-with-replacements';
```
### 2. Create the data object
Define between `{ }` the keywords that will be replaced
```javascript
const data = {
"hello_world": "Hello World {name}!"
};
```
### 3. Call the function
```javascript
const text = getText({
data,
key: "hello_world",
replacements: { name: "Roberto" },
notFoundText: "not_found"
});
```
#### Result
```javascript
console.log(text);
// Hello World Roberto!
```
### Attributes
| Attribute | Type | Mandatory |
| ------ | ------ | ------ |
| data | `object` | true |
| key | `string` | true |
| replacements | `object` | false |
| notFoundText | `string` | false (default: ``) |
#### Example of attributes
```json
{
"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
```javascript
import { getText, TGetText } from '@codexcentral/get-text-with-replacements';
```
### 2. Create the Helper function
```javascript
const getTextReplaced = >({
data,
key,
replacements,
}: TGetText) => {
return getText({
data,
key,
replacements,
});
};
```
# Credits
These code was written by [Roberto Silva Z.](https://www.linkedin.com/in/robertosilvazuniga/)