Guard against calling fieldWildcardMatcher or fieldWildcardFilter with undefined (#19865)

This commit is contained in:
CJ Cenizal 2018-06-14 08:45:13 -07:00 committed by GitHub
parent f5a70272ce
commit fd22e216fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View file

@ -57,6 +57,18 @@ describe('fieldWildcard', function () {
});
describe('filter', function () {
it('filters nothing when given undefined', function () {
const filter = fieldWildcardFilter();
const original = [
'foo',
'bar',
'baz',
1234
];
expect(original.filter(filter)).to.eql(original);
});
it('filters nothing when given an empty array', function () {
const filter = fieldWildcardFilter([]);
const original = [

View file

@ -26,7 +26,8 @@ export function FieldWildcardProvider(config) {
return new RegExp('^' + glob.split('*').map(escapeRegExp).join('.*') + '$');
});
function fieldWildcardMatcher(globs) {
// Note that this will return an essentially noop function if globs is undefined.
function fieldWildcardMatcher(globs = []) {
return function matcher(val) {
// do not test metaFields or keyword
if (metaFields.indexOf(val) !== -1) {
@ -36,7 +37,8 @@ export function FieldWildcardProvider(config) {
};
}
function fieldWildcardFilter(globs) {
// Note that this will return an essentially noop function if globs is undefined.
function fieldWildcardFilter(globs = []) {
const matcher = fieldWildcardMatcher(globs);
return function filter(val) {
return !matcher(val);