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