Forger is an elegant TypeScript library designed to effortlessly craft dynamic strings by seamlessly replacing placeholders within a template with values from a data object. It offers a streamlined and versatile solution for generating text with finesse.
You can integrate Forger into your project with ease using npm or yarn:
npm install @devdesignersid/forger
# or
yarn add @devdesignersid/forger
Unlock the power of Forger in your TypeScript/JavaScript project with this simple example:
import Forger from '@devdesignersid/forger';
// Create a template string with placeholders
const emailTemplate = 'Hello, {{user.name}}! Your email is {{user.email}}.';
// Instantiate a Forger instance with the template
const emailForger = new Forger(template);
// Define a data object with values to replace the placeholders
const data = {
user: {
name: 'John Doe',
email: 'john@example.com',
},
};
// Forge the string by replacing placeholders with data values
const result = emailForger.forge(data);
console.log(result);
// Output: "Hello, John Doe! Your email is john@example.com."
You have the option to escape placeholders by using double backslashes (\\
) before the opening and closing braces. For example:
const template = 'This is an escaped placeholder: \\{{escaped\\}}';
Forger is vigilant about placeholders. If a placeholder in the template doesn't have a corresponding value in the data object, it will gracefully raise an error. Ensure your data object contains all the necessary values to maintain a smooth runtime.
Creates a new Forger instance with the specified template.
template
(string): The template string containing placeholders.
Generates a new string by replacing placeholders in the template with values from the data object.
data
(T): The data object containing values to replace placeholders.- Returns: A string with placeholders replaced by data values.
This library operates under the MIT License. Delve into the LICENSE file for detailed information.
Should you encounter any issues or have valuable feedback to offer, kindly create an issue on our GitHub repository.