import isObject from './isObject' type keyableItem = Array | { [k: number | string]: any } /** * Get an array of keys from any object or array. Will return empty array when invalid or there are no keys. * Optional flag will include the inherited keys from prototype chain when set. * @param object * @param includeInherited */ const objectKeys = (object: keyableItem, includeInherited: boolean = false): Array => { if (typeof object !== 'function' && !isObject(object)) { return [] } if (includeInherited) { const propNames = Object.getOwnPropertyNames(object) if (propNames.length) { return propNames } } const keys = [] for (const key in object) { if (includeInherited || Object.prototype.hasOwnProperty.call(object, key)) { if (Array.isArray(object)) { keys.push(parseInt(key)) continue } keys.push(key) } } return keys } export default objectKeys