source: node_modules/es-toolkit/dist/compat/object/keys.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.2 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 keys(object) {
8 if (isArrayLike(object)) {
9 return arrayLikeKeys(object);
10 }
11 const result = Object.keys(Object(object));
12 if (!isPrototype(object)) {
13 return result;
14 }
15 return result.filter(key => key !== 'constructor');
16}
17function arrayLikeKeys(object) {
18 const indices = times(object.length, index => `${index}`);
19 const filteredKeys = new Set(indices);
20 if (isBuffer(object)) {
21 filteredKeys.add('offset');
22 filteredKeys.add('parent');
23 }
24 if (isTypedArray(object)) {
25 filteredKeys.add('buffer');
26 filteredKeys.add('byteLength');
27 filteredKeys.add('byteOffset');
28 }
29 const inheritedKeys = Object.keys(object).filter(key => !filteredKeys.has(key));
30 if (Array.isArray(object)) {
31 return [...indices, ...inheritedKeys];
32 }
33 return [...indices.filter(index => Object.hasOwn(object, index)), ...inheritedKeys];
34}
35
36export { keys };
Note: See TracBrowser for help on using the repository browser.