Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
49 lines (45 sloc)
1.24 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var baseGet = require('../internal/baseGet'), | |
| baseSlice = require('../internal/baseSlice'), | |
| isKey = require('../internal/isKey'), | |
| last = require('../array/last'), | |
| toPath = require('../internal/toPath'); | |
| /** Used for native method references. */ | |
| var objectProto = Object.prototype; | |
| /** Used to check objects for own properties. */ | |
| var hasOwnProperty = objectProto.hasOwnProperty; | |
| /** | |
| * Checks if `path` is a direct property. | |
| * | |
| * @static | |
| * @memberOf _ | |
| * @category Object | |
| * @param {Object} object The object to query. | |
| * @param {Array|string} path The path to check. | |
| * @returns {boolean} Returns `true` if `path` is a direct property, else `false`. | |
| * @example | |
| * | |
| * var object = { 'a': { 'b': { 'c': 3 } } }; | |
| * | |
| * _.has(object, 'a'); | |
| * // => true | |
| * | |
| * _.has(object, 'a.b.c'); | |
| * // => true | |
| * | |
| * _.has(object, ['a', 'b', 'c']); | |
| * // => true | |
| */ | |
| function has(object, path) { | |
| if (object == null) { | |
| return false; | |
| } | |
| var result = hasOwnProperty.call(object, path); | |
| if (!result && !isKey(path)) { | |
| path = toPath(path); | |
| object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); | |
| path = last(path); | |
| result = object != null && hasOwnProperty.call(object, path); | |
| } | |
| return result; | |
| } | |
| module.exports = has; |