Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 10, 2017
0 parents commit d1326b9
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
@@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: node_js
node_js:
- '6'
- '4'
22 changes: 22 additions & 0 deletions index.js
@@ -0,0 +1,22 @@
'use strict';
module.exports = (tpl, data) => {
if (typeof tpl !== 'string') {
throw new TypeError(`Expected a string in the first argument, got ${typeof tpl}`);
}

if (typeof data !== 'object') {
throw new TypeError(`Expected an Object/Array in the second argument, got ${typeof data}`);
}

const re = /{(.*?)}/g;

return tpl.replace(re, (_, key) => {
let ret = data;

for (const prop of key.split('.')) {
ret = ret ? ret[prop] : '';
}

return ret || '';
});
};
21 changes: 21 additions & 0 deletions license
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
49 changes: 49 additions & 0 deletions package.json
@@ -0,0 +1,49 @@
{
"name": "pupa",
"version": "0.0.0",
"description": "Simple micro templating",
"license": "MIT",
"repository": "sindresorhus/pupa",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"string",
"formatting",
"template",
"object",
"format",
"interpolate",
"interpolation",
"templating",
"str",
"obj",
"tpl",
"expand",
"simple",
"replace",
"placeholders",
"values",
"fmt",
"transform",
"micro"
],
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
}
59 changes: 59 additions & 0 deletions readme.md
@@ -0,0 +1,59 @@
# pupa [![Build Status](https://travis-ci.org/sindresorhus/pupa.svg?branch=master)](https://travis-ci.org/sindresorhus/pupa)

> Simple micro templating
Useful when all you need is to fill in some placeholders.


## Install

```
$ npm install --save pupa
```


## Usage

```js
const pupa = require('pupa');

pupa('The mobile number of {name} is {phone.mobile}', {
name: 'Sindre',
phone: {
mobile: '609 24 363'
}
});
//=> 'The mobile number of Sindre is 609 24 363'

pupa('I like {0} and {1}', ['🦄', '🐮']);
//=> 'I like 🦄 and 🐮'
```


## API

### pupa(template, data)

#### template

Type: `string`

Text with placeholders for `data` properties.

#### data

Type: `Object` `Array`

Data to interpolate into `template`.


## FAQ

### What about template literals?

Template literals expand on creation. This module expands the template on execution, which can be useful if either or both template and data are lazily created or user-supplied.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
22 changes: 22 additions & 0 deletions test.js
@@ -0,0 +1,22 @@
import test from 'ava';
import m from './';

test(t => {
t.is(m('{foo}', {foo: '!'}), '!');
t.is(m('{foo}', {foo: 10}), '10');
t.is(m('{foo}{foo}', {foo: '!'}), '!!');
t.is(m('{foo}{bar}{foo}', {foo: '!', bar: '#'}), '!#!');
t.is(m('yo {foo} lol {bar} sup', {foo: '🦄', bar: '🌈'}), 'yo 🦄 lol 🌈 sup');

t.is(m('{foo}{deeply.nested.value}', {
foo: '!',
deeply: {
nested: {
value: '#'
}
}
}), '!#');

t.is(m('{0}{1}', ['!', '#']), '!#');
});

0 comments on commit d1326b9

Please sign in to comment.