@newdash/newdash
Version:
javascript/typescript utility library
57 lines (56 loc) • 1.89 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasPath = void 0;
const castPath_1 = __importDefault(require("./.internal/castPath"));
const isArguments_1 = __importDefault(require("./isArguments"));
const isIndex_1 = __importDefault(require("./.internal/isIndex"));
const isLength_1 = __importDefault(require("./isLength"));
const toKey_1 = __importDefault(require("./.internal/toKey"));
const isArray_1 = __importDefault(require("./isArray"));
/**
* Checks if `path` is a direct property of `object`.
*
* @internal
* @ignore
* @since 5.0.0
* @category Object
* @param {Object} object The object to query.
* @param {Array|string} path The path to check.
* @returns {boolean} Returns `true` if `path` exists, else `false`.
* @see [[has]], [[hasIn]], [[hasPathIn]]
* @example
*
* ```js
* const object = { 'a': { 'b': 2 } }
* const other = create({ 'a': create({ 'b': 2 }) })
*
* hasPath(object, 'a.b')
* // => true
*
* hasPath(object, ['a', 'b'])
* // => true
* ```
*/
function hasPath(object, path, hasFunc) {
path = (0, castPath_1.default)(path, object);
let index = -1, length = path.length, result = false;
let key;
while (++index < length) {
key = (0, toKey_1.default)(path[index]);
if (!(result = object != null && hasFunc(object, key))) {
break;
}
object = object[key];
}
if (result || ++index != length) {
return result;
}
length = object == null ? 0 : object.length;
return !!length && (0, isLength_1.default)(length) && (0, isIndex_1.default)(key, length) &&
((0, isArray_1.default)(object) || (0, isArguments_1.default)(object));
}
exports.hasPath = hasPath;
exports.default = hasPath;