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

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