1 | ;
|
2 | /**
|
3 | * Returns a function that checks whether a value is equal to one of allowed values
|
4 | * Function compares values using === operator
|
5 | *
|
6 | * @function oneOf
|
7 | *
|
8 | * @example
|
9 | * var is = require('predicates');
|
10 | *
|
11 | * var isAllowedToAccess = is.oneOf('ROLE_ADMIN', 'ROLE_USER');
|
12 | * // same as
|
13 | * // var isAllowedToAccess = is.in(['ROLE_ADMIN', 'ROLE_USER']);
|
14 | *
|
15 | * isAllowedToAccess('ROLE_ADMIN'); // true
|
16 | * isAllowedToAccess('ROLE_ANONYMOUS'); // false
|
17 | *
|
18 | * @param {...*} allowedValue
|
19 | * @throws {Error} if 0 or 1 allowed value provided
|
20 | * @returns {Predicate}
|
21 | */
|
22 | module.exports = function isOneOf() {
|
23 | var allowedValues = Array.prototype.slice.call(arguments);
|
24 |
|
25 | if (allowedValues.length < 2) {
|
26 | throw new Error('At least 2 allowed values are required');
|
27 | }
|
28 |
|
29 | return function isOneOfPredicate(value) {
|
30 | return allowedValues.indexOf(value) !== -1;
|
31 | };
|
32 | };
|