You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
7.7 KiB

4 years ago
  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. > Utils for working with JavaScript classes and prototype methods.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save class-utils
  8. ```
  9. ## Usage
  10. ```js
  11. var cu = require('class-utils');
  12. ```
  13. ## API
  14. ### [.has](index.js#L43)
  15. Returns true if an array has any of the given elements, or an object has any of the give keys.
  16. **Params**
  17. * `obj` **{Object}**
  18. * `val` **{String|Array}**
  19. * `returns` **{Boolean}**
  20. **Example**
  21. ```js
  22. cu.has(['a', 'b', 'c'], 'c');
  23. //=> true
  24. cu.has(['a', 'b', 'c'], ['c', 'z']);
  25. //=> true
  26. cu.has({a: 'b', c: 'd'}, ['c', 'z']);
  27. //=> true
  28. ```
  29. ### [.hasAll](index.js#L90)
  30. Returns true if an array or object has all of the given values.
  31. **Params**
  32. * `val` **{Object|Array}**
  33. * `values` **{String|Array}**
  34. * `returns` **{Boolean}**
  35. **Example**
  36. ```js
  37. cu.hasAll(['a', 'b', 'c'], 'c');
  38. //=> true
  39. cu.hasAll(['a', 'b', 'c'], ['c', 'z']);
  40. //=> false
  41. cu.hasAll({a: 'b', c: 'd'}, ['c', 'z']);
  42. //=> false
  43. ```
  44. ### [.arrayify](index.js#L117)
  45. Cast the given value to an array.
  46. **Params**
  47. * `val` **{String|Array}**
  48. * `returns` **{Array}**
  49. **Example**
  50. ```js
  51. cu.arrayify('foo');
  52. //=> ['foo']
  53. cu.arrayify(['foo']);
  54. //=> ['foo']
  55. ```
  56. ### [.hasConstructor](index.js#L152)
  57. Returns true if a value has a `contructor`
  58. **Params**
  59. * `value` **{Object}**
  60. * `returns` **{Boolean}**
  61. **Example**
  62. ```js
  63. cu.hasConstructor({});
  64. //=> true
  65. cu.hasConstructor(Object.create(null));
  66. //=> false
  67. ```
  68. ### [.nativeKeys](index.js#L174)
  69. Get the native `ownPropertyNames` from the constructor of the given `object`. An empty array is returned if the object does not have a constructor.
  70. **Params**
  71. * `obj` **{Object}**: Object that has a `constructor`.
  72. * `returns` **{Array}**: Array of keys.
  73. **Example**
  74. ```js
  75. cu.nativeKeys({a: 'b', b: 'c', c: 'd'})
  76. //=> ['a', 'b', 'c']
  77. cu.nativeKeys(function(){})
  78. //=> ['length', 'caller']
  79. ```
  80. ### [.getDescriptor](index.js#L208)
  81. Returns property descriptor `key` if it's an "own" property of the given object.
  82. **Params**
  83. * `obj` **{Object}**
  84. * `key` **{String}**
  85. * `returns` **{Object}**: Returns descriptor `key`
  86. **Example**
  87. ```js
  88. function App() {}
  89. Object.defineProperty(App.prototype, 'count', {
  90. get: function() {
  91. return Object.keys(this).length;
  92. }
  93. });
  94. cu.getDescriptor(App.prototype, 'count');
  95. // returns:
  96. // {
  97. // get: [Function],
  98. // set: undefined,
  99. // enumerable: false,
  100. // configurable: false
  101. // }
  102. ```
  103. ### [.copyDescriptor](index.js#L238)
  104. Copy a descriptor from one object to another.
  105. **Params**
  106. * `receiver` **{Object}**
  107. * `provider` **{Object}**
  108. * `name` **{String}**
  109. * `returns` **{Object}**
  110. **Example**
  111. ```js
  112. function App() {}
  113. Object.defineProperty(App.prototype, 'count', {
  114. get: function() {
  115. return Object.keys(this).length;
  116. }
  117. });
  118. var obj = {};
  119. cu.copyDescriptor(obj, App.prototype, 'count');
  120. ```
  121. ### [.copy](index.js#L264)
  122. Copy static properties, prototype properties, and descriptors
  123. from one object to another.
  124. **Params**
  125. * `receiver` **{Object}**
  126. * `provider` **{Object}**
  127. * `omit` **{String|Array}**: One or more properties to omit
  128. * `returns` **{Object}**
  129. ### [.inherit](index.js#L299)
  130. Inherit the static properties, prototype properties, and descriptors
  131. from of an object.
  132. **Params**
  133. * `receiver` **{Object}**
  134. * `provider` **{Object}**
  135. * `omit` **{String|Array}**: One or more properties to omit
  136. * `returns` **{Object}**
  137. ### [.extend](index.js#L343)
  138. Returns a function for extending the static properties, prototype properties, and descriptors from the `Parent` constructor onto `Child` constructors.
  139. **Params**
  140. * `Parent` **{Function}**: Parent ctor
  141. * `extend` **{Function}**: Optional extend function to handle custom extensions. Useful when updating methods that require a specific prototype.
  142. * `Child` **{Function}**: Child ctor
  143. * `proto` **{Object}**: Optionally pass additional prototype properties to inherit.
  144. * `returns` **{Object}**
  145. **Example**
  146. ```js
  147. var extend = cu.extend(Parent);
  148. Parent.extend(Child);
  149. // optional methods
  150. Parent.extend(Child, {
  151. foo: function() {},
  152. bar: function() {}
  153. });
  154. ```
  155. ### [.bubble](index.js#L356)
  156. Bubble up events emitted from static methods on the Parent ctor.
  157. **Params**
  158. * `Parent` **{Object}**
  159. * `events` **{Array}**: Event names to bubble up
  160. ## About
  161. <details>
  162. <summary><strong>Contributing</strong></summary>
  163. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  164. </details>
  165. <details>
  166. <summary><strong>Running Tests</strong></summary>
  167. 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:
  168. ```sh
  169. $ npm install && npm test
  170. ```
  171. </details>
  172. <details>
  173. <summary><strong>Building docs</strong></summary>
  174. _(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.)_
  175. To generate the readme, run the following command:
  176. ```sh
  177. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  178. ```
  179. </details>
  180. ### Related projects
  181. You might also be interested in these projects:
  182. * [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.")
  183. * [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.")
  184. * [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.")
  185. ### Contributors
  186. | **Commits** | **Contributor** |
  187. | --- | --- |
  188. | 34 | [jonschlinkert](https://github.com/jonschlinkert) |
  189. | 8 | [doowb](https://github.com/doowb) |
  190. | 2 | [wtgtybhertgeghgtwtg](https://github.com/wtgtybhertgeghgtwtg) |
  191. ### Author
  192. **Jon Schlinkert**
  193. * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
  194. * [github/jonschlinkert](https://github.com/jonschlinkert)
  195. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  196. ### License
  197. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  198. Released under the [MIT License](LICENSE).
  199. ***
  200. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on January 11, 2018._