1 | ;
|
2 |
|
3 | var isObject = require('./object'),
|
4 | isString = require('./string'),
|
5 | handleCurry = require('./utils/handleCurry');
|
6 |
|
7 | /**
|
8 | * Checks whether an object has a given property
|
9 | *
|
10 | * **Aliases** _has_
|
11 | *
|
12 | * @function hasProperty
|
13 | *
|
14 | * @example
|
15 | * var is = require('predicates');
|
16 | *
|
17 | * var isDuck = is.hasProperty('quack');
|
18 | *
|
19 | * isDuck({quack: ':)'}); // true
|
20 | * // same as
|
21 | * is.hasProperty('quack', {quack: ':)'}); // true
|
22 | *
|
23 | * isDuck({type: 'car'}); // false
|
24 | *
|
25 | * @param {String} property
|
26 | * @param {Object} [object]
|
27 | * @throws {TypeError} if property is not a string
|
28 | * @returns {(Boolean|Predicate)} bool if at least two arguments provided, otherwise a predicate
|
29 | */
|
30 | module.exports = function hasProperty(property) {
|
31 | if (!isString(property)) {
|
32 | throw new TypeError('Property name must be a string');
|
33 | }
|
34 | return handleCurry.call(this, arguments, function hasPropertyPredicate(object) {
|
35 | return isObject(object) && property in object;
|
36 | });
|
37 | };
|