UNPKG

6.2 kBMarkdownView Raw
1# class-utils [![NPM version](https://img.shields.io/npm/v/class-utils.svg)](https://www.npmjs.com/package/class-utils) [![Build Status](https://img.shields.io/travis/jonschlinkert/class-utils.svg)](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 class-utils --save
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## Related projects
236
237* [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. | [homepage](https://github.com/jonschlinkert/define-property)
238* [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://www.npmjs.com/package/delegate-properties) | [homepage](https://github.com/jonschlinkert/delegate-properties)
239* [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://www.npmjs.com/package/is-descriptor) | [homepage](https://github.com/jonschlinkert/is-descriptor)
240
241## Contributing
242
243Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/class-utils/issues/new).
244
245## Building docs
246
247Generate readme and API documentation with [verb](https://github.com/verbose/verb):
248
249```sh
250$ npm install verb && npm run docs
251```
252
253Or, if [verb](https://github.com/verbose/verb) is installed globally:
254
255```sh
256$ verb
257```
258
259## Running tests
260
261Install dev dependencies:
262
263```sh
264$ npm install -d && npm test
265```
266
267## Author
268
269**Jon Schlinkert**
270
271* [github/jonschlinkert](https://github.com/jonschlinkert)
272* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
273
274## License
275
276Copyright © 2016 [Jon Schlinkert](https://github.com/jonschlinkert)
277Released under the [MIT license](https://github.com/jonschlinkert/class-utils/blob/master/LICENSE).
278
279***
280
281_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on March 11, 2016._
\No newline at end of file