1 | ;
|
2 |
|
3 | var isObject = require('./object'),
|
4 | isFunction = require('./function'),
|
5 | handleCurry = require('./utils/handleCurry');
|
6 |
|
7 | /**
|
8 | * Checks whether a value of given property of an object satisfies a predicate
|
9 | *
|
10 | * If you need to check more properties at a time use {@link structure}.
|
11 | *
|
12 | * NOTE! Provided predicate will be called ALWAYS if a provided value is an object.
|
13 | *
|
14 | * **Aliases** _prop_
|
15 | *
|
16 | * @function property
|
17 | *
|
18 | * @example
|
19 | * var is = require('predicates');
|
20 | *
|
21 | * is.property('name', is.string, {name: 'Tommy'}); // true
|
22 | * is.property('name', is.string)({name: 'Tommy'}); // true
|
23 | * is.property('name', is.string, {name: 2}); // false - since 2 is not a string
|
24 | * is.property('name', is.string, {}); // false - since undefined is not a string
|
25 | *
|
26 | * @param {*} propertyName
|
27 | * @param {Predicate} predicate
|
28 | * @param {Object} [value]
|
29 | * @param {...*} [additionalArgs] additional arguments passed to the predicate
|
30 | * @throws {TypeError} if predicate is not a function
|
31 | * @throws {Error} if too few arguments provided
|
32 | * @return {(Boolean|Predicate)} boolean if at least 3 arguments provided, otherwise a predicate
|
33 | */
|
34 | module.exports = function property(propertyName, predicate) {
|
35 | if (arguments.length < 2) {
|
36 | throw new Error('Too few arguments - 2 required');
|
37 | }
|
38 |
|
39 | if (!isFunction(predicate)) {
|
40 | throw new TypeError('Predicate is not a function');
|
41 | }
|
42 |
|
43 | return handleCurry.call(this, arguments, function isPropertySatisfiesPredicateTest(value) {
|
44 | var args = Array.prototype.slice.call(arguments);
|
45 | args.splice(0, 1, isObject(value) ? value[propertyName] : undefined);
|
46 | return isObject(value) && predicate.apply(this, args);
|
47 | }, 2);
|
48 | }; |
\ | No newline at end of file |