Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
feat(options): throw exception on undefined
Browse files Browse the repository at this point in the history
Added option to throw an exception when an undefined value is
triggered
  • Loading branch information
thetutlage committed Sep 7, 2017
1 parent 3841981 commit eea6048
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
29 changes: 25 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
![](https://img.shields.io/travis/poppinss/pope.svg)
[![Coverage Status](https://coveralls.io/repos/poppinss/pope/badge.svg?branch=master&service=github)](https://coveralls.io/github/poppinss/pope?branch=master)

Pope is a fast, minimal and micro templating engine for strings only, it plays well where you want to embed micro templates inside your module.
Pope is a fast, minimal and micro template engine for strings only, it plays well where you want to embed micro templates inside your module.

## Examples

Expand All @@ -29,19 +29,40 @@ pope('There are {{0}} emails in your inbox', [20])

## Options

You can also pass an options object, which takes only one param for now and that is `skipUndefined`.
You can also pass an options object to define the interpolation behavior.

If `skipUndefined` is set to true, all unfound variables will be untouched, whereas originally they get replaced with an empty string.
#### skipUndefined
Do not replace the undefined values with an empty string.

```javascript
const pope = require('pope')
pope('There are {{0}} emails in your inbox', {}, {
skipUndefined: true
})

// returns - There are {{0}} emails in your inbox
```

#### throwOnUndefined
Throw exception when a undefined value is found. `throwOnUndefined` gets priority over `skipUndefined` if both are defined.

```javascript
const pope = require('pope')
pope('Hello {{ username }}', {}, {
throwOnUndefined: true
})

// throws exception
```

```js
{
message: 'Missing value for {{ username }}',
key: 'username',
code: 'E_MISSING_KEY',
stack: '.....'
}
```



## The MIT License
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var prop = function(obj, path) {
* @public
*/
var pope = function (string, data, options) {
options = options || { skipUndefined: false }
options = options || { skipUndefined: false, throwOnUndefined: false }
var regex = /\{{2}([\w\$\s]+)\}{2}/gi
var result
var formattedString = string
Expand All @@ -50,6 +50,11 @@ var pope = function (string, data, options) {
var value = prop(data, item) || null
if (value) {
formattedString = formattedString.replace(result[0], value)
} else if (options.throwOnUndefined) {
const error = new Error('Missing value for ' + result[0])
error.key = item
error.code = 'E_MISSING_KEY'
throw error
} else if (!options.skipUndefined) {
formattedString = formattedString.replace(result[0], '')
}
Expand Down
22 changes: 22 additions & 0 deletions test/pope.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,26 @@ describe('pope', function () {
it('skip undefined', function () {
expect(pope("Hello {{ user_name }}", {}, { skipUndefined: true }).trim()).to.equal('Hello {{ user_name }}')
})

it('throw exception on undefined', function () {
try {
pope("Hello {{ user_name }}", {}, { throwOnUndefined: true })
expect(true).to.equal(false)
} catch (error) {
expect(error.message).to.equal('Missing value for {{ user_name }}')
expect(error.code).to.equal('E_MISSING_KEY')
expect(error.key).to.equal('user_name')
}
})

it('give priority to throwOnUndefined over skipUndefined', function () {
try {
pope("Hello {{ user_name }}", {}, { throwOnUndefined: true, skipUndefined: true })
expect(true).to.equal(false)
} catch (error) {
expect(error.message).to.equal('Missing value for {{ user_name }}')
expect(error.code).to.equal('E_MISSING_KEY')
expect(error.key).to.equal('user_name')
}
})
})

0 comments on commit eea6048

Please sign in to comment.