lodash-decorators
Advanced tools
Comparing version 0.4.6 to 0.4.7
@@ -22,4 +22,2 @@ 'use strict'; | ||
var FUNCTION_PROPERTY_EXCLUDES = ['length', 'name', 'arguments', 'caller', 'prototype']; | ||
function bindAllWrapper() { | ||
@@ -41,5 +39,5 @@ for (var _len = arguments.length, methods = Array(_len), _key = 0; _key < _len; _key++) { | ||
var ctor = properties[0]; | ||
var Ctor = properties[0]; | ||
function BindAllWrapper() { | ||
function BindAllConstructor() { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
@@ -51,8 +49,8 @@ args[_key3] = arguments[_key3]; | ||
return ctor.apply(this, args); | ||
return Ctor.apply(this, args); | ||
} | ||
BindAllWrapper.prototype = ctor.prototype; | ||
BindAllConstructor.prototype = Ctor.prototype; | ||
return (0, _utilsAssignAll2['default'])(BindAllWrapper, ctor, FUNCTION_PROPERTY_EXCLUDES); | ||
return (0, _utilsAssignAll2['default'])(BindAllConstructor, Ctor, _utilsAssignAll.FUNCTION_PROPERTY_EXCLUDES); | ||
}; | ||
@@ -59,0 +57,0 @@ } |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.getDecoratorTarget = getDecoratorTarget; | ||
exports.createDecorator = createDecorator; | ||
@@ -37,3 +38,25 @@ exports.createInstanceDecorator = createInstanceDecorator; | ||
var applicators = _Applicator2['default'].applicators; | ||
var decorateTargets = { | ||
GET: 'get', | ||
SET: 'set', | ||
VALUE: 'value', | ||
INITIALIZER: 'initializer' | ||
}; | ||
exports.decorateTargets = decorateTargets; | ||
function getDecoratorTarget(target, name, descriptor, getterSetterMap) { | ||
if (descriptor.get && !getterSetterMap.has([target, name, 'get'])) { | ||
return decorateTargets.GET; | ||
} else if (descriptor.set && !getterSetterMap.has([target, name, 'set'])) { | ||
return decorateTargets.SET; | ||
} else if (descriptor.value) { | ||
return decorateTargets.VALUE; | ||
} else if (descriptor.initializer) { | ||
return decorateTargets.INITIALIZER; | ||
} | ||
throw new ReferenceError('Invalid decorator target.'); | ||
} | ||
function createDecorator(method) { | ||
@@ -52,17 +75,10 @@ var applicator = arguments[1] === undefined ? applicators.pre : arguments[1]; | ||
return function decorator(target, name, descriptor) { | ||
var value = descriptor.value; | ||
var get = descriptor.get; | ||
var set = descriptor.set; | ||
var decoratorTarget = getDecoratorTarget(target, name, descriptor, getterSetterMap); | ||
var preTransform = descriptor[decoratorTarget]; | ||
if (get && !getterSetterMap.has([target, name, 'get'])) { | ||
descriptor.get = _Applicator2['default'].invoke.apply(_Applicator2['default'], [applicator, method, target, get].concat(args)); | ||
(0, _utilsCopyMetaData2['default'])(descriptor.get, get); | ||
getterSetterMap.set([target, name, 'get'], descriptor.get); | ||
} else if (set && !getterSetterMap.has([target, name, 'set'])) { | ||
descriptor.set = _Applicator2['default'].invoke.apply(_Applicator2['default'], [applicator, method, target, set].concat(args)); | ||
(0, _utilsCopyMetaData2['default'])(descriptor.set, set); | ||
getterSetterMap.set([target, name, 'set'], descriptor.set); | ||
} else if (value) { | ||
descriptor.value = _Applicator2['default'].invoke.apply(_Applicator2['default'], [applicator, method, target, value].concat(args)); | ||
(0, _utilsCopyMetaData2['default'])(descriptor.value, value); | ||
descriptor[decoratorTarget] = _Applicator2['default'].invoke.apply(_Applicator2['default'], [applicator, method, target, preTransform].concat(args)); | ||
(0, _utilsCopyMetaData2['default'])(descriptor[decoratorTarget], preTransform); | ||
if (decoratorTarget === decorateTargets.SET || decoratorTarget === decorateTargets.GET) { | ||
getterSetterMap.set([target, name, decoratorTarget], descriptor[decoratorTarget]); | ||
} | ||
@@ -89,14 +105,9 @@ | ||
return function decorator(target, name, descriptor) { | ||
var value = descriptor.value; | ||
var get = descriptor.get; | ||
var set = descriptor.set; | ||
var decoratorTarget = getDecoratorTarget(target, name, descriptor, getterSetterMap); | ||
var preTransform = descriptor[decoratorTarget]; | ||
if (get && !getterSetterMap.has([target, name, 'get'])) { | ||
descriptor.get = (0, _utilsCopyMetaData2['default'])((0, _lodashFunctionPartial2['default'])(instanceDecoratorWrapper, get), get); | ||
getterSetterMap.set([target, name, 'get'], descriptor.get); | ||
} else if (set && !getterSetterMap.has([target, name, 'set'])) { | ||
descriptor.set = (0, _utilsCopyMetaData2['default'])((0, _lodashFunctionPartial2['default'])(instanceDecoratorWrapper, set), set); | ||
getterSetterMap.set([target, name, 'set'], descriptor.set); | ||
} else if (value) { | ||
descriptor.value = (0, _utilsCopyMetaData2['default'])((0, _lodashFunctionPartial2['default'])(instanceDecoratorWrapper, value), value); | ||
descriptor[decoratorTarget] = (0, _utilsCopyMetaData2['default'])((0, _lodashFunctionPartial2['default'])(instanceDecoratorWrapper, preTransform), preTransform); | ||
if (decoratorTarget === decorateTargets.SET || decoratorTarget === decorateTargets.GET) { | ||
getterSetterMap.set([target, name, decoratorTarget], descriptor[decoratorTarget]); | ||
} | ||
@@ -103,0 +114,0 @@ |
@@ -33,4 +33,7 @@ 'use strict'; | ||
returnsArg: _returnsArg2['default'], | ||
writable: _writable2['default'] | ||
writable: _writable2['default'], | ||
readonly: (0, _writable2['default'])(false), | ||
nonenumerable: (0, _enumerable2['default'])(false), | ||
nonconfigurable: (0, _configurable2['default'])(false) | ||
}); | ||
module.exports = exports['default']; |
@@ -5,3 +5,3 @@ { | ||
"description": "A collection of decorators using lodash at it's core.", | ||
"version": "0.4.6", | ||
"version": "0.4.7", | ||
"engines": { | ||
@@ -8,0 +8,0 @@ "node": ">=0.12.0" |
@@ -228,5 +228,9 @@ # lodash-decorators | ||
```javascript | ||
import { once } from 'lodash-decorators' | ||
import { once, compose } from 'lodash-decorators' | ||
import _ from 'lodash'; | ||
function alwaysArray(value) { | ||
return Array.isArray(value) ? value : _.isUndefined(value) ? [] : [value]; | ||
} | ||
class Person { | ||
@@ -236,9 +240,9 @@ constructor() {} | ||
@once | ||
get name() { | ||
return `${this.firstName} ${this.lastName}`; | ||
get names() { | ||
return this.nameList.join(' '); | ||
} | ||
@compose(_.trim) | ||
set name(name) { | ||
[this.firstName, this.lastName] = name.split(' '); | ||
@compose(alwaysArray) | ||
set names(names) { | ||
this.nameList = names; | ||
} | ||
@@ -249,4 +253,6 @@ } | ||
person.name = ' Joe Smith '; | ||
person.name; //=> Joe Smith | ||
// nameList will always be an array. | ||
person.names = undefined; //=> [] | ||
person.names = 'Joe'; //=> ['Joe'] | ||
person.names = ['Jim']; //=> ['Jim'] | ||
``` | ||
@@ -253,0 +259,0 @@ |
@@ -7,2 +7,3 @@ 'use strict'; | ||
exports['default'] = assignAll; | ||
exports.assignProperty = assignProperty; | ||
@@ -21,2 +22,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _lodashUtilityAttempt = require('lodash/utility/attempt'); | ||
var _lodashUtilityAttempt2 = _interopRequireDefault(_lodashUtilityAttempt); | ||
var FUNCTION_PROPERTY_EXCLUDES = ['length', 'name', 'arguments', 'caller', 'prototype']; | ||
exports.FUNCTION_PROPERTY_EXCLUDES = FUNCTION_PROPERTY_EXCLUDES; | ||
/** | ||
@@ -35,14 +43,3 @@ * Assigns all properties, including non-enumerable properties over | ||
var destDtor = Object.getOwnPropertyDescriptor(to, _name); | ||
if (!destDtor || destDtor.configurable) { | ||
var srcDtor = Object.getOwnPropertyDescriptor(from, _name); | ||
if ((0, _lodashLangIsObject2['default'])(srcDtor)) { | ||
Object.defineProperty(to, _name, srcDtor); | ||
} else { | ||
// Fallback to regular assignment | ||
to[_name] = from[_name]; | ||
} | ||
} | ||
(0, _lodashUtilityAttempt2['default'])(assignProperty, to, from, _name); | ||
} | ||
@@ -53,2 +50,15 @@ | ||
module.exports = exports['default']; | ||
function assignProperty(to, from, name) { | ||
var destDtor = Object.getOwnPropertyDescriptor(to, name); | ||
if (!destDtor || destDtor.configurable) { | ||
var srcDtor = Object.getOwnPropertyDescriptor(from, name); | ||
if ((0, _lodashLangIsObject2['default'])(srcDtor)) { | ||
Object.defineProperty(to, name, srcDtor); | ||
} else { | ||
// Fallback to regular assignment | ||
to[name] = from[name]; | ||
} | ||
} | ||
} |
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
39850
682
337