Handle invalid input to list filter functions safely.

Previously list filter functions did not handle invalid input safely.
This adds a check for null or undefined input and returns a default safe
value.

Change-Id: I59ea3be8b3d8413851204046890121bda3a44e04
This commit is contained in:
Tim Buckley 2016-01-14 15:55:14 -07:00
parent da45dab63c
commit a96875e606

View File

@ -3,36 +3,64 @@
var filtersModule = require('./_index.js');
var split = function(input, delim) {
if (typeof input === 'undefined' || input === null) {
return [];
}
delim = delim || ',';
return input.split(delim);
};
var join = function(input, delim) {
if (typeof input === 'undefined' || input === null) {
return '';
}
delim = delim || ', ';
return input.join(delim);
};
var pick = function(input, index) {
if (typeof input === 'undefined' || input === null) {
return '';
}
return input[index];
};
var pickRight = function(input, index) {
if (typeof input === 'undefined' || input === null) {
return '';
}
return input[input.length - index];
};
var slice = function(input, begin, end) {
if (typeof input === 'undefined' || input === null) {
return [];
}
return input.slice(begin, end);
};
var first = function(input, length) {
if (typeof input === 'undefined' || input === null) {
return '';
}
length = length || 1;
return input.slice(0, input.length - length);
};
var last = function(input, length) {
if (typeof input === 'undefined' || input === null) {
return '';
}
length = length || 1;
return input.slice(input.length - length, input.length);