custom-ability
Advanced tools
Comparing version 2.0.0-alpha.6 to 2.0.0-alpha.7
@@ -26,4 +26,5 @@ export declare const abilitiesSym = "$abilities"; | ||
depends?: AdditionalAbilities; | ||
afterInjection?: (targetClass: Function, options?: AbilityOptions) => void; | ||
} | ||
export type AbilityFn = (targetClass: Function, options?: AbilityOptions) => Function; | ||
export type AbilityFn = (targetClass?: Function, options?: AbilityOptions) => Function; | ||
export declare function createAbilityInjector(abilityClass: Function, isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn; | ||
@@ -30,0 +31,0 @@ export declare function createAbilityInjector(abilityClass: Function, aCoreMethod?: string | string[], isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn; |
@@ -85,2 +85,3 @@ "use strict"; | ||
const vDepends = injectorOpts && injectorOpts.depends; | ||
const afterInjection = injectorOpts && injectorOpts.afterInjection; | ||
function abilityFn(aClass, aOptions) { | ||
@@ -109,3 +110,4 @@ let AbilityClass = abilityClass; | ||
} | ||
if (!(vHasCoreMethod || ($abilities && $abilities['$' + vName]))) { | ||
const needInjection = !(vHasCoreMethod || ($abilities && $abilities['$' + vName])); | ||
if (needInjection) { | ||
let vIncludeMembers; | ||
@@ -214,2 +216,5 @@ let vFilterMembers; | ||
} | ||
if (needInjection && typeof afterInjection === 'function') { | ||
afterInjection(aClass, aOptions); | ||
} | ||
} | ||
@@ -216,0 +221,0 @@ else { |
@@ -26,4 +26,5 @@ export declare const abilitiesSym = "$abilities"; | ||
depends?: AdditionalAbilities; | ||
afterInjection?: (targetClass: Function, options?: AbilityOptions) => void; | ||
} | ||
export type AbilityFn = (targetClass: Function, options?: AbilityOptions) => Function; | ||
export type AbilityFn = (targetClass?: Function, options?: AbilityOptions) => Function; | ||
export declare function createAbilityInjector(abilityClass: Function, isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn; | ||
@@ -30,0 +31,0 @@ export declare function createAbilityInjector(abilityClass: Function, aCoreMethod?: string | string[], isGetClassFunc?: boolean, injectorOpts?: AbilityInjectorOptions): AbilityFn; |
@@ -79,2 +79,3 @@ import isArray from 'util-ex/lib/is/type/array'; | ||
const vDepends = injectorOpts && injectorOpts.depends; | ||
const afterInjection = injectorOpts && injectorOpts.afterInjection; | ||
function abilityFn(aClass, aOptions) { | ||
@@ -103,3 +104,4 @@ let AbilityClass = abilityClass; | ||
} | ||
if (!(vHasCoreMethod || ($abilities && $abilities['$' + vName]))) { | ||
const needInjection = !(vHasCoreMethod || ($abilities && $abilities['$' + vName])); | ||
if (needInjection) { | ||
let vIncludeMembers; | ||
@@ -208,2 +210,5 @@ let vFilterMembers; | ||
} | ||
if (needInjection && typeof afterInjection === 'function') { | ||
afterInjection(aClass, aOptions); | ||
} | ||
} | ||
@@ -210,0 +215,0 @@ else { |
{ | ||
"name": "custom-ability", | ||
"version": "2.0.0-alpha.6", | ||
"version": "2.0.0-alpha.7", | ||
"description": "make custom ability more easy. generate the ability which can be added to any class directly.", | ||
@@ -17,2 +17,5 @@ "homepage": "https://github.com/snowyu/custom-ability.js", | ||
], | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"scripts": { | ||
@@ -36,5 +39,2 @@ "build": "npm run build.ts && npm run doc.md", | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"dependencies": { | ||
@@ -41,0 +41,0 @@ "inherits-ex": "^2.1.0-alpha.12", |
@@ -163,2 +163,3 @@ import isArray from 'util-ex/lib/is/type/array'; | ||
depends?: AdditionalAbilities; | ||
afterInjection?: (targetClass: Function, options?: AbilityOptions) => void; | ||
} | ||
@@ -176,3 +177,3 @@ | ||
*/ | ||
export type AbilityFn = (targetClass: Function, options?: AbilityOptions) => Function; | ||
export type AbilityFn = (targetClass?: Function, options?: AbilityOptions) => Function; | ||
@@ -224,2 +225,3 @@ /** | ||
const vDepends = injectorOpts && injectorOpts.depends; | ||
const afterInjection = injectorOpts && injectorOpts.afterInjection; | ||
@@ -253,3 +255,5 @@ function abilityFn(aClass, aOptions?) { | ||
if (!(vHasCoreMethod || ($abilities && $abilities['$' + vName]))) { | ||
const needInjection = !(vHasCoreMethod || ($abilities && $abilities['$' + vName])) | ||
if (needInjection) { | ||
let vIncludeMembers!: Array<string> | ||
@@ -361,2 +365,4 @@ let vFilterMembers!: (name: string) => boolean | ||
} | ||
if (needInjection && typeof afterInjection === 'function') { afterInjection(aClass, aOptions) } | ||
} else { | ||
@@ -363,0 +369,0 @@ aClass = AbilityClass; |
@@ -136,9 +136,8 @@ import chai, { expect } from 'chai'; | ||
it('could use getAbilityClass', function() { | ||
var My, getAbilityClass, result, testable1; | ||
My = class My {}; | ||
getAbilityClass = function(aClass) { | ||
const My = class My {}; | ||
const getAbilityClass = function(aClass) { | ||
return MyAbility; | ||
}; | ||
testable1 = createAbilityInjector(getAbilityClass, true); | ||
result = testable1(My); | ||
const testable1 = createAbilityInjector(getAbilityClass, true); | ||
const result = testable1(My); | ||
result.should.be.equal(My); | ||
@@ -148,20 +147,14 @@ return myAbilityCheck(result); | ||
it('could get AbilityClass when no aClass passing', function() { | ||
var My, testable1; | ||
testable1 = createAbilityInjector(MyAbility); | ||
My = testable1(); | ||
const testable1 = createAbilityInjector(MyAbility); | ||
const My = testable1(); | ||
return My.should.be.equal(MyAbility); | ||
}); | ||
it('could no inject if have already static coreMethod', function() { | ||
var My, testable1; | ||
testable1 = createAbilityInjector(MyAbility, '@cone'); | ||
My = (function() { | ||
class My { | ||
static cone: number; | ||
}; | ||
const testable1 = createAbilityInjector(MyAbility, '@cone'); | ||
class My { | ||
static cone: number; | ||
}; | ||
My.cone = 12; | ||
My.cone = 12; | ||
return My; | ||
}).call(this); | ||
testable1(My); | ||
@@ -290,8 +283,5 @@ return My.should.have.property('cone', 12); | ||
it('should get proper aClass in getClass function to make ability ', function() { | ||
let My, MyA, k, my, ref, testable1, v; | ||
MyA = undefined; | ||
let MyA; | ||
function fn(aClass, aOptions) { | ||
var MyAbility1; | ||
return MyA = MyAbility1 = (function() { | ||
class MyAbility1 { | ||
class MyAbility1 { | ||
emit: sinon.SinonSpy<any[], any>; | ||
@@ -310,30 +300,27 @@ one: any; | ||
MyAbility1.count = 1; | ||
MyA = MyAbility1 | ||
return MyAbility1; | ||
}).call(this); | ||
}; | ||
testable1 = createAbilityInjector(fn, 'emit', true); | ||
My = function() {}; | ||
const testable1 = createAbilityInjector(fn, 'emit', true); | ||
const My = function() {}; | ||
testable1(My).should.be.equal(My); | ||
for (k in MyA) { | ||
v = MyA[k]; | ||
for (const k in MyA) { | ||
const v = MyA[k]; | ||
v.should.be.equal(My[k]); | ||
} | ||
ref = MyA.prototype; | ||
for (k in ref) { | ||
v = ref[k]; | ||
const ref = MyA.prototype; | ||
for (const k in ref) { | ||
const v = ref[k]; | ||
v.should.be.equal(My.prototype[k]); | ||
} | ||
my = new My(); | ||
const my = new My(); | ||
my.one(); | ||
return my.one.should.be.calledOnce; | ||
my.one.should.be.calledOnce; | ||
}); | ||
it('should only include methods', function() { | ||
var My, keys; | ||
My = function() {}; | ||
const My = function() {}; | ||
testable(My, { | ||
include: ['one', '@ctwo'] | ||
}); | ||
keys = Object.keys(My); | ||
let keys = Object.keys(My); | ||
assert.deepEqual(keys, ['ctwo']); | ||
@@ -344,8 +331,7 @@ keys = Object.keys(My.prototype); | ||
it('should include one method as string', function() { | ||
var My, keys; | ||
My = function() {}; | ||
const My = function() {}; | ||
testable(My, { | ||
include: 'two' | ||
}); | ||
keys = Object.keys(My); | ||
let keys = Object.keys(My); | ||
assert.deepEqual(keys, []); | ||
@@ -357,8 +343,7 @@ keys = Object.keys(My.prototype); | ||
it('should exclude methods', function() { | ||
var My, keys; | ||
My = function() {}; | ||
const My = function() {}; | ||
testable(My, { | ||
exclude: ['one', 'two', '@ctwo'] | ||
}); | ||
keys = Object.keys(My); | ||
let keys = Object.keys(My); | ||
assert.deepEqual(keys, ['cone']); | ||
@@ -371,13 +356,11 @@ My.should.not.have.ownProperty('ctwo'); | ||
it('should exclude one method', function() { | ||
var My, keys; | ||
My = function() {}; | ||
const My = function() {}; | ||
testable(My, { | ||
exclude: 'one' | ||
}); | ||
keys = Object.keys(My.prototype).sort(); | ||
const keys = Object.keys(My.prototype).sort(); | ||
assert.deepEqual(keys, ['emit', 'two', 'three'].sort()); | ||
}); | ||
it('should include and exclude methods', function() { | ||
var My, keys; | ||
My = function() {}; | ||
const My = function() {}; | ||
testable(My, { | ||
@@ -387,3 +370,3 @@ include: ['one', 'three'], | ||
}); | ||
keys = Object.keys(My.prototype).sort(); | ||
const keys = Object.keys(My.prototype).sort(); | ||
assert.deepEqual(keys, ['one', 'two', 'three', 'emit'].sort()); | ||
@@ -390,0 +373,0 @@ }); |
156523
3320