util-ex
Version:
Browser-friendly enhanced util fully compatible with standard node.js
30 lines (29 loc) • 915 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.getAllNames = getAllNames;
/**
* Get all names of an object, include non-enumerable properties.
* @param {object} obj - The object to get names from
* @param {boolean} [inherited] - whether includes inherited properties, defaults to true
* @returns {string[]} An array of all names of the object
*/
function getAllNames(obj, inherited) {
const keys = new Set();
// if primitive (primitives still have keys) skip the first iteration
if (!(obj instanceof Object)) {
obj = Object.getPrototypeOf(obj);
}
if (inherited !== false) {
while (obj) {
Reflect.ownKeys(obj).forEach(keys.add, keys);
obj = Object.getPrototypeOf(obj);
}
} else {
Reflect.ownKeys(obj).forEach(keys.add, keys);
}
return Array.from(keys);
}
var _default = exports.default = getAllNames;