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 |
|
7 | Install with [npm](https://www.npmjs.com/):
|
8 |
|
9 | ```sh
|
10 | $ npm install class-utils --save
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```js
|
16 | var cu = require('class-utils');
|
17 | ```
|
18 |
|
19 | ## API
|
20 |
|
21 | ### [.has](index.js#L40)
|
22 |
|
23 | Returns 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
|
34 | cu.has(['a', 'b', 'c'], 'c');
|
35 | //=> true
|
36 |
|
37 | cu.has(['a', 'b', 'c'], ['c', 'z']);
|
38 | //=> true
|
39 |
|
40 | cu.has({a: 'b', c: 'd'}, ['c', 'z']);
|
41 | //=> true
|
42 | ```
|
43 |
|
44 | ### [.hasAll](index.js#L87)
|
45 |
|
46 | Returns 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
|
57 | cu.hasAll(['a', 'b', 'c'], 'c');
|
58 | //=> true
|
59 |
|
60 | cu.hasAll(['a', 'b', 'c'], ['c', 'z']);
|
61 | //=> false
|
62 |
|
63 | cu.hasAll({a: 'b', c: 'd'}, ['c', 'z']);
|
64 | //=> false
|
65 | ```
|
66 |
|
67 | ### [.arrayify](index.js#L114)
|
68 |
|
69 | Cast the given value to an array.
|
70 |
|
71 | **Params**
|
72 |
|
73 | * `val` **{String|Array}**
|
74 | * `returns` **{Array}**
|
75 |
|
76 | **Example**
|
77 |
|
78 | ```js
|
79 | cu.arrayify('foo');
|
80 | //=> ['foo']
|
81 |
|
82 | cu.arrayify(['foo']);
|
83 | //=> ['foo']
|
84 | ```
|
85 |
|
86 | ### [.hasConstructor](index.js#L149)
|
87 |
|
88 | Returns true if a value has a `contructor`
|
89 |
|
90 | **Params**
|
91 |
|
92 | * `value` **{Object}**
|
93 | * `returns` **{Boolean}**
|
94 |
|
95 | **Example**
|
96 |
|
97 | ```js
|
98 | cu.hasConstructor({});
|
99 | //=> true
|
100 |
|
101 | cu.hasConstructor(Object.create(null));
|
102 | //=> false
|
103 | ```
|
104 |
|
105 | ### [.nativeKeys](index.js#L171)
|
106 |
|
107 | Get 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
|
117 | cu.nativeKeys({a: 'b', b: 'c', c: 'd'})
|
118 | //=> ['a', 'b', 'c']
|
119 |
|
120 | cu.nativeKeys(function(){})
|
121 | //=> ['length', 'caller']
|
122 | ```
|
123 |
|
124 | ### [.getDescriptor](index.js#L203)
|
125 |
|
126 | Returns 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
|
137 | function App() {}
|
138 | Object.defineProperty(App.prototype, 'count', {
|
139 | get: function() {
|
140 | return Object.keys(this).length;
|
141 | }
|
142 | });
|
143 | cu.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 |
|
155 | Copy 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
|
167 | function App() {}
|
168 | Object.defineProperty(App.prototype, 'count', {
|
169 | get: function() {
|
170 | return Object.keys(this).length;
|
171 | }
|
172 | });
|
173 | var obj = {};
|
174 | cu.copyDescriptor(obj, App.prototype, 'count');
|
175 | ```
|
176 |
|
177 | ### [.copy](index.js#L259)
|
178 |
|
179 | Copy static properties, prototype properties, and descriptors
|
180 | from 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 |
|
191 | Inherit the static properties, prototype properties, and descriptors
|
192 | from 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 |
|
203 | Returns 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
|
216 | var extend = cu.extend(Parent);
|
217 | Parent.extend(Child);
|
218 |
|
219 | // optional methods
|
220 | Parent.extend(Child, {
|
221 | foo: function() {},
|
222 | bar: function() {}
|
223 | });
|
224 | ```
|
225 |
|
226 | ### [.bubble](index.js#L351)
|
227 |
|
228 | Bubble 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 |
|
243 | Pull 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 |
|
247 | Generate readme and API documentation with [verb](https://github.com/verbose/verb):
|
248 |
|
249 | ```sh
|
250 | $ npm install verb && npm run docs
|
251 | ```
|
252 |
|
253 | Or, if [verb](https://github.com/verbose/verb) is installed globally:
|
254 |
|
255 | ```sh
|
256 | $ verb
|
257 | ```
|
258 |
|
259 | ## Running tests
|
260 |
|
261 | Install 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 |
|
276 | Copyright © 2016 [Jon Schlinkert](https://github.com/jonschlinkert)
|
277 | Released 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 |