UNPKG

1.14 kBJavaScriptView Raw
1var castPath = require('./_castPath'),
2 isArguments = require('./isArguments'),
3 isArray = require('./isArray'),
4 isIndex = require('./_isIndex'),
5 isKey = require('./_isKey'),
6 isLength = require('./isLength'),
7 toKey = require('./_toKey');
8
9/**
10 * Checks if `path` exists on `object`.
11 *
12 * @private
13 * @param {Object} object The object to query.
14 * @param {Array|string} path The path to check.
15 * @param {Function} hasFunc The function to check properties.
16 * @returns {boolean} Returns `true` if `path` exists, else `false`.
17 */
18function hasPath(object, path, hasFunc) {
19 path = isKey(path, object) ? [path] : castPath(path);
20
21 var index = -1,
22 length = path.length,
23 result = false;
24
25 while (++index < length) {
26 var key = toKey(path[index]);
27 if (!(result = object != null && hasFunc(object, key))) {
28 break;
29 }
30 object = object[key];
31 }
32 if (result || ++index != length) {
33 return result;
34 }
35 length = object == null ? 0 : object.length;
36 return !!length && isLength(length) && isIndex(key, length) &&
37 (isArray(object) || isArguments(object));
38}
39
40module.exports = hasPath;