https://github.com/peternaydenov/literal-toolbox
A collection of tag functions designed to transform string literals into dynamic render functions.
https://github.com/peternaydenov/literal-toolbox
Last synced: about 1 month ago
JSON representation
A collection of tag functions designed to transform string literals into dynamic render functions.
- Host: GitHub
- URL: https://github.com/peternaydenov/literal-toolbox
- Owner: PeterNaydenov
- License: mit
- Created: 2024-11-20T14:00:09.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-04-22T07:45:02.000Z (about 2 months ago)
- Last Synced: 2025-05-07T06:05:26.058Z (about 1 month ago)
- Language: JavaScript
- Size: 191 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# literal-toolbox (@peter.naydenov/literal-toolbox)




A collection of tag functions designed to transform string literals into dynamic render functions.
## Installation
```bash
npm install @peter.naydenov/literal-toolbox
```## Usage
Use function `obj` when you will name your placeholders and will provide data as an object.
```js
import literal from '@peter.naydenov/literal-toolbox'const templateFn = literal.obj`${'greet'} darling ${'name'}, my age is ${'age'}. Great!`
// NOTE: Take a look that the placeholder names are wrapped in quotes!const htmlSnippet = templateFn({
greet: 'Hello',
name: 'John',
age: 42
})
```Use function `arr` when you will number your placeholders and will provide data as an array. Example:
```js
const templateFn = literal.arr`${0} darling ${1}, my age is ${2}. Great!`
const htmlSnippet = templateFn(['Hello', 'John', 42])
```Use function 'arg' when you will number your placeholders and will provide data as separate arguments. Example:
```js
const templateFn = literal.arg`${0} darling ${1}, my age is ${2}. Great!`
const htmlSnippet = templateFn ( 'Hello', 'John', 42 )```
### Dynamic Placeholder Evaluation
In version 1.1.0 and onwards, the `obj`, `arr`, and `arg` functions support functions as arguments. Functions can dynamically evaluate application state or other data to produce a string result. Example:
```js
let loginState = false; // external state
function loginFn() {
return loginState ? '' : 'You need to login first';
}const fn = literal.obj`Change profile name. ${'login'}`;
let res = fn({ login: loginFn });
// res == 'Change profile name. You need to login first'loginState = true;
res = fn({ login: loginFn }) // Use exactly the same request
// res == 'Change profile name.'
```## Credits
'@peter.naydenov/literal-toolbox' was created and supported by Peter Naydenov.## License
'@peter.naydenov/literal-toolbox' is released under the MIT License.