source: node_modules/es-toolkit/dist/compat/object/has.mjs@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.0 KB
Line 
1import { isDeepKey } from '../_internal/isDeepKey.mjs';
2import { isIndex } from '../_internal/isIndex.mjs';
3import { isArguments } from '../predicate/isArguments.mjs';
4import { toPath } from '../util/toPath.mjs';
5
6function has(object, path) {
7 let resolvedPath;
8 if (Array.isArray(path)) {
9 resolvedPath = path;
10 }
11 else if (typeof path === 'string' && isDeepKey(path) && object?.[path] == null) {
12 resolvedPath = toPath(path);
13 }
14 else {
15 resolvedPath = [path];
16 }
17 if (resolvedPath.length === 0) {
18 return false;
19 }
20 let current = object;
21 for (let i = 0; i < resolvedPath.length; i++) {
22 const key = resolvedPath[i];
23 if (current == null || !Object.hasOwn(current, key)) {
24 const isSparseIndex = (Array.isArray(current) || isArguments(current)) && isIndex(key) && key < current.length;
25 if (!isSparseIndex) {
26 return false;
27 }
28 }
29 current = current[key];
30 }
31 return true;
32}
33
34export { has };
Note: See TracBrowser for help on using the repository browser.