lodash-decorators
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -16,2 +16,4 @@ 'use strict'; | ||
var _decoratorFactory = require('./decoratorFactory'); | ||
function bindWrapper() { | ||
@@ -60,21 +62,28 @@ for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
configurable: true, | ||
get: function bindGetter() { | ||
var thisValue = value; | ||
get: bindGetter | ||
}; | ||
if ((0, _lodash.isFunction)(get)) { | ||
thisValue = get.call(this); | ||
} | ||
function bindGetter() { | ||
var thisValue = value; | ||
var boundValue = (0, _lodash.isFunction)(thisValue) ? _lodash.bind.apply(undefined, [thisValue, this].concat(args)) : thisValue; | ||
if ((0, _lodash.isFunction)(get)) { | ||
thisValue = get.call(this); | ||
} | ||
Object.defineProperty(this, name, { | ||
writable: writable, | ||
configurable: true, | ||
value: boundValue | ||
}); | ||
var boundValue = thisValue; | ||
return boundValue; | ||
if ((0, _lodash.isFunction)(thisValue)) { | ||
boundValue = _lodash.bind.apply(undefined, [thisValue, this].concat(args)); | ||
(0, _decoratorFactory.copyMetaData)(thisValue, boundValue); | ||
} | ||
}; | ||
Object.defineProperty(this, name, { | ||
writable: writable, | ||
configurable: true, | ||
value: boundValue | ||
}); | ||
return boundValue; | ||
} | ||
} | ||
module.exports = exports['default']; |
@@ -84,2 +84,13 @@ 'use strict'; | ||
/** | ||
* Used to copy over meta data from function to function. | ||
* If meta data is attached to a function. This can get lost | ||
* when wrapping functions. This tries to persist that. | ||
*/ | ||
function copyMetaData(from, to) { | ||
(0, _lodash.forOwn)(from, function (value, key) { | ||
return to[key] = from[key]; | ||
}); | ||
} | ||
/** | ||
* Creates a generic decorator for a method on an object. | ||
@@ -109,4 +120,6 @@ * | ||
descriptor.get = TYPE_MAP[type].apply(TYPE_MAP, [root[method], target, toWrap].concat(args)); | ||
copyMetaData(toWrap, descriptor.get); | ||
} else if (value) { | ||
descriptor.value = TYPE_MAP[type].apply(TYPE_MAP, [root[method], target, value].concat(args)); | ||
copyMetaData(value, descriptor.get); | ||
} | ||
@@ -137,3 +150,3 @@ | ||
if (get) { | ||
getter[getterAnnotation] = get[getterAnnotation]; | ||
copyMetaData(get, getter); | ||
} | ||
@@ -151,2 +164,3 @@ | ||
newDescriptor.get = action.apply(undefined, [root[method], this, toWrap].concat(args)); | ||
copyMetaData(toWrap, newDescriptor.get); | ||
@@ -159,2 +173,4 @@ Object.defineProperty(this, name, newDescriptor); | ||
newDescriptor.value = action.apply(undefined, [root[method], this, value].concat(args)); | ||
copyMetaData(value, newDescriptor.value); | ||
Object.defineProperty(this, name, newDescriptor); | ||
@@ -170,4 +186,5 @@ | ||
createDecorator: createDecorator, | ||
createInstanceDecorator: createInstanceDecorator | ||
createInstanceDecorator: createInstanceDecorator, | ||
copyMetaData: copyMetaData | ||
}; | ||
module.exports = exports['default']; |
{ | ||
"name": "lodash-decorators", | ||
"author": "Steven Sojka", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=0.12.0" |
@@ -5,2 +5,3 @@ 'use strict'; | ||
import { isFunction, bind } from 'lodash'; | ||
import { copyMetaData } from './decoratorFactory'; | ||
@@ -30,20 +31,27 @@ export default function bindWrapper(...args) { | ||
configurable: true, | ||
get: function bindGetter() { | ||
let thisValue = value; | ||
get: bindGetter | ||
}; | ||
if (isFunction(get)) { | ||
thisValue = get.call(this); | ||
} | ||
function bindGetter() { | ||
let thisValue = value; | ||
let boundValue = isFunction(thisValue) ? bind(thisValue, this, ...args) : thisValue; | ||
if (isFunction(get)) { | ||
thisValue = get.call(this); | ||
} | ||
Object.defineProperty(this, name, { | ||
writable, | ||
configurable: true, | ||
value: boundValue | ||
}); | ||
let boundValue = thisValue; | ||
return boundValue; | ||
if (isFunction(thisValue)) { | ||
boundValue = bind(thisValue, this, ...args); | ||
copyMetaData(thisValue, boundValue); | ||
} | ||
Object.defineProperty(this, name, { | ||
writable, | ||
configurable: true, | ||
value: boundValue | ||
}); | ||
return boundValue; | ||
} | ||
} |
'use strict'; | ||
import { isFunction, partial } from 'lodash'; | ||
import { forOwn, isFunction, partial } from 'lodash'; | ||
import settings from './settings'; | ||
@@ -33,2 +33,11 @@ | ||
/** | ||
* Used to copy over meta data from function to function. | ||
* If meta data is attached to a function. This can get lost | ||
* when wrapping functions. This tries to persist that. | ||
*/ | ||
function copyMetaData(from, to) { | ||
forOwn(from, (value, key) => to[key] = from[key]); | ||
} | ||
/** | ||
* Creates a generic decorator for a method on an object. | ||
@@ -51,4 +60,6 @@ * | ||
descriptor.get = TYPE_MAP[type](root[method], target, toWrap, ...args); | ||
copyMetaData(toWrap, descriptor.get); | ||
} else if (value) { | ||
descriptor.value = TYPE_MAP[type](root[method], target, value, ...args); | ||
copyMetaData(value, descriptor.get); | ||
} | ||
@@ -71,3 +82,3 @@ | ||
if (get) { | ||
getter[getterAnnotation] = get[getterAnnotation]; | ||
copyMetaData(get, getter); | ||
} | ||
@@ -85,2 +96,3 @@ | ||
newDescriptor.get = action(root[method], this, toWrap, ...args); | ||
copyMetaData(toWrap, newDescriptor.get); | ||
@@ -93,2 +105,4 @@ Object.defineProperty(this, name, newDescriptor); | ||
newDescriptor.value = action(root[method], this, value, ...args); | ||
copyMetaData(value, newDescriptor.value); | ||
Object.defineProperty(this, name, newDescriptor); | ||
@@ -104,3 +118,4 @@ | ||
createDecorator, | ||
createInstanceDecorator | ||
createInstanceDecorator, | ||
copyMetaData | ||
}; |
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
32750
723