https://github.com/chrisyip/node-class-keys
Get keys of an instance of any class
https://github.com/chrisyip/node-class-keys
nodejs nodejs-modules
Last synced: about 2 hours ago
JSON representation
Get keys of an instance of any class
- Host: GitHub
- URL: https://github.com/chrisyip/node-class-keys
- Owner: chrisyip
- License: mit
- Created: 2017-02-26T17:30:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-26T18:23:54.000Z (over 9 years ago)
- Last Synced: 2025-09-04T10:02:10.632Z (10 months ago)
- Topics: nodejs, nodejs-modules
- Language: JavaScript
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-class-keys
[![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Travis CI][travis-image]][travis-url] [![codecov][codecov-image]](codecov-url)
Get keys of an instance of any class. Uses `Object.getOwnPropertyNames` and `Object.getOwnPropertySymbols`.
Usage:
```
npm i class-keys
```
```js
import classKeys from 'class-keys'
const keys = classKeys(instance)
```
## Why?
Because:
```
class Foo {
constructor () {
this.baz = 'baz'
}
bar () {}
[Symbol.for('foo')] () {}
}
const foo = new Foo()
Object.keys(foo) // []
Object.getOwnPropertyNames(foo) // []
Object.getOwnPropertySymbols(foo) // []
```
With `class-keys`:
```
class Foo {
constructor () {
this.baz = 'baz'
}
bar () {}
[Symbol.for('foo')] () {}
}
const foo = new Foo()
classKeys(foo) // ['baz', 'constructor', 'bar', Symbol(foo)]
```
## Warning
**`class-keys` uses instance's prototype to detect non-enumerable properties, so results may not match your expectation.**
```js
classKeys({}) // [ '__defineGetter__', '__defineSetter__', 'hasOwnProperty', '__lookupGetter__', '__lookupSetter__', 'propertyIsEnumerable', 'constructor', 'toString', 'toLocaleString', 'valueOf', 'isPrototypeOf', '__proto__']
classKeys(new Date()) // ['constructor', 'toString', 'toDateString', 'toTimeString', ... 'toLocaleDateString', 'toLocaleTimeString', Symbol(Symbol.toPrimitive)]
```
[npm-url]: https://npmjs.org/package/class-keys
[npm-image]: http://img.shields.io/npm/v/class-keys.svg?style=flat-square
[daviddm-url]: https://david-dm.org/chrisyip/node-class-keys
[daviddm-image]: http://img.shields.io/david/chrisyip/node-class-keys.svg?style=flat-square
[travis-url]: https://travis-ci.org/chrisyip/node-class-keys
[travis-image]: http://img.shields.io/travis/chrisyip/node-class-keys.svg?style=flat-square
[codecov-url]: https://codecov.io/gh/chrisyip/node-class-keys
[codecov-image]: https://img.shields.io/codecov/c/github/chrisyip/node-class-keys.svg?style=flat-square