From a79c64894f119d05c1b9de4b25da1cbe4daf171d Mon Sep 17 00:00:00 2001 From: Jairo <68893868+jairo-bc@users.noreply.github.com> Date: Fri, 3 Feb 2023 10:37:38 -0500 Subject: [PATCH] fix: STRF-9673 Fix null params passed to helpers (#240) --- helpers/all.js | 2 +- helpers/contains.js | 2 +- helpers/deprecated/pick.js | 2 +- helpers/join.js | 3 +++ helpers/pluck.js | 5 ++++- spec/helpers/all.js | 7 ++++++- spec/helpers/any.js | 4 ++++ spec/helpers/join.js | 3 ++- spec/helpers/pluck.js | 17 ++++++++++++++++- spec/helpers/thirdParty.js | 9 +++++++++ 10 files changed, 47 insertions(+), 7 deletions(-) diff --git a/helpers/all.js b/helpers/all.js index b147fe9..627d8a9 100644 --- a/helpers/all.js +++ b/helpers/all.js @@ -15,7 +15,7 @@ const factory = () => { const opts = args.pop(); // Check if all the args are valid / truthy - const result = args.every( function (arg) { + const result = args.every(function (arg) { if (utils.isArray(arg)) { return !!arg.length; } diff --git a/helpers/contains.js b/helpers/contains.js index 73d8ca4..d20073f 100644 --- a/helpers/contains.js +++ b/helpers/contains.js @@ -14,7 +14,7 @@ const factory = () => { return function(container, value) { const options = arguments[arguments.length - 1]; const preparedContainer = utils.isObject(container) ? Object.values(container) : container; - const contained = preparedContainer.includes(value); + const contained = preparedContainer ? preparedContainer.includes(value) : false; // Yield block if true if (contained) { diff --git a/helpers/deprecated/pick.js b/helpers/deprecated/pick.js index 2a70a21..ad5e4b4 100644 --- a/helpers/deprecated/pick.js +++ b/helpers/deprecated/pick.js @@ -9,7 +9,7 @@ const factory = () => { const target = args.shift(); const toReturn = {}; const paths = args[0]; - if (paths) { + if (paths && Array.isArray(paths)) { paths.forEach((key) => { if (target.hasOwnProperty(key)) { toReturn[key] = target[key]; diff --git a/helpers/join.js b/helpers/join.js index 918f9aa..2e951b1 100644 --- a/helpers/join.js +++ b/helpers/join.js @@ -5,6 +5,9 @@ const factory = () => { const options = arguments[arguments.length - 1]; var config = options.hash || {}; + if (!Array.isArray(array)) { + throw new TypeError("Non-array passed to join helper"); + } array = array.slice(); // Truncate array diff --git a/helpers/pluck.js b/helpers/pluck.js index cb0acdc..80e96fa 100644 --- a/helpers/pluck.js +++ b/helpers/pluck.js @@ -2,7 +2,10 @@ const factory = () => { return function(collection, path) { - return collection.map(item => item.hasOwnProperty(path) ? item[path] : undefined); + if (collection) { + return collection.map(item => item.hasOwnProperty(path) ? item[path] : undefined); + } + return []; }; }; diff --git a/spec/helpers/all.js b/spec/helpers/all.js index 876bf4d..eb4cc84 100644 --- a/spec/helpers/all.js +++ b/spec/helpers/all.js @@ -16,7 +16,8 @@ describe('all helper', function() { emptyObject: {}, itemArray: [1,2], emptyString: "", - big: 'big' + big: 'big', + alwaysNull: null, }; // Build a test runner that uses a default context @@ -114,6 +115,10 @@ describe('all helper', function() { input: '{{#all true "" alwaysTrue}}big{{/all}}', output: '', }, + { + input: '{{#all alwaysNull}}big{{/all}}', + output: '', + }, ], done); }); }); diff --git a/spec/helpers/any.js b/spec/helpers/any.js index 77bbca2..7afb311 100644 --- a/spec/helpers/any.js +++ b/spec/helpers/any.js @@ -65,6 +65,10 @@ describe('any helper (with option hash)', function() { foobar: undefined } }, + { + input: '{{#any null num=null}}{{big}}{{/any}}', + output: '', + }, ], done); }); }); diff --git a/spec/helpers/join.js b/spec/helpers/join.js index 73a1dc3..5c2d751 100644 --- a/spec/helpers/join.js +++ b/spec/helpers/join.js @@ -6,7 +6,8 @@ const Lab = require('lab'), describe('join helper', function() { const context = { - list: ['Mario', 'Chris', 'Mick', 'Hau', 'Cody'] + list: ['Mario', 'Chris', 'Mick', 'Hau', 'Cody'], + notArray: null, }; const runTestCases = testRunner({context}); diff --git a/spec/helpers/pluck.js b/spec/helpers/pluck.js index dcc9a4a..36f6300 100644 --- a/spec/helpers/pluck.js +++ b/spec/helpers/pluck.js @@ -9,7 +9,9 @@ describe('pluck helper', function() { users: [ { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 } - ] + ], + null: null, + undefined: undefined, }; const runTestCases = testRunner({context}); @@ -39,4 +41,17 @@ describe('pluck helper', function() { }, ], done); }); + + it('should return empty array when null is provided', function(done) { + runTestCases([ + { + input: '{{pluck null "age"}}', + output: '', + }, + { + input: '{{pluck undefined "age"}}', + output: '', + }, + ], done); + }); }); diff --git a/spec/helpers/thirdParty.js b/spec/helpers/thirdParty.js index 1a3014a..20ec689 100644 --- a/spec/helpers/thirdParty.js +++ b/spec/helpers/thirdParty.js @@ -7,6 +7,7 @@ const Lab = require('lab'), describe('third party handlebars-helpers', function() { const context = { array: [1, 2, 3, 4, 5], + null: null, object: {"a" : 1, "b" : 2}, options: { a: { b: { c: 'd' } } } }; @@ -90,6 +91,14 @@ describe('third party handlebars-helpers', function() { }, ], done); }); + it('renders the else block if it evaluates to false. null as input', function(done) { + runTestCases([ + { + input: `{{#contains null '3'}}This will not be rendered.{{else}}This will be rendered.{{/contains}}`, + output: 'This will be rendered.', + }, + ], done); + }); }); });