Вот те универсальная шляпа.)
/**
* @callback Filter
* @param value
* @param {string | symbol} key
* @param {?object} getset
* @param {?function} getset.get
* @param {?function} getset.set
* @returns {boolean}
*/
/**
* @typedef Options
* @type {object}
* @property {boolean=true} deep
* @property {boolean=true} getters
* @property {boolean=getters} setters
* @property {boolean=false} symbols
* @property {boolean=} enumerable
* @property {boolean=} configurable
* @property {boolean=} writable
*/
/**
* @param {object} obj
* @param {Filter} [filter]
* @param {Options} [options]
* @returns {string[]}
*/
function getPropertyKeys(obj, filter, options) {
if (typeof filter !== 'function') {
options = filter;
filter = (value, key) => true;
}
const {
deep = true,
getters = true,
setters = getters,
symbols = false,
...descriptorOptions
} = options ?? {};
const not = (descriptor, key) => key in descriptorOptions && descriptorOptions[key] !== descriptor[key];
const {
getOwnPropertySymbols,
getPrototypeOf,
getOwnPropertyDescriptors,
keys,
assign,
prototype
} = Object;
let current = obj;
let descriptors;
do {
descriptors = assign(getOwnPropertyDescriptors(current), descriptors);
if (current === prototype) break;
current = getPrototypeOf(current);
} while (deep && current);
let list = keys(descriptors);
if (symbols)
list = list.concat(getOwnPropertySymbols(descriptors));
return list.filter(key => {
const descriptor = descriptors[key];
const { get, set, value } = descriptor;
if (not(descriptor, 'enumerable'))
return false;
if (not(descriptor, 'configurable'))
return false;
if (get || set) {
return (setters || setters)
&& filter(get && get.call(obj), key, { get, set });
}
if (not(descriptor, 'writable'))
return false;
return filter(value, key);
});
}
getPropertyKeys([], value => typeof value === 'function', {
symbols: true
});