test-list
v1.0.0
Published
Flexible whitelist/blacklist testing
Downloads
2
Readme
Flexible whitelist/blacklist testing
Synopsis
var TestLists = require('test-list');
var lists = new TestLists(whitelist, blacklist);
var passed = lists.test(string);
Lists
Lists may be:
- an array — elements that are not already strings or
RegExp
objects are converted to strings - a
RegExp
— converted to a single-element array - a string — converted to a single-element array
- an object — converted to array comprised of the keys of the object's enumerable members that have defined values
undefined
— a no-op
Test
The single defined method, test(string)
, returns a boolean subject to the following conditions:
- If
whitelist
is defined, the test string mustmatch
one of its elements. - If
blacklist
is defined, the test string must not match any of its elements.
Examples
var lists = new TestLists(['abc','de','f']);
lists.test('abc') // true: whitelisted and no blacklist
lists.test('ab') // false: not in whitelist
lists.test('de') // true: whitelisted and no blacklist
lists.test('f') // true: whitelisted and no blacklist
lists.test('g') // false: not in whitelist
var threeOrMore = /^[a-z]{3}$/;
var lists = new TestLists(undefined, [threeOrMore, 'f']);
lists.test('abc') // false: no whitelist but strings of length 3 or more are blacklisted
lists.test('ab') // true: no whitelist and not blacklisted
lists.test('de') // true: no whitelist and not blacklisted
lists.test('f') // false: no whitelist but blacklisted
lists.test('g') // true: no whitelist and not blacklisted
var lists = new TestLists(['abc','de','f'], [threeOrMore, 'f']);
lists.test('abc') // false: whitelisted but strings of length 3 or more are blacklisted
lists.test('ab') // false: not in whitelist
lists.test('de') // true: whitelisted and not blacklisted
lists.test('f') // false: whitelisted but blacklisted
lists.test('g') // false: not in whitelist