source: node_modules/es-toolkit/dist/compat/object/keysIn.mjs

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

Added visualizations

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import { isBuffer } from '../../predicate/isBuffer.mjs';
2import { isPrototype } from '../_internal/isPrototype.mjs';
3import { isArrayLike } from '../predicate/isArrayLike.mjs';
4import { isTypedArray } from '../predicate/isTypedArray.mjs';
5import { times } from '../util/times.mjs';
6
7function keysIn(object) {
8 if (object == null) {
9 return [];
10 }
11 switch (typeof object) {
12 case 'object':
13 case 'function': {
14 if (isArrayLike(object)) {
15 return arrayLikeKeysIn(object);
16 }
17 if (isPrototype(object)) {
18 return prototypeKeysIn(object);
19 }
20 return keysInImpl(object);
21 }
22 default: {
23 return keysInImpl(Object(object));
24 }
25 }
26}
27function keysInImpl(object) {
28 const result = [];
29 for (const key in object) {
30 result.push(key);
31 }
32 return result;
33}
34function prototypeKeysIn(object) {
35 const keys = keysInImpl(object);
36 return keys.filter(key => key !== 'constructor');
37}
38function arrayLikeKeysIn(object) {
39 const indices = times(object.length, index => `${index}`);
40 const filteredKeys = new Set(indices);
41 if (isBuffer(object)) {
42 filteredKeys.add('offset');
43 filteredKeys.add('parent');
44 }
45 if (isTypedArray(object)) {
46 filteredKeys.add('buffer');
47 filteredKeys.add('byteLength');
48 filteredKeys.add('byteOffset');
49 }
50 const inheritedKeys = keysInImpl(object).filter(key => !filteredKeys.has(key));
51 if (Array.isArray(object)) {
52 return [...indices, ...inheritedKeys];
53 }
54 return [...indices.filter(index => Object.hasOwn(object, index)), ...inheritedKeys];
55}
56
57export { keysIn };
Note: See TracBrowser for help on using the repository browser.