| 1 | import { PropertyPath } from '../_internal/PropertyPath.mjs';
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * Checks if a given path exists in an object, **including inherited properties**.
|
|---|
| 5 | *
|
|---|
| 6 | * You can provide the path as a single property key, an array of property keys,
|
|---|
| 7 | * or a string representing a deep path.
|
|---|
| 8 | *
|
|---|
| 9 | * Unlike `has`, which only checks for own properties, `hasIn` also checks for properties
|
|---|
| 10 | * in the prototype chain.
|
|---|
| 11 | *
|
|---|
| 12 | * If the path is an index and the object is an array or an arguments object, the function will verify
|
|---|
| 13 | * if the index is valid and within the bounds of the array or arguments object, even if the array or
|
|---|
| 14 | * arguments object is sparse (i.e., not all indexes are defined).
|
|---|
| 15 | *
|
|---|
| 16 | * @template T
|
|---|
| 17 | * @param {T} object - The object to query.
|
|---|
| 18 | * @param {PropertyPath} path - The path to check. This can be a single property key,
|
|---|
| 19 | * an array of property keys, or a string representing a deep path.
|
|---|
| 20 | * @returns {boolean} Returns `true` if the path exists (own or inherited), `false` otherwise.
|
|---|
| 21 | *
|
|---|
| 22 | * @example
|
|---|
| 23 | *
|
|---|
| 24 | * const obj = { a: { b: { c: 3 } } };
|
|---|
| 25 | *
|
|---|
| 26 | * hasIn(obj, 'a'); // true
|
|---|
| 27 | * hasIn(obj, ['a', 'b']); // true
|
|---|
| 28 | * hasIn(obj, ['a', 'b', 'c']); // true
|
|---|
| 29 | * hasIn(obj, 'a.b.c'); // true
|
|---|
| 30 | * hasIn(obj, 'a.b.d'); // false
|
|---|
| 31 | * hasIn(obj, ['a', 'b', 'c', 'd']); // false
|
|---|
| 32 | *
|
|---|
| 33 | * // Example with inherited properties:
|
|---|
| 34 | * function Rectangle() {}
|
|---|
| 35 | * Rectangle.prototype.area = function() {};
|
|---|
| 36 | *
|
|---|
| 37 | * const rect = new Rectangle();
|
|---|
| 38 | * hasIn(rect, 'area'); // true
|
|---|
| 39 | * has(rect, 'area'); // false - has only checks own properties
|
|---|
| 40 | */
|
|---|
| 41 | declare function hasIn<T>(object: T, path: PropertyPath): boolean;
|
|---|
| 42 |
|
|---|
| 43 | export { hasIn };
|
|---|