Skip to content

Recursive approach to Object getOwnPropertyNames and getOwnPropertySymbols

License

Notifications You must be signed in to change notification settings

gingur/object-props

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

object-props

Build Status Coverage Status GitHub license

NPM

Why

The example below exports a class Example. The class has several static properties, defined on multiple levels of the prototype chain but they're unfortunately not enummarable.

const symbol1 = Symbol('test1');
const symbol2 = Symbol('test2');

class Base {
  static test1 = '...';
  static test2() {
    // ...
  }
}
Base[symbol1] = '...'

class Example extends Base {
  static test3 = '...';
  static test4() {
    // ...
  }
}
Example[symbol2] = '...'

Thankfully Object.getOwnPropertyNames and Object.getOwnPropertySymbols is able to list these properties however it does not support recursive operations.

Object.getOwnPropertyNames(Example); // 5 Array ["length", "name", "prototype", "test4", "test3"]

Object.getOwnPropertySymbols(Example); // 1 Array [Symbol(test2)]

Hence, object-props was created to simply get ALL or OWN (explicitly defined and not inherited from the Object.prototype)

getAllProps

Returns an Array of names for the object and all its inherited properties.

const { getAllProps } from 'object-props';

getAllProps(Example); // 24 Array ["test4", "test3", "test2", "test1", ...all]

getAllPropsAndSymbols

Returns an Array of names and Symbols for the object and all its inherited properties.

const { getAllPropsAndSymbols } from 'object-props';

getAllPropsAndSymbols(Example); // 27 Array ["test4", "test3", Symbol(test2), "test2", "test1", Symbol(test1), ...all]

getOwnProps

Returns an Array of names for the object and its inherited properties excluding Object prototype.

const { getOwnProps } from 'object-props';

getOwnProps(Example); // 7 Array ["length", "name", "prototype", "test4", "test3", "test2", "test1"]

getOwnPropsAndSymbols

Returns an Array of names and Symbols for the object and all its inherited properties excluding Object prototype.

const { getOwnPropsAndSymbols } from 'object-props';

getOwnPropsAndSymbols(Example); // 9 Array ["length", "name", "prototype", "test4", "test3", Symbol(test2), "test2", "test1", Symbol(test1)]

About

Recursive approach to Object getOwnPropertyNames and getOwnPropertySymbols

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published