custom-ability
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0-alpha.3
@@ -14,2 +14,3 @@ "use strict"; | ||
const get_non_enumerable_names_1 = require("util-ex/lib/get-non-enumerable-names"); | ||
const inherits_ex_1 = require("inherits-ex"); | ||
const injected_on_parent_1 = __importDefault(require("./injected-on-parent")); | ||
@@ -53,8 +54,13 @@ const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | ||
if (!desc.get && isFn) { | ||
if ((0, function_1.default)(aTargetClass[name])) { | ||
(0, injectMethod_1.default)(aTargetClass, name, v); | ||
result.push(vName); | ||
return; | ||
const vTargetFn = aTargetClass[name]; | ||
if ((0, function_1.default)(vTargetFn)) { | ||
if (!(0, inherits_ex_1.isEmptyFunction)(vTargetFn)) { | ||
if (!(0, inherits_ex_1.isEmptyFunction)(v)) { | ||
(0, injectMethod_1.default)(aTargetClass, name, v); | ||
result.push(vName); | ||
} | ||
return; | ||
} | ||
} | ||
else if (aTargetClass[name] != null) { | ||
else if (vTargetFn != null) { | ||
throw new TypeError('the same non-null name is not function:' + name); | ||
@@ -61,0 +67,0 @@ } |
@@ -8,2 +8,3 @@ import isArray from 'util-ex/lib/is/type/array'; | ||
import { getNonEnumerableNames as getNonEnumNames } from 'util-ex/lib/get-non-enumerable-names'; | ||
import { isEmptyFunction } from 'inherits-ex'; | ||
import isInjectedOnParent from './injected-on-parent'; | ||
@@ -47,8 +48,13 @@ const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; | ||
if (!desc.get && isFn) { | ||
if (isFunction(aTargetClass[name])) { | ||
injectMethod(aTargetClass, name, v); | ||
result.push(vName); | ||
return; | ||
const vTargetFn = aTargetClass[name]; | ||
if (isFunction(vTargetFn)) { | ||
if (!isEmptyFunction(vTargetFn)) { | ||
if (!isEmptyFunction(v)) { | ||
injectMethod(aTargetClass, name, v); | ||
result.push(vName); | ||
} | ||
return; | ||
} | ||
} | ||
else if (aTargetClass[name] != null) { | ||
else if (vTargetFn != null) { | ||
throw new TypeError('the same non-null name is not function:' + name); | ||
@@ -55,0 +61,0 @@ } |
{ | ||
"name": "custom-ability", | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0-alpha.3", | ||
"description": "make custom ability more easy. generate the ability which can be added to any class directly.", | ||
@@ -39,3 +39,3 @@ "homepage": "https://github.com/snowyu/custom-ability.js", | ||
"dependencies": { | ||
"inherits-ex": "^2.1.0-alpha.9", | ||
"inherits-ex": "^2.1.0-alpha.10", | ||
"util-ex": "^2.0.0-alpha.8" | ||
@@ -42,0 +42,0 @@ }, |
@@ -25,3 +25,3 @@ # 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) | ||
**Note**: The all non-enumerable members on the Ability class will be injected into the target class. | ||
**Note**: The all **non-enumerable members** on the Ability class will be injected into the target class. | ||
@@ -160,3 +160,3 @@ **Replace Exists Methods** | ||
**Note**: The methods must be non-enumerable members of the target class(prototype). | ||
**Note**: The methods must be **non-enumerable members** of the target class(prototype). | ||
@@ -163,0 +163,0 @@ In order to make certain ability to work, you need to modify some methods |
@@ -8,2 +8,4 @@ import isArray from 'util-ex/lib/is/type/array'; | ||
import {getNonEnumerableNames as getNonEnumNames} from 'util-ex/lib/get-non-enumerable-names'; | ||
import { isEmptyFunction } from 'inherits-ex'; | ||
import isInjectedOnParent from './injected-on-parent'; | ||
@@ -45,7 +47,12 @@ | ||
if (!desc.get && isFn) { | ||
if (isFunction(aTargetClass[name])) { | ||
injectMethod(aTargetClass, name, v); | ||
result.push(vName); | ||
return; | ||
} else if (aTargetClass[name] != null) { | ||
const vTargetFn = aTargetClass[name] | ||
if (isFunction(vTargetFn)) { | ||
if (!isEmptyFunction(vTargetFn)) { | ||
if (!isEmptyFunction(v)) { | ||
injectMethod(aTargetClass, name, v); | ||
result.push(vName); | ||
} | ||
return; | ||
} | ||
} else if (vTargetFn != null) { | ||
throw new TypeError('the same non-null name is not function:' + name); | ||
@@ -52,0 +59,0 @@ } else { |
@@ -10,3 +10,2 @@ import chai, { expect } from 'chai'; | ||
import {inherits, defineProperty} from 'inherits-ex' | ||
// import defineProperty from 'inherits-ex/lib/defineProperty'; | ||
import {createAbilityInjector as customAbility} from '../src/custom-ability'; | ||
@@ -86,3 +85,3 @@ | ||
}); | ||
it('should ignore getter attribute', function() { | ||
it('should add non-enumerable attributes', function() { | ||
class MyFeature { | ||
@@ -116,2 +115,24 @@ static additionalClassMethod: () => void; | ||
}); | ||
it('should not overwrite an empty function', function() { | ||
class MyFeature { | ||
static additionalClassMethod: () => void; | ||
static coreAbilityClassMethod(){}; | ||
static emptyMethod(){} | ||
coreAbilityMethod(){}; | ||
additionalAbilityMethod(){}; | ||
} | ||
MyFeature.additionalClassMethod = function() {} | ||
const addFeatureTo = customAbility(MyFeature, ['coreAbilityMethod', '@coreAbilityClassMethod']); | ||
function emptyMethod(){console.log("MyClass")} | ||
class MyClass { | ||
declare static emptyMethod: Function | ||
someMethod() {} | ||
} | ||
defineProperty(MyClass, 'emptyMethod', emptyMethod) | ||
// inject the static and instance methods to the MyClass. | ||
addFeatureTo(MyClass); | ||
MyClass.should.have.ownProperty('emptyMethod', emptyMethod) | ||
}); | ||
it('could use getAbilityClass', function() { | ||
@@ -118,0 +139,0 @@ var My, getAbilityClass, result, testable1; |
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
120406
2751
Updatedinherits-ex@^2.1.0-alpha.10