1# class-utils [![NPM version](https://img.shields.io/npm/v/class-utils.svg?style=flat)](https://www.npmjs.com/package/class-utils) [![NPM monthly downloads](https://img.shields.io/npm/dm/class-utils.svg?style=flat)](https://npmjs.org/package/class-utils)  [![NPM total downloads](https://img.shields.io/npm/dt/class-utils.svg?style=flat)](https://npmjs.org/package/class-utils) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/class-utils.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/class-utils)
2
3> Utils for working with JavaScript classes and prototype methods.
4
5## Install
6
7Install with [npm](https://www.npmjs.com/):
8
9```sh
10$ npm install --save class-utils
11```
12
13## Usage
14
15```js
16var cu = require('class-utils');
17```
18
19## API
20
21### [.has](index.js#L40)
22
23Returns true if an array has any of the given elements, or an object has any of the give keys.
24
25**Params**
26
27* `obj` **{Object}**
28* `val` **{String|Array}**
29* `returns` **{Boolean}**
30
31**Example**
32
33```js
34cu.has(['a', 'b', 'c'], 'c');
35//=> true
36
37cu.has(['a', 'b', 'c'], ['c', 'z']);
38//=> true
39
40cu.has({a: 'b', c: 'd'}, ['c', 'z']);
41//=> true
42```
43
44### [.hasAll](index.js#L87)
45
46Returns true if an array or object has all of the given values.
47
48**Params**
49
50* `val` **{Object|Array}**
51* `values` **{String|Array}**
52* `returns` **{Boolean}**
53
54**Example**
55
56```js
57cu.hasAll(['a', 'b', 'c'], 'c');
58//=> true
59
60cu.hasAll(['a', 'b', 'c'], ['c', 'z']);
61//=> false
62
63cu.hasAll({a: 'b', c: 'd'}, ['c', 'z']);
64//=> false
65```
66
67### [.arrayify](index.js#L114)
68
69Cast the given value to an array.
70
71**Params**
72
73* `val` **{String|Array}**
74* `returns` **{Array}**
75
76**Example**
77
78```js
79cu.arrayify('foo');
80//=> ['foo']
81
82cu.arrayify(['foo']);
83//=> ['foo']
84```
85
86### [.hasConstructor](index.js#L149)
87
88Returns true if a value has a `contructor`
89
90**Params**
91
92* `value` **{Object}**
93* `returns` **{Boolean}**
94
95**Example**
96
97```js
98cu.hasConstructor({});
99//=> true
100
101cu.hasConstructor(Object.create(null));
102//=> false
103```
104
105### [.nativeKeys](index.js#L171)
106
107Get the native `ownPropertyNames` from the constructor of the given `object`. An empty array is returned if the object does not have a constructor.
108
109**Params**
110
111* `obj` **{Object}**: Object that has a `constructor`.
112* `returns` **{Array}**: Array of keys.
113
114**Example**
115
116```js
117cu.nativeKeys({a: 'b', b: 'c', c: 'd'})
118//=> ['a', 'b', 'c']
119
120cu.nativeKeys(function(){})
121//=> ['length', 'caller']
122```
123
124### [.getDescriptor](index.js#L203)
125
126Returns property descriptor `key` if it's an "own" property of the given object.
127
128**Params**
129
130* `obj` **{Object}**
131* `key` **{String}**
132* `returns` **{Object}**: Returns descriptor `key`
133
134**Example**
135
136```js
137function App() {}
138Object.defineProperty(App.prototype, 'count', {
139  get: function() {
140    return Object.keys(this).length;
141  }
142});
143cu.getDescriptor(App.prototype, 'count');
144// returns:
145// {
146//   get: [Function],
147//   set: undefined,
148//   enumerable: false,
149//   configurable: false
150// }
151```
152
153### [.copyDescriptor](index.js#L233)
154
155Copy a descriptor from one object to another.
156
157**Params**
158
159* `receiver` **{Object}**
160* `provider` **{Object}**
161* `name` **{String}**
162* `returns` **{Object}**
163
164**Example**
165
166```js
167function App() {}
168Object.defineProperty(App.prototype, 'count', {
169  get: function() {
170    return Object.keys(this).length;
171  }
172});
173var obj = {};
174cu.copyDescriptor(obj, App.prototype, 'count');
175```
176
177### [.copy](index.js#L259)
178
179Copy static properties, prototype properties, and descriptors
180from one object to another.
181
182**Params**
183
184* `receiver` **{Object}**
185* `provider` **{Object}**
186* `omit` **{String|Array}**: One or more properties to omit
187* `returns` **{Object}**
188
189### [.inherit](index.js#L294)
190
191Inherit the static properties, prototype properties, and descriptors
192from of an object.
193
194**Params**
195
196* `receiver` **{Object}**
197* `provider` **{Object}**
198* `omit` **{String|Array}**: One or more properties to omit
199* `returns` **{Object}**
200
201### [.extend](index.js#L338)
202
203Returns a function for extending the static properties, prototype properties, and descriptors from the `Parent` constructor onto `Child` constructors.
204
205**Params**
206
207* `Parent` **{Function}**: Parent ctor
208* `extend` **{Function}**: Optional extend function to handle custom extensions. Useful when updating methods that require a specific prototype.
209* `Child` **{Function}**: Child ctor
210* `proto` **{Object}**: Optionally pass additional prototype properties to inherit.
211* `returns` **{Object}**
212
213**Example**
214
215```js
216var extend = cu.extend(Parent);
217Parent.extend(Child);
218
219// optional methods
220Parent.extend(Child, {
221  foo: function() {},
222  bar: function() {}
223});
224```
225
226### [.bubble](index.js#L351)
227
228Bubble up events emitted from static methods on the Parent ctor.
229
230**Params**
231
232* `Parent` **{Object}**
233* `events` **{Array}**: Event names to bubble up
234
235## About
236
237### Related projects
238
239* [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. | [homepage](https://github.com/jonschlinkert/define-property)
240* [delegate-properties](https://www.npmjs.com/package/delegate-properties): Deep-clone properties from one object to another and make them non-enumerable, or make existing properties… [more](https://github.com/jonschlinkert/delegate-properties) | [homepage](https://github.com/jonschlinkert/delegate-properties)
241* [is-descriptor](https://www.npmjs.com/package/is-descriptor): Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… [more](https://github.com/jonschlinkert/is-descriptor) | [homepage](https://github.com/jonschlinkert/is-descriptor)
242
243### Contributing
244
245Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
246
247### Contributors
248
249| **Commits** | **Contributor** |
250| --- | --- |
251| 32 | [jonschlinkert](https://github.com/jonschlinkert) |
252| 8  | [doowb](https://github.com/doowb) |
253| 2  | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
254
255### Building docs
256
257_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
258
259To generate the readme, run the following command:
260
261```sh
262$ npm install -g verbose/verb#dev verb-generate-readme && verb
263```
264
265### Running tests
266
267Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
268
269```sh
270$ npm install && npm test
271```
272
273### Author
274
275**Jon Schlinkert**
276
277* [github/jonschlinkert](https://github.com/jonschlinkert)
278* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
279
280### License
281
282Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
283Released under the [MIT License](LICENSE).
284
285***
286
287_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 25, 2017._