-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add JSONparseSafe helper (#235)
* feat: Add JSONparseSafe helper * feat: Update tests
- Loading branch information
1 parent
c83bb31
commit f9edc1b
Showing
4 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ const helpersList = [ | |
'join', | ||
'jsContext', | ||
'json', | ||
'jsonParseSafe', | ||
'lang', | ||
'langJson', | ||
'limit', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
function factory(globals) { | ||
return function (value) { | ||
const options = arguments[arguments.length - 1]; | ||
|
||
try { | ||
return options.fn(JSON.parse(value)); | ||
} catch (err) { | ||
return options.inverse(this); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = [{ | ||
name: 'JSONparseSafe', | ||
factory: factory, | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { describe, it } = exports.lab = require('lab').script(); | ||
|
||
const { testRunner } = require('../spec-helpers'); | ||
|
||
describe('JSONParseSafe helper', () => { | ||
const context = { | ||
jsonString: '{"name": "John"}', | ||
string: 'John Doe', | ||
}; | ||
const runTestCases = testRunner({context}); | ||
|
||
it('should execute the main instruction', done => { | ||
runTestCases([ | ||
{ | ||
input: '{{#JSONparseSafe jsonString}}{{name}}{{/JSONparseSafe}}', | ||
output: 'John', | ||
}, | ||
], done); | ||
}); | ||
|
||
it('should skip the main instruction if variable is non-json', done => { | ||
runTestCases([ | ||
{ | ||
input: '{{#JSONparseSafe string}}{{name}}{{/JSONparseSafe}}', | ||
output: '', | ||
}, | ||
], done); | ||
}); | ||
|
||
it('should execute the else instruction if variable is non-json', done => { | ||
runTestCases([ | ||
{ | ||
input: '{{#JSONparseSafe string}}{{name}}{{else}}{{string}}{{/JSONparseSafe}}', | ||
output: 'John Doe', | ||
}, | ||
], done); | ||
}); | ||
}); |