adamrenklint/bap

View on GitHub
lib/types/instanceOf.js

Summary

Maintainability
B
4 hrs
Test Coverage
B
84%
function instanceOf (type, Constructor) {

  return {
    set: function (newVal) {
      if (newVal instanceof Constructor) {
        return {
          val: newVal,
          type: type
        };
      }
      else {
        return {
          val: newVal,
          type: typeof newVal
        };
      }
    },
    compare: function (currentVal, newVal, attributeName) {
      var isSame = currentVal === newVal;
      if (!isSame) {
        if (currentVal) {
          this.stopListening(currentVal);
        }
        if (newVal !== null) {
          this.listenTo(newVal, 'all', this._getEventBubblingHandler(attributeName));
        }
      }
      return isSame;
    }
  };
}

module.exports = instanceOf;