From ff072856250a3ca413f98f98675ea547b69516f1 Mon Sep 17 00:00:00 2001 From: Roman Krasinskyi Date: Mon, 12 Dec 2022 12:03:44 +0200 Subject: [PATCH 1/2] feat: Add JSONparseSafe helper --- helpers.js | 1 + helpers/jsonParseSafe.js | 18 ++++++++++++++++++ spec/helpers.js | 1 + spec/helpers/jsonParseSafe.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 helpers/jsonParseSafe.js create mode 100644 spec/helpers/jsonParseSafe.js diff --git a/helpers.js b/helpers.js index 9f78ec6..c764cce 100644 --- a/helpers.js +++ b/helpers.js @@ -30,6 +30,7 @@ const helpersList = [ 'join', 'jsContext', 'json', + 'jsonParseSafe', 'lang', 'langJson', 'limit', diff --git a/helpers/jsonParseSafe.js b/helpers/jsonParseSafe.js new file mode 100644 index 0000000..0cb6c74 --- /dev/null +++ b/helpers/jsonParseSafe.js @@ -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, +}]; diff --git a/spec/helpers.js b/spec/helpers.js index 337f55d..221feaf 100644 --- a/spec/helpers.js +++ b/spec/helpers.js @@ -146,6 +146,7 @@ describe('helper registration', () => { 'hasOwn', 'isObject', 'merge', + 'JSONparseSafe', 'JSONparse', 'JSONstringify', 'camelcase', diff --git a/spec/helpers/jsonParseSafe.js b/spec/helpers/jsonParseSafe.js new file mode 100644 index 0000000..dbb1351 --- /dev/null +++ b/spec/helpers/jsonParseSafe.js @@ -0,0 +1,28 @@ +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 have the same behavior as the original if helper', done => { + runTestCases([ + { + input: '{{#JSONparseSafe string}}{{name}}{{/JSONparseSafe}}', + output: '', + }, + { + input: '{{#JSONparseSafe jsonString}}{{name}}{{/JSONparseSafe}}', + output: 'John', + }, + { + input: '{{#JSONparseSafe string}}{{name}}{{else}}{{string}}{{/JSONparseSafe}}', + output: 'John Doe', + }, + ], done); + }); +}); From cb956c078df1b78eeaaa0e061236092375c0714b Mon Sep 17 00:00:00 2001 From: Roman Krasinskyi Date: Wed, 21 Dec 2022 17:42:43 +0200 Subject: [PATCH 2/2] feat: Update tests --- spec/helpers/jsonParseSafe.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/helpers/jsonParseSafe.js b/spec/helpers/jsonParseSafe.js index dbb1351..59f88d3 100644 --- a/spec/helpers/jsonParseSafe.js +++ b/spec/helpers/jsonParseSafe.js @@ -9,16 +9,26 @@ describe('JSONParseSafe helper', () => { }; const runTestCases = testRunner({context}); - it('should have the same behavior as the original if helper', done => { + it('should execute the main instruction', done => { runTestCases([ - { - input: '{{#JSONparseSafe string}}{{name}}{{/JSONparseSafe}}', - output: '', - }, { 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',