Skip to content

Oleg-Imanilov/str-literals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

str-literals

Dynamic string template with simple expressions support.

The idea to use format similar to JS template literals, but be able to get it dynamically.

You can use all global functions and objects unless you don't using "{" and/or "}" inside "${}".

Be carreful when using this module, expression evaluation using eval function.

npm install str-literals

Examples

Simple variable:

const template = require('str-literals')
// const who = 'World'
// console.log( `Hello, ${who}!`)
console.log( template('Hello, ${who}!', {who: 'World'}))

Output:

Hello, World!

No variables:

const template = require('str-literals')
console.log( template('Total: ${1+1}') )

Output:

Total: 2

JS Array expression

const template = require('str-literals')
console.log( template('Note: ${arr.join("")}', {arr: ['Hello, ', 'World', '!']}))

Output:

Note: Hello, World!

Numeric expression

const template = require('str-literals')
console.log( template('Total : ${10 + a - b}', {a: 10, b: 5}))

Output:

Total : 15

License

MIT © Oleg Imanilov

About

Dynamic string template with nested support

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published