https://github.com/dawsbot/duplival
JSON + variables === <3 in only 292 bytes
https://github.com/dawsbot/duplival
Last synced: 2 months ago
JSON representation
JSON + variables === <3 in only 292 bytes
- Host: GitHub
- URL: https://github.com/dawsbot/duplival
- Owner: dawsbot
- License: mit
- Created: 2016-03-09T05:32:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-07-01T04:41:59.000Z (almost 10 years ago)
- Last Synced: 2025-03-12T06:45:27.656Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Contributing: .github/contributing.md
- License: license
Awesome Lists containing this project
README
# duplival
[](https://www.npmjs.com/package/duplival)
[](https://travis-ci.org/dawsonbotsford/duplival)
[](http://npmjs.org/duplival)
[](https://github.com/sindresorhus/xo)
> JSON + variables === <3 in only 292 bytes
## Usage
```js
const duplival = require('duplival');
duplival({
key1: 'value1',
key2: '<%= key1 %>'
});
/*
{
key1: 'value1',
key2: 'value1'
};
*/
```
## Install
```
npm install --save duplival
```
## Advanced Example
```js
const duplival = require('duplival');
duplival({
key1: 1,
key2: '<%= key1 %>',
key3: '<%= key1 + 2 %>'
});
/*
{
key1: 1,
key2: '1',
key3: '3'
};
*/
const piglatin = require('piglatin');
const piglatinTemplate = {
translate: piglatin,
englishSentence: 'This is a very very long string you would not want to duplicate or type out. Yet you do want to do some type of operation with it. Let\'s say for example that you want to translate this. With english as your base and pig-latin as your goal, let\'s have some fun',
piglatinSentence: `<%= translate(englishSentence) %>`
};
duplival(piglatinTemplate).piglatinSentence;
/*
'histay is a eryvay eryvay onglay tringsay ouyay ouldway otnay antway to uplicateday or ypetay utoay etyay ouyay do antway to do omesay ypetay of perationoay ithway it etlay s aysay orfay xampleeay hattay ouyay antway to ranslatetay histay ithway nglisheay as ouryay asebay ndaay igpay atinlay as ouryay oalgay etlay s avehay omesay unfay';
*/
```
#### How did that just happen?
`duplival` is powered by mighty [ejs templating](http://ejs.co/). Although originally intended for html, ejs can easily be hijacked into being our engine. It means we can now use variables, conditionals, and even loops inside of JSON objects.
## API
### duplival(target)
##### target
Type: `object`
Powered by [ejs](http://ejs.co/)'s powerful templating, this **tiny module** can help reduce code duplication create smaller JSON objects in your code.
## License
MIT © [Dawson Botsford](http://dawsonbotsford.com)
---
If you like this, star it. If you want to follow me, follow me.