| 1 | import { isBuffer } from '../../predicate/isBuffer.mjs';
|
|---|
| 2 | import { isPrototype } from '../_internal/isPrototype.mjs';
|
|---|
| 3 | import { isArrayLike } from '../predicate/isArrayLike.mjs';
|
|---|
| 4 | import { isTypedArray } from '../predicate/isTypedArray.mjs';
|
|---|
| 5 | import { times } from '../util/times.mjs';
|
|---|
| 6 |
|
|---|
| 7 | function 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 | }
|
|---|
| 27 | function keysInImpl(object) {
|
|---|
| 28 | const result = [];
|
|---|
| 29 | for (const key in object) {
|
|---|
| 30 | result.push(key);
|
|---|
| 31 | }
|
|---|
| 32 | return result;
|
|---|
| 33 | }
|
|---|
| 34 | function prototypeKeysIn(object) {
|
|---|
| 35 | const keys = keysInImpl(object);
|
|---|
| 36 | return keys.filter(key => key !== 'constructor');
|
|---|
| 37 | }
|
|---|
| 38 | function 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 |
|
|---|
| 57 | export { keysIn };
|
|---|