mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import baseCastPath from './_baseCastPath';
|
|
import isArguments from './isArguments';
|
|
import isArray from './isArray';
|
|
import isIndex from './_isIndex';
|
|
import isKey from './_isKey';
|
|
import isLength from './isLength';
|
|
import isString from './isString';
|
|
|
|
/**
|
|
* Checks if `path` exists on `object`.
|
|
*
|
|
* @private
|
|
* @param {Object} object The object to query.
|
|
* @param {Array|string} path The path to check.
|
|
* @param {Function} hasFunc The function to check properties.
|
|
* @returns {boolean} Returns `true` if `path` exists, else `false`.
|
|
*/
|
|
function hasPath(object, path, hasFunc) {
|
|
if (object == null) {
|
|
return false;
|
|
}
|
|
var result = hasFunc(object, path);
|
|
if (!result && !isKey(path)) {
|
|
path = baseCastPath(path);
|
|
|
|
var index = -1,
|
|
length = path.length;
|
|
|
|
while (object != null && ++index < length) {
|
|
var key = path[index];
|
|
if (!(result = hasFunc(object, key))) {
|
|
break;
|
|
}
|
|
object = object[key];
|
|
}
|
|
}
|
|
var length = object ? object.length : undefined;
|
|
return result || (
|
|
!!length && isLength(length) && isIndex(path, length) &&
|
|
(isArray(object) || isString(object) || isArguments(object))
|
|
);
|
|
}
|
|
|
|
export default hasPath;
|