UNPKG

5.7 kBMarkdownView Raw
1# class-utils [![NPM version](https://badge.fury.io/js/class-utils.svg)](http://badge.fury.io/js/class-utils)
2
3> Utils for working with JavaScript classes and prototype methods.
4
5<!-- toc -->
6
7* [Install](#install)
8* [Usage](#usage)
9* [API](#api)
10* [Related projects](#related-projects)
11* [Running tests](#running-tests)
12* [Contributing](#contributing)
13* [Author](#author)
14* [License](#license)
15
16_(Table of contents generated by [verb](https://github.com/verbose/verb))_
17
18<!-- tocstop -->
19
20## Install
21
22Install with [npm](https://www.npmjs.com/)
23
24```sh
25$ npm i class-utils --save
26```
27
28## Usage
29
30```js
31var cu = require('class-utils');
32```
33
34## API
35
36### [.has](index.js#L37)
37
38Returns true if an array has any of the given elements, or an object has any of the give keys.
39
40**Params**
41
42* `obj` **{Object}**
43* `val` **{String|Array}**
44* `returns` **{Boolean}**
45
46**Example**
47
48```js
49cu.has(['a', 'b', 'c'], 'c');
50//=> true
51
52cu.has(['a', 'b', 'c'], ['c', 'z']);
53//=> true
54
55cu.has({a: 'b', c: 'd'}, ['c', 'z']);
56//=> true
57```
58
59### [.hasAll](index.js#L84)
60
61Returns true if an array or object has all of the given values.
62
63**Params**
64
65* `val` **{Object|Array}**
66* `values` **{String|Array}**
67* `returns` **{Boolean}**
68
69**Example**
70
71```js
72cu.hasAll(['a', 'b', 'c'], 'c');
73//=> true
74
75cu.hasAll(['a', 'b', 'c'], ['c', 'z']);
76//=> false
77
78cu.hasAll({a: 'b', c: 'd'}, ['c', 'z']);
79//=> false
80```
81
82### [.arrayify](index.js#L111)
83
84Cast the given value to an array.
85
86**Params**
87
88* `val` **{String|Array}**
89* `returns` **{Array}**
90
91**Example**
92
93```js
94cu.arrayify('foo');
95//=> ['foo']
96
97cu.arrayify(['foo']);
98//=> ['foo']
99```
100
101### [.hasConstructor](index.js#L146)
102
103Returns true if a value has a `contructor`
104
105**Params**
106
107* `value` **{Object}**
108* `returns` **{Boolean}**
109
110**Example**
111
112```js
113cu.hasConstructor({});
114//=> true
115
116cu.hasConstructor(Object.create(null));
117//=> false
118```
119
120### [.nativeKeys](index.js#L168)
121
122Get the native `ownPropertyNames` from the constructor of the given `object`. An empty array is returned if the object does not have a constructor.
123
124**Params**
125
126* `obj` **{Object}**: Object that has a `constructor`.
127* `returns` **{Array}**: Array of keys.
128
129**Example**
130
131```js
132cu.nativeKeys({a: 'b', b: 'c', c: 'd'})
133//=> ['a', 'b', 'c']
134
135cu.nativeKeys(function(){})
136//=> ['length', 'caller']
137```
138
139### [.getDescriptor](index.js#L200)
140
141Returns property descriptor `key` if it's an "own" property of the given object.
142
143**Params**
144
145* `obj` **{Object}**
146* `key` **{String}**
147* `returns` **{Object}**: Returns descriptor `key`
148
149**Example**
150
151```js
152function App() {}
153Object.defineProperty(App.prototype, 'count', {
154 get: function() {
155 return Object.keys(this).length;
156 }
157});
158cu.getDescriptor(App.prototype, 'count');
159// returns:
160// {
161// get: [Function],
162// set: undefined,
163// enumerable: false,
164// configurable: false
165// }
166```
167
168### [.copyDescriptor](index.js#L230)
169
170Copy a descriptor from one object to another.
171
172**Params**
173
174* `receiver` **{Object}**
175* `provider` **{Object}**
176* `name` **{String}**
177* `returns` **{Object}**
178
179**Example**
180
181```js
182function App() {}
183Object.defineProperty(App.prototype, 'count', {
184 get: function() {
185 return Object.keys(this).length;
186 }
187});
188var obj = {};
189cu.copyDescriptor(obj, App.prototype, 'count');
190```
191
192### [.copy](index.js#L255)
193
194Copy static properties, prototype properties, and descriptors
195from one object to another.
196
197**Params**
198
199* `receiver` **{Object}**
200* `provider` **{Object}**
201* `omit` **{String|Array}**: One or more properties to omit
202* `returns` **{Object}**
203
204### [.inherit](index.js#L289)
205
206Inherit the static properties, prototype properties, and descriptors
207from of an object.
208
209**Params**
210
211* `receiver` **{Object}**
212* `provider` **{Object}**
213* `omit` **{String|Array}**: One or more properties to omit
214* `returns` **{Object}**
215
216### [.extend](index.js#L332)
217
218Returns a function for extending the static properties, prototype properties, and descriptors from the `Parent` constructor onto `Child` constructors.
219
220**Params**
221
222* `Parent` **{Function}**: Parent ctor
223* `Child` **{Function}**: Child ctor
224* `proto` **{Object}**: Optionally pass additional prototype properties to inherit.
225* `returns` **{Object}**
226
227**Example**
228
229```js
230var extend = cu.extend(Parent);
231Parent.extend(Child);
232
233// optional methods
234Parent.extend(Child, {
235 foo: function() {},
236 bar: function() {}
237});
238```
239
240## Related projects
241
242* [define-property](https://www.npmjs.com/package/define-property): Define a non-enumerable property on an object. | [homepage](https://github.com/jonschlinkert/define-property)
243* [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)
244* [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)
245
246## Running tests
247
248Install dev dependencies:
249
250```sh
251$ npm i -d && npm test
252```
253
254## Contributing
255
256Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/class-utils/issues/new).
257
258## Author
259
260**Jon Schlinkert**
261
262+ [github/jonschlinkert](https://github.com/jonschlinkert)
263+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
264
265## License
266
267Copyright © 2015 Jon Schlinkert
268Released under the MIT license.
269
270***
271
272_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on September 15, 2015._