Comparing version 1.4.1 to 1.4.2
@@ -114,3 +114,3 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.classy=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | ||
}) | ||
},{"./core":10,"./define":13,"./utils/copy":28}],4:[function(_dereq_,module,exports){ | ||
},{"./core":11,"./define":14,"./utils/copy":29}],4:[function(_dereq_,module,exports){ | ||
module.exports = {} | ||
@@ -123,3 +123,4 @@ },{}],5:[function(_dereq_,module,exports){ | ||
var canDefineProperty = _dereq_('./canDefineProperty') | ||
var canDefineProperty = _dereq_('./canDefineProperty') | ||
var canGetOwnPropertyDescriptor = _dereq_('./canGetOwnPropertyDescriptor') | ||
@@ -136,4 +137,19 @@ var assignClassProperty = function(Class, propName, propDescriptor, config){ | ||
superClass | ||
var own = config.own | ||
var targetPropDescriptor | ||
if (canGetOwnPropertyDescriptor && (propDescriptor.get === undefined || propDescriptor.set == undefined)){ | ||
targetPropDescriptor = Object.getOwnPropertyDescriptor(target, propName) | ||
if (targetPropDescriptor && propDescriptor.get === undefined && targetPropDescriptor.get !== undefined){ | ||
propDescriptor.get = targetPropDescriptor.get | ||
} | ||
if (targetPropDescriptor && propDescriptor.set === undefined && targetPropDescriptor.set !== undefined){ | ||
propDescriptor.set = targetPropDescriptor.set | ||
} | ||
} | ||
// propDescriptor.get = propDescriptor.get || superTarget.get | ||
// propDescriptor.set = propDescriptor.set || superTarget.set | ||
var getterOrSetter = propDescriptor.get || propDescriptor.set | ||
@@ -150,3 +166,3 @@ var newPropDescriptor | ||
if (propDescriptor.set !== undefined){ | ||
newPropDescriptor.set = modifyFn(propName, propDescriptor.get, superTarget, superClass, target, { setter: true }) | ||
newPropDescriptor.set = modifyFn(propName, propDescriptor.set, superTarget, superClass, target, { setter: true }) | ||
} | ||
@@ -185,3 +201,3 @@ propDescriptor = newPropDescriptor | ||
module.exports = assignClassProperty | ||
},{"../utils/copy":28,"./canDefineProperty":7,"./modifyFn":11}],6:[function(_dereq_,module,exports){ | ||
},{"../utils/copy":29,"./canDefineProperty":7,"./canGetOwnPropertyDescriptor":8,"./modifyFn":12}],6:[function(_dereq_,module,exports){ | ||
module.exports = function(){ | ||
@@ -358,2 +374,21 @@ | ||
},{}],9:[function(_dereq_,module,exports){ | ||
'use strict' | ||
var canGetOwnPropertyDescriptor = _dereq_('./canGetOwnPropertyDescriptor') | ||
function copy(source, target){ | ||
Object.getOwnPropertyNames(source).forEach(function(name){ | ||
var sourceDescriptor = Object.getOwnPropertyDescriptor(source, name) | ||
if (!sourceDescriptor.get && !sourceDescriptor.set){ | ||
//dont copy non getters/setters, since this is handled by prototype inheritance | ||
return | ||
} | ||
Object.defineProperty(target, name, sourceDescriptor) | ||
}) | ||
} | ||
module.exports = canGetOwnPropertyDescriptor? copy: function(){} | ||
},{"./canGetOwnPropertyDescriptor":8}],10:[function(_dereq_,module,exports){ | ||
module.exports = function(){ | ||
@@ -384,3 +419,3 @@ | ||
}() | ||
},{}],10:[function(_dereq_,module,exports){ | ||
},{}],11:[function(_dereq_,module,exports){ | ||
module.exports = function(){ | ||
@@ -401,2 +436,4 @@ | ||
var copyDescriptors = _dereq_('./copyDescriptors') | ||
var Base = function(){} | ||
@@ -464,2 +501,5 @@ | ||
copyDescriptors(Parent.prototype, Class.prototype) | ||
copyDescriptors(Parent, Class) | ||
//remove statics from config | ||
@@ -478,2 +518,3 @@ var statics = config.statics || {} | ||
//copy static properties from Parent to Class | ||
copyClassConfig( Class, Parent, { | ||
@@ -490,4 +531,6 @@ proto : false, | ||
//copy static properties from config statics to class | ||
copyClassConfig( Class, statics, { proto: false }) | ||
//copy static own properties | ||
if ($own){ | ||
@@ -530,3 +573,3 @@ copyClassConfig( Class, $own, { proto: false, own: true }) | ||
if (skipOwn && configOwn[key]){ | ||
//this property should not be copied, to skip to next property | ||
//this property should not be copied -> skip to next property | ||
continue | ||
@@ -548,3 +591,7 @@ } | ||
} else { | ||
result[key] = keyResult | ||
if (canGetOwnPropertyDescriptor){ | ||
Object.defineProperty(result, key, valueDescriptor) | ||
} else { | ||
result[key] = keyResult | ||
} | ||
} | ||
@@ -596,3 +643,3 @@ | ||
}() | ||
},{"../utils/copy":28,"./assignClassProperty":5,"./canDefineProperty":7,"./canGetOwnPropertyDescriptor":8,"./extend":9,"newify":2}],11:[function(_dereq_,module,exports){ | ||
},{"../utils/copy":29,"./assignClassProperty":5,"./canDefineProperty":7,"./canGetOwnPropertyDescriptor":8,"./copyDescriptors":9,"./extend":10,"newify":2}],12:[function(_dereq_,module,exports){ | ||
var callSuperRe = /\bcallSuper|callSuperWith\b/ | ||
@@ -625,3 +672,3 @@ var callOverridenRe = /\bcallOverriden|callOverridenWith\b/ | ||
module.exports = modify | ||
},{"./buildClassFunctions":6}],12:[function(_dereq_,module,exports){ | ||
},{"./buildClassFunctions":6}],13:[function(_dereq_,module,exports){ | ||
var SLICE = Array.prototype.slice | ||
@@ -659,3 +706,3 @@ | ||
} | ||
},{"./getClass":17,"newify":2}],13:[function(_dereq_,module,exports){ | ||
},{"./getClass":18,"newify":2}],14:[function(_dereq_,module,exports){ | ||
var getClass = _dereq_('./getClass') | ||
@@ -716,3 +763,3 @@ var processClass = _dereq_('./processClass') | ||
} | ||
},{"./Registry":4,"./core":10,"./getClass":17,"./processClass":24,"./processors/ClassProcessor":25}],14:[function(_dereq_,module,exports){ | ||
},{"./Registry":4,"./core":11,"./getClass":18,"./processClass":25,"./processors/ClassProcessor":26}],15:[function(_dereq_,module,exports){ | ||
var define = _dereq_('./define') | ||
@@ -727,3 +774,3 @@ var copyIf = _dereq_('./utils/copy').copyIf | ||
} | ||
},{"./define":13,"./utils/copy":28}],15:[function(_dereq_,module,exports){ | ||
},{"./define":14,"./utils/copy":29}],16:[function(_dereq_,module,exports){ | ||
/** | ||
@@ -752,3 +799,3 @@ * @method destroyClass | ||
} | ||
},{"./core":10,"./getClass":17}],16:[function(_dereq_,module,exports){ | ||
},{"./core":11,"./getClass":18}],17:[function(_dereq_,module,exports){ | ||
@@ -768,3 +815,3 @@ module.exports = function(config){ | ||
} | ||
},{"./define":13}],17:[function(_dereq_,module,exports){ | ||
},{"./define":14}],18:[function(_dereq_,module,exports){ | ||
/** | ||
@@ -797,3 +844,3 @@ * @method getClass | ||
} | ||
},{"./Registry":4,"./core":10}],18:[function(_dereq_,module,exports){ | ||
},{"./Registry":4,"./core":11}],19:[function(_dereq_,module,exports){ | ||
var BaseClass = _dereq_('./core').BaseClass | ||
@@ -842,3 +889,3 @@ var getClass = _dereq_('./getClass') | ||
} | ||
},{"./core":10,"./getClass":17}],19:[function(_dereq_,module,exports){ | ||
},{"./core":11,"./getClass":18}],20:[function(_dereq_,module,exports){ | ||
var BaseClass = _dereq_('./core').BaseClass | ||
@@ -869,3 +916,3 @@ var getClass = _dereq_('./getClass') | ||
} | ||
},{"./core":10,"./getClass":17}],20:[function(_dereq_,module,exports){ | ||
},{"./core":11,"./getClass":18}],21:[function(_dereq_,module,exports){ | ||
/* | ||
@@ -928,3 +975,3 @@ | ||
}() | ||
},{"./Mixin":3,"./Registry":4,"./core":10,"./create":12,"./define":13,"./defineMixin":14,"./destroyClass":15,"./getClass":17,"./getInstance":18,"./getParentClass":19,"./isSubclassOf":21,"./override":22,"./processors/MixinProcessor":26,"./utils/copy":28}],21:[function(_dereq_,module,exports){ | ||
},{"./Mixin":3,"./Registry":4,"./core":11,"./create":13,"./define":14,"./defineMixin":15,"./destroyClass":16,"./getClass":18,"./getInstance":19,"./getParentClass":20,"./isSubclassOf":22,"./override":23,"./processors/MixinProcessor":27,"./utils/copy":29}],22:[function(_dereq_,module,exports){ | ||
var getClass = _dereq_('./getClass') | ||
@@ -953,3 +1000,3 @@ | ||
} | ||
},{"./getClass":17}],22:[function(_dereq_,module,exports){ | ||
},{"./getClass":18}],23:[function(_dereq_,module,exports){ | ||
var getClass = _dereq_('./getClass') | ||
@@ -985,3 +1032,3 @@ | ||
} | ||
},{"./getClass":17}],23:[function(_dereq_,module,exports){ | ||
},{"./getClass":18}],24:[function(_dereq_,module,exports){ | ||
module.exports = function(config){ | ||
@@ -994,3 +1041,3 @@ | ||
} | ||
},{"./core":10}],24:[function(_dereq_,module,exports){ | ||
},{"./core":11}],25:[function(_dereq_,module,exports){ | ||
var copyKeys = _dereq_('./utils/copy').copyKeys | ||
@@ -1036,3 +1083,3 @@ | ||
} | ||
},{"./extendClass":16,"./overrideClass":23,"./processors/ClassProcessor":25,"./unregisterClass":27,"./utils/copy":28}],25:[function(_dereq_,module,exports){ | ||
},{"./extendClass":17,"./overrideClass":24,"./processors/ClassProcessor":26,"./unregisterClass":28,"./utils/copy":29}],26:[function(_dereq_,module,exports){ | ||
/* | ||
@@ -1078,3 +1125,3 @@ | ||
}() | ||
},{"./MixinProcessor":26}],26:[function(_dereq_,module,exports){ | ||
},{"./MixinProcessor":27}],27:[function(_dereq_,module,exports){ | ||
/* | ||
@@ -1466,3 +1513,3 @@ | ||
}() | ||
},{"../core":10,"../getClass":17,"../utils/copy":28,"../utils/function":29}],27:[function(_dereq_,module,exports){ | ||
},{"../core":11,"../getClass":18,"../utils/copy":29,"../utils/function":30}],28:[function(_dereq_,module,exports){ | ||
var REGISTRY = _dereq_('./Registry') | ||
@@ -1481,3 +1528,3 @@ | ||
} | ||
},{"./Registry":4}],28:[function(_dereq_,module,exports){ | ||
},{"./Registry":4}],29:[function(_dereq_,module,exports){ | ||
/* | ||
@@ -1825,3 +1872,3 @@ | ||
}() | ||
},{}],29:[function(_dereq_,module,exports){ | ||
},{}],30:[function(_dereq_,module,exports){ | ||
module.exports = function(){ | ||
@@ -1883,4 +1930,4 @@ | ||
}() | ||
},{}]},{},[20]) | ||
(20) | ||
},{}]},{},[21]) | ||
(21) | ||
}); |
{ | ||
"name": "classy", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"scripts": { | ||
@@ -5,0 +5,0 @@ "build": "browserify src/index.js -s classy -o dist/classy.js", |
@@ -6,3 +6,4 @@ 'use strict' | ||
var canDefineProperty = require('./canDefineProperty') | ||
var canDefineProperty = require('./canDefineProperty') | ||
var canGetOwnPropertyDescriptor = require('./canGetOwnPropertyDescriptor') | ||
@@ -19,4 +20,17 @@ var assignClassProperty = function(Class, propName, propDescriptor, config){ | ||
superClass | ||
var own = config.own | ||
var targetPropDescriptor | ||
if (canGetOwnPropertyDescriptor && (propDescriptor.get === undefined || propDescriptor.set == undefined)){ | ||
targetPropDescriptor = Object.getOwnPropertyDescriptor(target, propName) | ||
if (targetPropDescriptor && propDescriptor.get === undefined && targetPropDescriptor.get !== undefined){ | ||
propDescriptor.get = targetPropDescriptor.get | ||
} | ||
if (targetPropDescriptor && propDescriptor.set === undefined && targetPropDescriptor.set !== undefined){ | ||
propDescriptor.set = targetPropDescriptor.set | ||
} | ||
} | ||
var getterOrSetter = propDescriptor.get || propDescriptor.set | ||
@@ -33,3 +47,3 @@ var newPropDescriptor | ||
if (propDescriptor.set !== undefined){ | ||
newPropDescriptor.set = modifyFn(propName, propDescriptor.get, superTarget, superClass, target, { setter: true }) | ||
newPropDescriptor.set = modifyFn(propName, propDescriptor.set, superTarget, superClass, target, { setter: true }) | ||
} | ||
@@ -36,0 +50,0 @@ propDescriptor = newPropDescriptor |
@@ -16,2 +16,4 @@ module.exports = function(){ | ||
var copyDescriptors = require('./copyDescriptors') | ||
var Base = function(){} | ||
@@ -79,2 +81,5 @@ | ||
copyDescriptors(Parent.prototype, Class.prototype) | ||
copyDescriptors(Parent, Class) | ||
//remove statics from config | ||
@@ -93,2 +98,3 @@ var statics = config.statics || {} | ||
//copy static properties from Parent to Class | ||
copyClassConfig( Class, Parent, { | ||
@@ -105,4 +111,6 @@ proto : false, | ||
//copy static properties from config statics to class | ||
copyClassConfig( Class, statics, { proto: false }) | ||
//copy static own properties | ||
if ($own){ | ||
@@ -145,3 +153,3 @@ copyClassConfig( Class, $own, { proto: false, own: true }) | ||
if (skipOwn && configOwn[key]){ | ||
//this property should not be copied, to skip to next property | ||
//this property should not be copied -> skip to next property | ||
continue | ||
@@ -163,3 +171,7 @@ } | ||
} else { | ||
result[key] = keyResult | ||
if (canGetOwnPropertyDescriptor){ | ||
Object.defineProperty(result, key, valueDescriptor) | ||
} else { | ||
result[key] = keyResult | ||
} | ||
} | ||
@@ -166,0 +178,0 @@ |
@@ -49,2 +49,139 @@ /* | ||
it('should work properly for setter', function(){ | ||
var Vehicle = root.define({ | ||
_speed: 10, | ||
get speed(){ | ||
return this._speed | ||
}, | ||
set speed(s){ | ||
this._speed = s | ||
} | ||
}) | ||
var v = new Vehicle() | ||
expect(v.speed).toBe(10) | ||
expect(v._speed).toBe(10) | ||
v.speed = 1 | ||
expect(v._speed).toBe(1) | ||
var Car = root.define({ | ||
extend: Vehicle, | ||
_speed: 100, | ||
get speed(){ | ||
return this.callSuper() * 2 | ||
} | ||
}) | ||
var c = new Car() | ||
c.speed = 2 | ||
expect(c._speed).toBe(2) | ||
expect(c.speed).toBe(4) | ||
}) | ||
it('should work properly for setters defined in classes 2 levels above', function(){ | ||
var Vehicle = root.define({ | ||
_speed: 10, | ||
get speed(){ | ||
return this._speed + 'km' | ||
}, | ||
set speed(s){ | ||
this._speed = s | ||
} | ||
}) | ||
var Car = root.define({ | ||
extend: Vehicle | ||
}) | ||
var SpeedCar = root.define({ | ||
extend: Car | ||
}) | ||
var lotus = new SpeedCar() | ||
lotus.speed = 250 | ||
expect(lotus.speed).toBe('250km') | ||
expect(lotus._speed).toBe(250) | ||
var TopSpeedCar = root.define({ | ||
extend: SpeedCar, | ||
set speed(s){ | ||
this.callSuperWith(2*s) | ||
} | ||
}) | ||
var ferrari = new TopSpeedCar() | ||
expect(ferrari._speed).toBe(10) | ||
expect(ferrari.speed).toBe('10km') | ||
ferrari.speed = 200 | ||
expect(ferrari._speed).toBe(400) | ||
expect(ferrari.speed).toBe('400km') | ||
}) | ||
it('should work properly for setters defined - case 2', function(){ | ||
var Vehicle = root.define({ | ||
_speed: 10, | ||
get speed(){ | ||
return this._speed + 'km' | ||
}, | ||
set speed(s){ | ||
this._speed = s | ||
} | ||
}) | ||
var Car = root.define({ | ||
extend: Vehicle, | ||
get speed(){ | ||
return this._speed + 'miles' | ||
} | ||
}) | ||
var c = new Car() | ||
c.speed = 1 | ||
expect(c.speed).toBe('1miles') | ||
var SpeedCar = root.define({ | ||
extend: Car | ||
}) | ||
var lotus = new SpeedCar() | ||
lotus.speed = 250 | ||
expect(lotus.speed).toBe('250miles') | ||
expect(lotus._speed).toBe(250) | ||
var TopSpeedCar = root.define({ | ||
extend: SpeedCar, | ||
set speed(s){ | ||
this.callSuperWith(2*s) | ||
} | ||
}) | ||
var ferrari = new TopSpeedCar() | ||
expect(ferrari._speed).toBe(10) | ||
expect(ferrari.speed).toBe('10miles') | ||
ferrari.speed = 200 | ||
expect(ferrari._speed).toBe(400) | ||
expect(ferrari.speed).toBe('400miles') | ||
}) | ||
it('should use getter with call overriden', function(){ | ||
@@ -122,3 +259,13 @@ var Vehicle = root.define({ | ||
it('should not call getters on define', function(){ | ||
root.define({ | ||
alias: 'adsdsaf3', | ||
get test(){ | ||
expect(1).toBe(2) | ||
this.x() | ||
} | ||
}) | ||
}) | ||
}) | ||
Sorry, the diff of this file is not supported yet
355088
69
4875