@feugene/mu
Version:
Helpful TS utilities without dependencies
48 lines • 2.11 kB
JavaScript
import isArguments from '../is/isArguments.mjs';
import isBuffer from '../is/isBuffer.mjs';
import isTypedArray from '../is/isTypedArray.mjs';
import isIndex from './isIndex.mjs';
/** Used for built-in method references. */
const objectProto = Object.prototype;
/** Used to check objects for own properties. */
const hasOwnProperty = objectProto.hasOwnProperty;
/**
* Creates an array of the enumerable property names of the array-like `value`.
*
* @private
* @param {*} value The value to query.
* @param {boolean} inherited Specify returning inherited property names.
* @returns {Array|[]} Returns the array of property names.
*/
export default function arrayLikeKeys(value, inherited = false) {
const isArray_ = Array.isArray(value);
const isArgument = !isArray_ && isArguments(value);
const isBuff = !isArray_ && !isArgument && isBuffer(value);
const isType = !isArray_ && !isArgument && !isBuff && isTypedArray(value);
const skipIndexes = isArray_ || isArgument || isBuff || isType;
// Pre-populate index keys for array-like values without relying on generic utils.
const result = [];
if (skipIndexes) {
const len = (value?.length ?? 0) >>> 0;
for (let i = 0; i < len; i++) {
result.push(String(i));
}
}
const length = result.length;
for (const key in value) {
if ((inherited || hasOwnProperty.call(value, key)) &&
!(skipIndexes &&
// Safari 9 has enumerable `arguments.length` in strict mode.
(key === 'length' ||
// Node.js 0.10 has enumerable non-index properties on buffers.
(isBuff && (key === 'offset' || key === 'parent')) ||
// PhantomJS 2 has enumerable non-index properties on typed arrays.
(isType && (key === 'buffer' || key === 'byteLength' || key === 'byteOffset')) ||
// Skip index properties.
isIndex(key, length)))) {
result.push(key);
}
}
return result;
}
//# sourceMappingURL=arrayLikeKeys.mjs.map