dynamic-strings
v1.0.2
Published
A lightweight package for making static data structures dynamic.
Downloads
11
Maintainers
Readme
dynamic-strings
This is a package dedicated to having an organized data structure that is consisted of static strings, meant to be dynamic.
Installation
Use the package manager NPM or yarn to install dynamic-strings.
NPM
npm install dynamic-strings --save
Yarn
yarn add dynamic-strings
Usage
Importing package
Javascript
require("dynamic-strings");
Features
- No dependencies
- Lightweight
- Minimal vulnerabilities
- Only variable property support
- Supports "dot" indexing as well as bracket indexing
Dynamifying Strings
require("dynamic-strings");
let site = {
name: "example.com"
};
let user = {
username: "User"
};
let welcomeMessage = "Hello, {{user.username}}! Welcome to {{site.name}}!";
console.log(welcomeMessage.dfy({ user: user, site: site }));
Outputs to the console
'Hello, User! Welcome to example.com!'
Dynamifying Objects
require("dynamic-strings");
let company = {
employees: [
{
firstName: "John",
lastName: "Doe",
message: "Welcome, you have a message: {{notes['John Doe']}}"
},
{
firstName: "Anna",
lastName: "Smith",
message: "Welcome, you have a message: {{notes['Anna Smith']}}"
},
{
firstName: "Peter",
lastName: "Jones",
message: "Welcome, you have a message: {{notes['Peter Jones']}}"
}
]
};
console.log(company.dfy({
notes: {
"John Doe": "Finish frontend.",
"Anna Smith": "Come to my office.",
"Peter Jones": "Organize the interview.",
}
}));
Outputs to the console
{
employees: [
{
firstName: 'John',
lastName: 'Doe',
message: 'Welcome, you have a message: Finish frontend.'
},
{
firstName: 'Anna',
lastName: 'Smith',
message: 'Welcome, you have a message: Come to my office.'
},
{
firstName: 'Peter',
lastName: 'Jones',
message: 'Welcome, you have a message: Organize the interview.'
}
]
}