Skip to content

Commit

Permalink
fix: STRF-9673 Fix null params passed to helpers (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
jairo-bc authored Feb 3, 2023
1 parent dcc2ddd commit a79c648
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion helpers/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion helpers/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion helpers/deprecated/pick.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
3 changes: 3 additions & 0 deletions helpers/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion helpers/pluck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
};
};

Expand Down
7 changes: 6 additions & 1 deletion spec/helpers/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,6 +115,10 @@ describe('all helper', function() {
input: '{{#all true "" alwaysTrue}}big{{/all}}',
output: '',
},
{
input: '{{#all alwaysNull}}big{{/all}}',
output: '',
},
], done);
});
});
4 changes: 4 additions & 0 deletions spec/helpers/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ describe('any helper (with option hash)', function() {
foobar: undefined
}
},
{
input: '{{#any null num=null}}{{big}}{{/any}}',
output: '',
},
], done);
});
});
Expand Down
3 changes: 2 additions & 1 deletion spec/helpers/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
17 changes: 16 additions & 1 deletion spec/helpers/pluck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -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);
});
});
9 changes: 9 additions & 0 deletions spec/helpers/thirdParty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } } }
};
Expand Down Expand Up @@ -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);
});
});

});
Expand Down

0 comments on commit a79c648

Please sign in to comment.