all files / lib/ class-utils.js

41.18% Statements 7/17
0% Branches 0/6
50% Functions 1/2
43.75% Lines 7/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                          
var _ = require('lodash')
  , util = require('util');
 
/**
 * Makes the `Constructor` inherit `SuperConstructor`.
 *
 * Calls node.js `util.inherits` but also copies the "static" properties from
 * `SuperConstructor` to `Constructor`.
 *
 * @param {function} Constructor
 * @param {function} SuperConstructor
 */
module.exports.inherits = function(Constructor, SuperConstructor) {
  var keys = Object.keys(SuperConstructor);
  for (var i = 0, l = keys.length; i < l; ++i) {
    var key = keys[i];
    Constructor[key] = SuperConstructor[key];
  }
  util.inherits(Constructor, SuperConstructor);
  Constructor.super_ = SuperConstructor;
};
 
/**
 * Tests if a constructor function inherits another constructor function.
 *
 * @ignore
 * @param {Object} Constructor
 * @param {Object} SuperConstructor
 * @returns {boolean}
 */
module.exports.isSubclassOf = function(Constructor, SuperConstructor) {
  if (!_.isFunction(SuperConstructor)) {
    return false;
  }
 
  while (_.isFunction(Constructor)) {
    if (Constructor === SuperConstructor) return true;
    var proto = Constructor.prototype.__proto__;
    Constructor = proto && proto.constructor;
  }
 
  return false;
};