mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Simplify has and hasIn.
This commit is contained in:
@@ -1,16 +0,0 @@
|
|||||||
/** Used to check objects for own properties. */
|
|
||||||
const hasOwnProperty = Object.prototype.hasOwnProperty
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The base implementation of `has` without support for deep paths.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Object} [object] The object to query.
|
|
||||||
* @param {Array|string} key The key to check.
|
|
||||||
* @returns {boolean} Returns `true` if `key` exists, else `false`.
|
|
||||||
*/
|
|
||||||
function baseHas(object, key) {
|
|
||||||
return object != null && hasOwnProperty.call(object, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default baseHas
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
/**
|
|
||||||
* The base implementation of `hasIn` without support for deep paths.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Object} [object] The object to query.
|
|
||||||
* @param {Array|string} key The key to check.
|
|
||||||
* @returns {boolean} Returns `true` if `key` exists, else `false`.
|
|
||||||
*/
|
|
||||||
function baseHasIn(object, key) {
|
|
||||||
return object != null && key in Object(object)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default baseHasIn
|
|
||||||
22
has.js
22
has.js
@@ -1,15 +1,15 @@
|
|||||||
import baseHas from './.internal/baseHas.js'
|
/** Used to check objects for own properties. */
|
||||||
import hasPath from './.internal/hasPath.js'
|
const hasOwnProperty = Object.prototype.hasOwnProperty
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if `path` is a direct property of `object`.
|
* Checks if `key` is a direct property of `object`.
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
* @category Object
|
* @category Object
|
||||||
* @param {Object} object The object to query.
|
* @param {Object} object The object to query.
|
||||||
* @param {Array|string} path The path to check.
|
* @param {string} key The key to check.
|
||||||
* @returns {boolean} Returns `true` if `path` exists, else `false`.
|
* @returns {boolean} Returns `true` if `key` exists, else `false`.
|
||||||
* @see hasIn, set, get
|
* @see hasIn, hasPath, hasPathIn
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* const object = { 'a': { 'b': 2 } }
|
* const object = { 'a': { 'b': 2 } }
|
||||||
@@ -18,17 +18,11 @@ import hasPath from './.internal/hasPath.js'
|
|||||||
* has(object, 'a')
|
* has(object, 'a')
|
||||||
* // => true
|
* // => true
|
||||||
*
|
*
|
||||||
* has(object, 'a.b')
|
|
||||||
* // => true
|
|
||||||
*
|
|
||||||
* has(object, ['a', 'b'])
|
|
||||||
* // => true
|
|
||||||
*
|
|
||||||
* has(other, 'a')
|
* has(other, 'a')
|
||||||
* // => false
|
* // => false
|
||||||
*/
|
*/
|
||||||
function has(object, path) {
|
function has(object, key) {
|
||||||
return object != null && hasPath(object, path, baseHas)
|
return object != null && hasOwnProperty.call(object, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default has
|
export default has
|
||||||
|
|||||||
19
hasIn.js
19
hasIn.js
@@ -1,15 +1,12 @@
|
|||||||
import baseHasIn from './.internal/baseHasIn.js'
|
|
||||||
import hasPath from './.internal/hasPath.js'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if `path` is a direct or inherited property of `object`.
|
* Checks if `path` is a direct or inherited property of `object`.
|
||||||
*
|
*
|
||||||
* @since 4.0.0
|
* @since 4.0.0
|
||||||
* @category Object
|
* @category Object
|
||||||
* @param {Object} object The object to query.
|
* @param {Object} object The object to query.
|
||||||
* @param {Array|string} path The path to check.
|
* @param {string} key The key to check.
|
||||||
* @returns {boolean} Returns `true` if `path` exists, else `false`.
|
* @returns {boolean} Returns `true` if `key` exists, else `false`.
|
||||||
* @see has, get, set, unset
|
* @see has, hasPath, hasPathIn
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* const object = create({ 'a': create({ 'b': 2 }) })
|
* const object = create({ 'a': create({ 'b': 2 }) })
|
||||||
@@ -17,17 +14,11 @@ import hasPath from './.internal/hasPath.js'
|
|||||||
* hasIn(object, 'a')
|
* hasIn(object, 'a')
|
||||||
* // => true
|
* // => true
|
||||||
*
|
*
|
||||||
* hasIn(object, 'a.b')
|
|
||||||
* // => true
|
|
||||||
*
|
|
||||||
* hasIn(object, ['a', 'b'])
|
|
||||||
* // => true
|
|
||||||
*
|
|
||||||
* hasIn(object, 'b')
|
* hasIn(object, 'b')
|
||||||
* // => false
|
* // => false
|
||||||
*/
|
*/
|
||||||
function hasIn(object, path) {
|
function hasIn(object, key) {
|
||||||
return object != null && hasPath(object, path, baseHasIn)
|
return object != null && key in Object(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default hasIn
|
export default hasIn
|
||||||
|
|||||||
Reference in New Issue
Block a user