index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. 'use strict';
  2. var util = require('util');
  3. var union = require('arr-union');
  4. var define = require('define-property');
  5. var staticExtend = require('static-extend');
  6. var isObj = require('isobject');
  7. /**
  8. * Expose class utils
  9. */
  10. var cu = module.exports;
  11. /**
  12. * Expose class utils: `cu`
  13. */
  14. cu.isObject = function isObject(val) {
  15. return isObj(val) || typeof val === 'function';
  16. };
  17. /**
  18. * Returns true if an array has any of the given elements, or an
  19. * object has any of the give keys.
  20. *
  21. * ```js
  22. * cu.has(['a', 'b', 'c'], 'c');
  23. * //=> true
  24. *
  25. * cu.has(['a', 'b', 'c'], ['c', 'z']);
  26. * //=> true
  27. *
  28. * cu.has({a: 'b', c: 'd'}, ['c', 'z']);
  29. * //=> true
  30. * ```
  31. * @param {Object} `obj`
  32. * @param {String|Array} `val`
  33. * @return {Boolean}
  34. * @api public
  35. */
  36. cu.has = function has(obj, val) {
  37. val = cu.arrayify(val);
  38. var len = val.length;
  39. if (cu.isObject(obj)) {
  40. for (var key in obj) {
  41. if (val.indexOf(key) > -1) {
  42. return true;
  43. }
  44. }
  45. var keys = cu.nativeKeys(obj);
  46. return cu.has(keys, val);
  47. }
  48. if (Array.isArray(obj)) {
  49. var arr = obj;
  50. while (len--) {
  51. if (arr.indexOf(val[len]) > -1) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. throw new TypeError('expected an array or object.');
  58. };
  59. /**
  60. * Returns true if an array or object has all of the given values.
  61. *
  62. * ```js
  63. * cu.hasAll(['a', 'b', 'c'], 'c');
  64. * //=> true
  65. *
  66. * cu.hasAll(['a', 'b', 'c'], ['c', 'z']);
  67. * //=> false
  68. *
  69. * cu.hasAll({a: 'b', c: 'd'}, ['c', 'z']);
  70. * //=> false
  71. * ```
  72. * @param {Object|Array} `val`
  73. * @param {String|Array} `values`
  74. * @return {Boolean}
  75. * @api public
  76. */
  77. cu.hasAll = function hasAll(val, values) {
  78. values = cu.arrayify(values);
  79. var len = values.length;
  80. while (len--) {
  81. if (!cu.has(val, values[len])) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. };
  87. /**
  88. * Cast the given value to an array.
  89. *
  90. * ```js
  91. * cu.arrayify('foo');
  92. * //=> ['foo']
  93. *
  94. * cu.arrayify(['foo']);
  95. * //=> ['foo']
  96. * ```
  97. *
  98. * @param {String|Array} `val`
  99. * @return {Array}
  100. * @api public
  101. */
  102. cu.arrayify = function arrayify(val) {
  103. return val ? (Array.isArray(val) ? val : [val]) : [];
  104. };
  105. /**
  106. * Noop
  107. */
  108. cu.noop = function noop() {
  109. return;
  110. };
  111. /**
  112. * Returns the first argument passed to the function.
  113. */
  114. cu.identity = function identity(val) {
  115. return val;
  116. };
  117. /**
  118. * Returns true if a value has a `contructor`
  119. *
  120. * ```js
  121. * cu.hasConstructor({});
  122. * //=> true
  123. *
  124. * cu.hasConstructor(Object.create(null));
  125. * //=> false
  126. * ```
  127. * @param {Object} `value`
  128. * @return {Boolean}
  129. * @api public
  130. */
  131. cu.hasConstructor = function hasConstructor(val) {
  132. return cu.isObject(val) && typeof val.constructor !== 'undefined';
  133. };
  134. /**
  135. * Get the native `ownPropertyNames` from the constructor of the
  136. * given `object`. An empty array is returned if the object does
  137. * not have a constructor.
  138. *
  139. * ```js
  140. * cu.nativeKeys({a: 'b', b: 'c', c: 'd'})
  141. * //=> ['a', 'b', 'c']
  142. *
  143. * cu.nativeKeys(function(){})
  144. * //=> ['length', 'caller']
  145. * ```
  146. *
  147. * @param {Object} `obj` Object that has a `constructor`.
  148. * @return {Array} Array of keys.
  149. * @api public
  150. */
  151. cu.nativeKeys = function nativeKeys(val) {
  152. if (!cu.hasConstructor(val)) return [];
  153. var keys = Object.getOwnPropertyNames(val);
  154. if ('caller' in val) keys.push('caller');
  155. return keys;
  156. };
  157. /**
  158. * Returns property descriptor `key` if it's an "own" property
  159. * of the given object.
  160. *
  161. * ```js
  162. * function App() {}
  163. * Object.defineProperty(App.prototype, 'count', {
  164. * get: function() {
  165. * return Object.keys(this).length;
  166. * }
  167. * });
  168. * cu.getDescriptor(App.prototype, 'count');
  169. * // returns:
  170. * // {
  171. * // get: [Function],
  172. * // set: undefined,
  173. * // enumerable: false,
  174. * // configurable: false
  175. * // }
  176. * ```
  177. *
  178. * @param {Object} `obj`
  179. * @param {String} `key`
  180. * @return {Object} Returns descriptor `key`
  181. * @api public
  182. */
  183. cu.getDescriptor = function getDescriptor(obj, key) {
  184. if (!cu.isObject(obj)) {
  185. throw new TypeError('expected an object.');
  186. }
  187. if (typeof key !== 'string') {
  188. throw new TypeError('expected key to be a string.');
  189. }
  190. return Object.getOwnPropertyDescriptor(obj, key);
  191. };
  192. /**
  193. * Copy a descriptor from one object to another.
  194. *
  195. * ```js
  196. * function App() {}
  197. * Object.defineProperty(App.prototype, 'count', {
  198. * get: function() {
  199. * return Object.keys(this).length;
  200. * }
  201. * });
  202. * var obj = {};
  203. * cu.copyDescriptor(obj, App.prototype, 'count');
  204. * ```
  205. * @param {Object} `receiver`
  206. * @param {Object} `provider`
  207. * @param {String} `name`
  208. * @return {Object}
  209. * @api public
  210. */
  211. cu.copyDescriptor = function copyDescriptor(receiver, provider, name) {
  212. if (!cu.isObject(receiver)) {
  213. throw new TypeError('expected receiving object to be an object.');
  214. }
  215. if (!cu.isObject(provider)) {
  216. throw new TypeError('expected providing object to be an object.');
  217. }
  218. if (typeof name !== 'string') {
  219. throw new TypeError('expected name to be a string.');
  220. }
  221. var val = cu.getDescriptor(provider, name);
  222. if (val) Object.defineProperty(receiver, name, val);
  223. };
  224. /**
  225. * Copy static properties, prototype properties, and descriptors
  226. * from one object to another.
  227. *
  228. * @param {Object} `receiver`
  229. * @param {Object} `provider`
  230. * @param {String|Array} `omit` One or more properties to omit
  231. * @return {Object}
  232. * @api public
  233. */
  234. cu.copy = function copy(receiver, provider, omit) {
  235. if (!cu.isObject(receiver)) {
  236. throw new TypeError('expected receiving object to be an object.');
  237. }
  238. if (!cu.isObject(provider)) {
  239. throw new TypeError('expected providing object to be an object.');
  240. }
  241. var props = Object.getOwnPropertyNames(provider);
  242. var keys = Object.keys(provider);
  243. var len = props.length,
  244. key;
  245. omit = cu.arrayify(omit);
  246. while (len--) {
  247. key = props[len];
  248. if (cu.has(keys, key)) {
  249. define(receiver, key, provider[key]);
  250. } else if (!(key in receiver) && !cu.has(omit, key)) {
  251. cu.copyDescriptor(receiver, provider, key);
  252. }
  253. }
  254. };
  255. /**
  256. * Inherit the static properties, prototype properties, and descriptors
  257. * from of an object.
  258. *
  259. * @param {Object} `receiver`
  260. * @param {Object} `provider`
  261. * @param {String|Array} `omit` One or more properties to omit
  262. * @return {Object}
  263. * @api public
  264. */
  265. cu.inherit = function inherit(receiver, provider, omit) {
  266. if (!cu.isObject(receiver)) {
  267. throw new TypeError('expected receiving object to be an object.');
  268. }
  269. if (!cu.isObject(provider)) {
  270. throw new TypeError('expected providing object to be an object.');
  271. }
  272. var keys = [];
  273. for (var key in provider) {
  274. keys.push(key);
  275. receiver[key] = provider[key];
  276. }
  277. keys = keys.concat(cu.arrayify(omit));
  278. var a = provider.prototype || provider;
  279. var b = receiver.prototype || receiver;
  280. cu.copy(b, a, keys);
  281. };
  282. /**
  283. * Returns a function for extending the static properties,
  284. * prototype properties, and descriptors from the `Parent`
  285. * constructor onto `Child` constructors.
  286. *
  287. * ```js
  288. * var extend = cu.extend(Parent);
  289. * Parent.extend(Child);
  290. *
  291. * // optional methods
  292. * Parent.extend(Child, {
  293. * foo: function() {},
  294. * bar: function() {}
  295. * });
  296. * ```
  297. * @param {Function} `Parent` Parent ctor
  298. * @param {Function} `extend` Optional extend function to handle custom extensions. Useful when updating methods that require a specific prototype.
  299. * @param {Function} `Child` Child ctor
  300. * @param {Object} `proto` Optionally pass additional prototype properties to inherit.
  301. * @return {Object}
  302. * @api public
  303. */
  304. cu.extend = function() {
  305. // keep it lazy, instead of assigning to `cu.extend`
  306. return staticExtend.apply(null, arguments);
  307. };
  308. /**
  309. * Bubble up events emitted from static methods on the Parent ctor.
  310. *
  311. * @param {Object} `Parent`
  312. * @param {Array} `events` Event names to bubble up
  313. * @api public
  314. */
  315. cu.bubble = function(Parent, events) {
  316. events = events || [];
  317. Parent.bubble = function(Child, arr) {
  318. if (Array.isArray(arr)) {
  319. events = union([], events, arr);
  320. }
  321. var len = events.length;
  322. var idx = -1;
  323. while (++idx < len) {
  324. var name = events[idx];
  325. Parent.on(name, Child.emit.bind(Child, name));
  326. }
  327. cu.bubble(Child, events);
  328. };
  329. };