Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 8, 2017
2 parents 41c4b30 + c34703c commit 593524a
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ npm-debug.log
.idea
dump.rdb
test/unit/storage/sessions
.nyc_output
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dump.rdb
benchmarks
.idea
bin
nyc_output
1 change: 0 additions & 1 deletion .nyc_output/151789ba697039d2f44ad8b2ae510c81.json

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/547cbf2c36d15fe5bf8134cbd816355f.json

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/a34c46f66b4e93c04da78b067144e055.json

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/a6b9bcacadf6ffa17128ec74456f5912.json

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/d6eaa94bd0706c293bbf9aac2f756d77.json

This file was deleted.

15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<a name="1.0.2"></a>
## [1.0.2](https://github.com/poppinss/edge/compare/v1.0.1...v1.0.2) (2017-08-08)


### Bug Fixes

* **context:** bind context to global functions ([e0a2685](https://github.com/poppinss/edge/commit/e0a2685))


### Features

* **globals:** add elIf ([fb522ce](https://github.com/poppinss/edge/commit/fb522ce))



<a name="1.0.1"></a>
## [1.0.1](https://github.com/poppinss/edge/compare/v1.0.0...v1.0.1) (2017-06-21)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edge.js",
"version": "1.0.1",
"version": "1.0.2",
"description": "Node.js logical templating engine with fresh air",
"main": "index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/Context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class Context {
if (value === undefined) {
debug('resolving %s key on globals', key)
value = _.get(this.$globals, key)
value = typeof (value) === 'function' ? value.bind(this) : value
}

return value
Expand Down
25 changes: 24 additions & 1 deletion src/Globals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,29 @@ const size = function (input) {
*/
const el = function (htmlStr, hash) {
return this.safe(htmlStr.replace(/\$([\w.-]+)/g, (match, group) => {
return _.get(hash, _.toPath(group))
return group === 'self' ? hash : _.get(hash, _.toPath(group))
}))
}

/**
* Return element only when the 3rd param
* is true.
*
* @method elIf
*
* @param {String} htmlStr
* @param {Object} hash
* @param {Boolean} ifResult
*
* @return {String}
*/
const elIf = function (htmlStr, hash, ifResult) {
if (!ifResult) {
return ''
}

return this.safe(htmlStr.replace(/\$([\w.-]+)/g, (match, group) => {
return group === 'self' ? hash : _.get(hash, _.toPath(group))
}))
}

Expand Down Expand Up @@ -276,6 +298,7 @@ module.exports = {
groupBy,
size,
el,
elIf,
camelCase,
upperCase,
upperFirst,
Expand Down
30 changes: 30 additions & 0 deletions test/unit/globals.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,34 @@ test.group('Globals', () => {
const output = Globals.urlEncode('http://foo.com?username=aman virk')
assert.equal(output, 'http://foo.com?username=aman%20virk')
})

test('self should return the entire hash back', (assert) => {
const context = {
safe (input) {
return input
}
}
const output = Globals.el.bind(context)('$self', 'Hello')
assert.equal(output, 'Hello')
})

test('render when if value is true', (assert) => {
const context = {
safe (input) {
return input
}
}
const output = Globals.elIf.bind(context)('Hello $name', { name: 'virk' }, true)
assert.equal(output, 'Hello virk')
})

test('return empty string when elIf boolean is false', (assert) => {
const context = {
safe (input) {
return input
}
}
const output = Globals.elIf.bind(context)('Hello $name', { name: 'virk' }, false)
assert.equal(output, '')
})
})

0 comments on commit 593524a

Please sign in to comment.