custom-ability
Advanced tools
Comparing version 1.6.1 to 1.6.2
@@ -31,5 +31,6 @@ (function() { | ||
nonEnumNames.forEach(function(k) { | ||
var v, vK; | ||
var is$, v, vK; | ||
if ((isStatic || k !== 'constructor') && isFunction(v = aObject[k])) { | ||
if (k[0] === '$') { | ||
is$ = k[0] === '$'; | ||
if (is$) { | ||
k = k.substr(1); | ||
@@ -44,2 +45,5 @@ } | ||
} else { | ||
if (is$ && aObject[k]) { | ||
v = aObject[k]; | ||
} | ||
aTargetClass[k] = v; | ||
@@ -46,0 +50,0 @@ } |
{ | ||
"name": "custom-ability", | ||
"version": "1.6.1", | ||
"version": "1.6.2", | ||
"description": "make custom ability more easy. generate the ability which can be added to any class directly.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/snowyu/custom-ability.js", |
@@ -191,2 +191,64 @@ # custom-ability [![Build Status](https://img.shields.io/travis/snowyu/custom-ability.js/master.png)](http://travis-ci.org/snowyu/custom-ability.js) [![npm](https://img.shields.io/npm/v/custom-ability.svg)](https://npmjs.org/package/custom-ability) [![downloads](https://img.shields.io/npm/dm/custom-ability.svg)](https://npmjs.org/package/custom-ability) [![license](https://img.shields.io/npm/l/custom-ability.svg)](https://npmjs.org/package/custom-ability) | ||
### V1.6.2 | ||
* fix: use replace instead inject method if there is no such method on the target | ||
```javascript | ||
const makeAbility = require('custom-ability') | ||
class Feature { | ||
$init() { | ||
const Super = this.super | ||
const that = this.self || this | ||
if (Super) { | ||
if (Super.apply(that, arguments) === 'ok') return | ||
} | ||
that._init.apply(that, arguments) | ||
} | ||
_init() {console.log('feature init')} | ||
} | ||
Feature.prototype.init = function() {this._init.apply(this, arguments)} | ||
const addFeatureTo = makeAbility(Feature) | ||
class My { | ||
} | ||
addFeatureTo(My) | ||
expect(My.prototype.init).toStrictEqual(Feature.prototype.init) | ||
``` | ||
* fix(1.6.1): the injectMethods(AOP) starting with "$" was incorrectly replaced with the original method | ||
```js | ||
const makeAbility = require('custom-ability') | ||
class Feature { | ||
// inject to the init method on target class | ||
$init() { | ||
const Super = this.super | ||
const that = this.self || this | ||
if (Super) { | ||
if (Super.apply(that, arguments) === 'ok') return | ||
} | ||
that._init.apply(that, arguments) | ||
} | ||
_init() {console.log('feature init')} | ||
} | ||
Feature.prototype.init = function() {this._init.apply(this, arguments)} | ||
const addFeatureTo = makeAbility(Feature) | ||
class My { | ||
init(doInitFeature = true) { | ||
// the my init procedure | ||
console.log('my init') | ||
if (!doInitFeature) return 'ok' | ||
} | ||
} | ||
addFeatureTo(My) | ||
const obj = new My | ||
obj.init() | ||
// my init | ||
// feature init | ||
obj.init(false) | ||
// my init | ||
``` | ||
### V1.6.0 | ||
@@ -193,0 +255,0 @@ |
@@ -585,4 +585,2 @@ var assert, chai, customAbility, defineProperty, inherits, setImmediate, should, sinon, sinonChai; | ||
OneAbility.prototype.init = sinon.spy(); | ||
OneAbility.cone = sinon.spy(); | ||
@@ -622,3 +620,2 @@ | ||
a = new A(123); | ||
OneAbility.prototype.init.should.not.be.called; | ||
OneAbility.prototype.$init.should.be.calledOnce; | ||
@@ -673,2 +670,107 @@ OneAbility.prototype.$init.should.be.calledWith(123); | ||
}); | ||
describe('use the injectMethods(AOP) to hook(with same method)', function() { | ||
var oneTestable; | ||
class OneAbility {}; | ||
defineProperty(OneAbility.prototype, '$init', sinon.spy(function() { | ||
if (this.super) { | ||
return this.super.apply(this.self, arguments); | ||
} | ||
})); | ||
OneAbility.prototype.one = sinon.spy(); | ||
OneAbility.prototype.two = sinon.spy(); | ||
OneAbility.prototype.three = sinon.spy(); | ||
OneAbility.prototype.emit = sinon.spy(); | ||
OneAbility.prototype.init = sinon.spy(); | ||
OneAbility.cone = sinon.spy(); | ||
OneAbility.ctwo = sinon.spy(); | ||
oneTestable = customAbility(OneAbility, 'emit'); | ||
beforeEach(function() { | ||
var k, ref, v; | ||
OneAbility.prototype.$init.reset(); | ||
for (k in OneAbility) { | ||
v = OneAbility[k]; | ||
v.reset(); | ||
} | ||
ref = OneAbility.prototype; | ||
for (k in ref) { | ||
v = ref[k]; | ||
v.reset(); | ||
} | ||
}); | ||
it('should injectMethod correctly', function() { | ||
var a, oldInit, t; | ||
oldInit = sinon.spy(); | ||
class A { | ||
constructor() { | ||
this.hi = 123; | ||
this.init.apply(this, arguments); | ||
} | ||
}; | ||
A.prototype.init = oldInit; | ||
oneTestable(A); | ||
a = new A(123); | ||
OneAbility.prototype.init.should.not.be.called; | ||
OneAbility.prototype.$init.should.be.calledOnce; | ||
OneAbility.prototype.$init.should.be.calledWith(123); | ||
t = OneAbility.prototype.$init.thisValues[0]; | ||
t.should.have.property('self', a); | ||
oldInit.should.be.calledOnce; | ||
oldInit.should.be.calledWith(123); | ||
}); | ||
it('should injectMethod non-exist correctly', function() { | ||
var a, oldInit, t; | ||
oldInit = sinon.spy(); | ||
class A { | ||
constructor() { | ||
this.hi = 123; | ||
this.init.apply(this, arguments); | ||
} | ||
}; | ||
oneTestable(A); | ||
a = new A(123); | ||
OneAbility.prototype.$init.should.not.be.called; | ||
OneAbility.prototype.init.should.be.calledOnce; | ||
OneAbility.prototype.init.should.be.calledWith(123); | ||
t = OneAbility.prototype.init.thisValues[0]; | ||
t.should.be.equal(a); | ||
}); | ||
it('should ignore some injectMethod', function() { | ||
var a, oldInit; | ||
oldInit = sinon.spy(); | ||
class A { | ||
constructor() { | ||
this.hi = 123; | ||
this.init.apply(this, arguments); | ||
} | ||
}; | ||
A.prototype.init = oldInit; | ||
oneTestable(A, { | ||
exclude: 'init' | ||
}); | ||
a = new A(123); | ||
OneAbility.prototype.$init.should.not.be.called; | ||
oldInit.should.be.calledOnce; | ||
oldInit.should.be.calledWith(123); | ||
}); | ||
}); | ||
describe('How to ensure that the injected object does not have the same method', function() { | ||
@@ -675,0 +777,0 @@ var init = sinon.spy() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78210
1065
482