Repository URL to install this package:
|
Version:
1.1.21 ▾
|
"use strict";
const NON_ENUMERABLE = require("../native/NON_ENUMERABLE");
const isSymbol = require("../is/symbol");
const uniq = require("../array/uniq");
const concatMutate = require("../array/concatMutate");
const forProto = require("../loop/each/forProto");
const keys = require("./keys");
const getOwnPropertyNames = Object.getOwnPropertyNames;
const getOwnPropertySymbols = Object.getOwnPropertySymbols;
/**
* @desc properties, property symbols, object keys
* ^ all again for prototype
* @memberOf util
* @since 3.0.0
* @version 5.0.0-beta.4 only used in gc (as of 5.0.0-beta.4)
*
* @param {Object} obj object to get properties & symbols from
* @return {Array<string>} properties
*
* @see deps/gc
* @see deps/utils/nonEnumerableTypes
* @see http://2ality.com/2011/07/js-properties.html
* @TODO https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
* `const getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors`
*
* @example
* var obj = {key: true}
* allProperties(obj)
* //=> ['key']
*
* @example
* class One {
* method() {}
* }
* class Two extends One {
* eh() {}
* }
* allProperties(new Two())
* //=> ['eh', 'method']
*
*/
function allProperties(obj) {
// .concat(keys(obj)) ?
return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj)).concat(keys(obj)); // const result = []
// for (const prop in obj) result.push(prop)
// return result
// flatten(getOwnPropertyNames, getOwnPropertySymbols)
// const proto = getPrototypeOf(obj)
// return [].concat(
// getOwnPropertyNames(obj),
// getOwnPropertySymbols(obj)
// // ObjectKeys(obj),
// // proto ? allProperties(proto) : []
// )
} // @TODO @HACK @FIXME - needs to be in another file
const NON_ASSIGNABLE = NON_ENUMERABLE.concat(['length', 'name']);
function walkAllProperties(obj, max = 1) {
let props = allProperties(obj);
forProto(obj, proto => concatMutate(props, allProperties(proto)), max);
return props.filter(key => !NON_ASSIGNABLE.includes(key)).filter(uniq);
}
const BUILT_IN_METHODS = NON_ASSIGNABLE.concat(['apply', 'bind', 'call']);
const BUILT_IN_FILTER = key => {
return !BUILT_IN_METHODS.includes(key) && !isSymbol(key) && key && !key.startsWith('$');
};
function walkAllCustomProperties(obj, max = 1) {
let props = allProperties(obj); // const iteratee = proto => {
// props = props.concat(allProperties(proto))
// }
// forProto(obj, iteratee, max)
return props.filter(BUILT_IN_FILTER).filter(uniq);
}
module.exports = walkAllCustomProperties;