Comparing version 2.0.2 to 2.1.0
61
index.js
@@ -10,4 +10,4 @@ 'use strict'; | ||
function GreyList(options) { | ||
this.white = arrayify(options && options.white); | ||
this.black = arrayify(options && options.black); | ||
this.white = getFlatArrayOfRegexAndOrString(options && options.white); | ||
this.black = getFlatArrayOfRegexAndOrString(options && options.black); | ||
} | ||
@@ -29,32 +29,43 @@ | ||
// Passes through `undefined` else converts to array of strings and/or `RegExp` objects. | ||
function arrayify(list) { | ||
if (list !== undefined) { | ||
// if an object, get its enumerable defined keys | ||
if (Object.prototype.toString.call(list) === '[object Object]') { | ||
list = Object.keys(list).filter(function(item) { | ||
return list[item] !== undefined; | ||
}); | ||
function getFlatArrayOfRegexAndOrString(array, flat) { | ||
if (!flat) { | ||
// this is the top-level (non-recursed) call so intialize: | ||
// `undefined` passes through without being converted to an array | ||
if (array === undefined) { | ||
return; | ||
} | ||
// make sure it's an array | ||
if (!Array.isArray(list)) { | ||
list = [list]; | ||
// arrayify given scalar string, regex, or object | ||
if (!Array.isArray(array)) { | ||
array = [array]; | ||
} | ||
// initialize flat | ||
flat = []; | ||
} | ||
array.forEach(function (item) { | ||
// make sure all elements are either string or RegExp | ||
list = list.map(function(item) { | ||
switch (Object.prototype.toString.call(item)) { | ||
case '[object String]': | ||
case '[object RegExp]': | ||
break; | ||
default: | ||
item = Object.prototype.toString.call(item); | ||
} | ||
return item; | ||
}); | ||
} | ||
return list; | ||
switch (Object.prototype.toString.call(item)) { | ||
case '[object String]': | ||
case '[object RegExp]': | ||
flat.push(item); | ||
break; | ||
case '[object Object]': | ||
// recurse on complex item (when an object or array) | ||
if (!Array.isArray(item)) { | ||
// convert object into an array (of it's enumerable keys, but only when not undefined) | ||
item = Object.keys(item).filter(function (key) { return item[key] !== undefined; }); | ||
} | ||
getFlatArrayOfRegexAndOrString(item, flat); | ||
break; | ||
default: | ||
flat.push(item + ''); // convert to string | ||
} | ||
}); | ||
return flat; | ||
} | ||
module.exports = GreyList; |
{ | ||
"name": "greylist", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Flexible whitelist/blacklist testing", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6972
58