Skip to content

Commit

Permalink
Add example helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
izqui committed Sep 20, 2018
1 parent 8116071 commit 8ff3081
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/helpers/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async (node, repeat = 1) => {
return { type: 'string', value: node.value.repeat(repeat) }
}
3 changes: 3 additions & 0 deletions src/helpers/formatDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async (timestamp, format='MM-DD-YYYY') => {
return { type: 'string', value: timestamp } // TODO
}
17 changes: 17 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const formatDate = require('./formatDate')
const echo = require('./echo')

module.exports = {
helpers: {
formatDate,
echo,
},

exists (helper) {
return !!this.helpers[helper]
},

execute (helper, inputs) {
return this.helpers[helper](...inputs)
}
}
11 changes: 10 additions & 1 deletion test/examples/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const bytes32 = (value) => ({
value
})

const helperCases = [
[{
source: 'helper `@echo(@echo(\'hi\', 2), 9 / 3)`',
bindings: { }
}, 'helper hihihihihihi'],
]

const cases = [
// Bindings
[{
Expand Down Expand Up @@ -108,7 +115,9 @@ const cases = [
[{
source: '`_bool ? \'h\' + _var + \'o\' : \'damn\'`',
bindings: { _bool: bool(true), _var: string('ell') }
}, 'hello']
}, 'hello'],

...helperCases,
]

for (let [input, expected] of cases) {
Expand Down

0 comments on commit 8ff3081

Please sign in to comment.