From 4c4b29cdef21d09c88ab909836efe0705c8be4bd Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 26 Feb 2017 23:08:19 -0800 Subject: [PATCH] Expose `hasPath` and `hasPathIn`. --- hasPath.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hasPathIn.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 hasPath.js create mode 100644 hasPathIn.js diff --git a/hasPath.js b/hasPath.js new file mode 100644 index 000000000..08c6ac893 --- /dev/null +++ b/hasPath.js @@ -0,0 +1,53 @@ +import castPath from './.internal/castPath.js' +import isArguments from './isArguments.js' +import isIndex from './.internal/isIndex.js' +import isLength from './isLength.js' +import toKey from './.internal/toKey.js' + +/** Used to check objects for own properties. */ +const hasOwnProperty = Object.prototype.hasOwnProperty + +/** + * Checks if `path` is a direct property of `object`. + * + * @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 + * + * 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) { + path = castPath(path, object) + + let key + let index = -1 + let length = path.length + let result = false + + while (++index < length) { + key = toKey(path[index]) + if (!(result = object != null && hasOwnProperty.call(object, key))) { + break + } + object = object[key] + } + if (result || ++index != length) { + return result + } + length = object == null ? 0 : object.length + return !!length && isLength(length) && isIndex(key, length) && + (Array.isArray(object) || isArguments(object)) +} + +export default hasPath diff --git a/hasPathIn.js b/hasPathIn.js new file mode 100644 index 000000000..aef7e026e --- /dev/null +++ b/hasPathIn.js @@ -0,0 +1,50 @@ +import castPath from './.internal/castPath.js' +import isArguments from './isArguments.js' +import isIndex from './.internal/isIndex.js' +import isLength from './isLength.js' +import toKey from './.internal/toKey.js' + +/** + * Checks if `path` is a direct property of `object`. + * + * @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 hasPath + * @example + * + * const object = { 'a': { 'b': 2 } } + * const other = create({ 'a': create({ 'b': 2 }) }) + * + * hasPathIn(object, 'a.b') + * // => true + * + * hasPathIn(object, ['a', 'b']) + * // => true + */ +function hasPathIn(object, path) { + path = castPath(path, object) + + let key + let index = -1 + let length = path.length + let result = false + + while (++index < length) { + key = toKey(path[index]) + if (!(result = object != null && key in Object(object))) { + break + } + object = object[key] + } + if (result || ++index != length) { + return result + } + length = object == null ? 0 : object.length + return !!length && isLength(length) && isIndex(key, length) && + (Array.isArray(object) || isArguments(object)) +} + +export default hasPath