lodash-decorators
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -56,3 +56,3 @@ 'use strict'; | ||
var value = descriptor.value; | ||
var get = descriptor.get; | ||
var _get = descriptor.get; | ||
var writable = descriptor.writable; | ||
@@ -62,28 +62,34 @@ | ||
configurable: true, | ||
get: bindGetter | ||
}; | ||
// Allows the user to reassign the variable | ||
set: function set(value) { | ||
Object.defineProperty(this, name, { | ||
configurable: true, | ||
value: value, | ||
writable: writable | ||
}); | ||
}, | ||
get: function get() { | ||
var thisValue = value; | ||
function bindGetter() { | ||
var thisValue = value; | ||
if ((0, _lodash.isFunction)(_get)) { | ||
thisValue = _get.call(this); | ||
} | ||
if ((0, _lodash.isFunction)(get)) { | ||
thisValue = get.call(this); | ||
} | ||
var boundValue = thisValue; | ||
var boundValue = thisValue; | ||
if ((0, _lodash.isFunction)(thisValue)) { | ||
boundValue = _lodash.bind.apply(undefined, [thisValue, this].concat(args)); | ||
(0, _decoratorFactory.copyMetaData)(thisValue, 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; | ||
} | ||
Object.defineProperty(this, name, { | ||
writable: writable, | ||
configurable: true, | ||
value: boundValue | ||
}); | ||
return boundValue; | ||
} | ||
}; | ||
} | ||
module.exports = exports['default']; |
@@ -7,4 +7,13 @@ 'use strict'; | ||
var _TYPE_MAP; | ||
exports.isGetter = isGetter; | ||
exports.copyMetaData = copyMetaData; | ||
exports.createDecorator = createDecorator; | ||
exports.createInstanceDecorator = createInstanceDecorator; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _defineProperty(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | ||
@@ -18,64 +27,48 @@ | ||
var TYPE_MAP = { | ||
// Methods where the function is the last argument or the first | ||
// and all other arguments come before or after. | ||
post: function post(fn, target, value) { | ||
for (var _len = arguments.length, args = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { | ||
args[_key - 3] = arguments[_key]; | ||
} | ||
var _applyTypes = require('./applyTypes'); | ||
return fn.apply(undefined, args.concat([value])); | ||
}, | ||
pre: function pre(fn, target, value) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) { | ||
args[_key2 - 3] = arguments[_key2]; | ||
} | ||
var TYPE_MAP = (_TYPE_MAP = {}, _defineProperty(_TYPE_MAP, _applyTypes.POST, function (fn, target, value) { | ||
for (var _len = arguments.length, args = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { | ||
args[_key - 3] = arguments[_key]; | ||
} | ||
return fn.apply(undefined, [value].concat(args)); | ||
}, | ||
return fn.apply(undefined, args.concat([value])); | ||
}), _defineProperty(_TYPE_MAP, _applyTypes.PRE, function (fn, target, value) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) { | ||
args[_key2 - 3] = arguments[_key2]; | ||
} | ||
// Partials are slightly different. They partial an existing function | ||
// on the object referenced by string name. | ||
partial: function partial(fn, target, value) { | ||
for (var _len3 = arguments.length, args = Array(_len3 > 3 ? _len3 - 3 : 0), _key3 = 3; _key3 < _len3; _key3++) { | ||
args[_key3 - 3] = arguments[_key3]; | ||
} | ||
return fn.apply(undefined, [value].concat(args)); | ||
}), _defineProperty(_TYPE_MAP, _applyTypes.PARTIAL, function (fn, target, value) { | ||
for (var _len3 = arguments.length, args = Array(_len3 > 3 ? _len3 - 3 : 0), _key3 = 3; _key3 < _len3; _key3++) { | ||
args[_key3 - 3] = arguments[_key3]; | ||
} | ||
return fn.apply(undefined, [resolveFunction(args[0], target)].concat(_toConsumableArray(args.slice(1)))); | ||
}, | ||
return fn.apply(undefined, [resolveFunction(args[0], target)].concat(_toConsumableArray(args.slice(1)))); | ||
}), _defineProperty(_TYPE_MAP, _applyTypes.WRAP, function (fn, target, value) { | ||
return fn(resolveFunction(arguments[3], target), value); | ||
}), _defineProperty(_TYPE_MAP, _applyTypes.REPLACE, function (fn, target, value) { | ||
for (var _len4 = arguments.length, args = Array(_len4 > 3 ? _len4 - 3 : 0), _key4 = 3; _key4 < _len4; _key4++) { | ||
args[_key4 - 3] = arguments[_key4]; | ||
} | ||
// Wrap is a different case since the original function value | ||
// needs to be given to the wrap method. | ||
wrap: function wrap(fn, target, value) { | ||
return fn(resolveFunction(arguments[3], target), value); | ||
}, | ||
replace: function replace(fn, target, value) { | ||
for (var _len4 = arguments.length, args = Array(_len4 > 3 ? _len4 - 3 : 0), _key4 = 3; _key4 < _len4; _key4++) { | ||
args[_key4 - 3] = arguments[_key4]; | ||
} | ||
return fn.apply(undefined, args); | ||
}), _defineProperty(_TYPE_MAP, _applyTypes.COMPOSE, function (fn, target, value) { | ||
for (var _len5 = arguments.length, args = Array(_len5 > 3 ? _len5 - 3 : 0), _key5 = 3; _key5 < _len5; _key5++) { | ||
args[_key5 - 3] = arguments[_key5]; | ||
} | ||
return fn.apply(undefined, args); | ||
}, | ||
return fn.apply(undefined, [value].concat(_toConsumableArray(args.map(function (method) { | ||
return resolveFunction(method, target); | ||
})))); | ||
}), _defineProperty(_TYPE_MAP, _applyTypes.PARTIALED, function (fn, target, value) { | ||
for (var _len6 = arguments.length, args = Array(_len6 > 3 ? _len6 - 3 : 0), _key6 = 3; _key6 < _len6; _key6++) { | ||
args[_key6 - 3] = arguments[_key6]; | ||
} | ||
// Calls the function with key functions and the value | ||
compose: function compose(fn, target, value) { | ||
for (var _len5 = arguments.length, args = Array(_len5 > 3 ? _len5 - 3 : 0), _key5 = 3; _key5 < _len5; _key5++) { | ||
args[_key5 - 3] = arguments[_key5]; | ||
} | ||
return _lodash.partial.apply(undefined, [fn, value].concat(args)); | ||
}), _TYPE_MAP); | ||
return fn.apply(undefined, [value].concat(_toConsumableArray(args.map(function (method) { | ||
return resolveFunction(method, target); | ||
})))); | ||
}, | ||
partialed: function partialed(fn, target, value) { | ||
for (var _len6 = arguments.length, args = Array(_len6 > 3 ? _len6 - 3 : 0), _key6 = 3; _key6 < _len6; _key6++) { | ||
args[_key6 - 3] = arguments[_key6]; | ||
} | ||
TYPE_MAP[_applyTypes.SINGLE] = TYPE_MAP[_applyTypes.PRE]; | ||
return _lodash.partial.apply(undefined, [fn, value].concat(args)); | ||
} | ||
}; | ||
TYPE_MAP.single = TYPE_MAP.pre; | ||
function resolveFunction(method, target) { | ||
@@ -94,5 +87,6 @@ return (0, _lodash.isFunction)(method) ? method : target[method]; | ||
*/ | ||
function copyMetaData(from, to) { | ||
(0, _lodash.forOwn)(from, function (value, key) { | ||
return to[key] = from[key]; | ||
return to[key] = value; | ||
}); | ||
@@ -109,7 +103,8 @@ } | ||
*/ | ||
function createDecorator(root, method) { | ||
var type = arguments[2] === undefined ? 'pre' : arguments[2]; | ||
return type === 'single' ? wrapper() : wrapper; | ||
function createDecorator(method) { | ||
var type = arguments[1] === undefined ? _applyTypes.PRE : arguments[1]; | ||
return type === _applyTypes.SINGLE ? wrapper() : wrapper; | ||
function wrapper() { | ||
@@ -126,6 +121,6 @@ for (var _len7 = arguments.length, args = Array(_len7), _key7 = 0; _key7 < _len7; _key7++) { | ||
var toWrap = isGetter(get) ? get : get.call(this); | ||
descriptor.get = TYPE_MAP[type].apply(TYPE_MAP, [root[method], target, toWrap].concat(args)); | ||
descriptor.get = TYPE_MAP[type].apply(TYPE_MAP, [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)); | ||
descriptor.value = TYPE_MAP[type].apply(TYPE_MAP, [method, target, value].concat(args)); | ||
copyMetaData(value, descriptor.value); | ||
@@ -139,6 +134,6 @@ } | ||
function createInstanceDecorator(root, method) { | ||
var type = arguments[2] === undefined ? 'pre' : arguments[2]; | ||
function createInstanceDecorator(method) { | ||
var type = arguments[1] === undefined ? 'pre' : arguments[1]; | ||
return type === 'single' ? wrapper() : wrapper; | ||
return type === _applyTypes.SINGLE ? wrapper() : wrapper; | ||
@@ -161,4 +156,12 @@ function wrapper() { | ||
return { get: getter, configurable: true }; | ||
return { get: getter, set: setter, configurable: true }; | ||
function setter(value) { | ||
Object.defineProperty(this, name, { | ||
configurable: true, | ||
value: value, | ||
writable: writable | ||
}); | ||
} | ||
function getter() { | ||
@@ -171,3 +174,3 @@ var isGetter = Boolean(getter[getterAnnotation]); | ||
newDescriptor.get = action.apply(undefined, [root[method], this, toWrap].concat(args)); | ||
newDescriptor.get = action.apply(undefined, [method, this, toWrap].concat(args)); | ||
copyMetaData(toWrap, newDescriptor.get); | ||
@@ -180,3 +183,3 @@ | ||
newDescriptor.value = action.apply(undefined, [root[method], this, value].concat(args)); | ||
newDescriptor.value = action.apply(undefined, [method, this, value].concat(args)); | ||
copyMetaData(value, newDescriptor.value); | ||
@@ -192,7 +195,11 @@ | ||
exports['default'] = { | ||
createDecorator: createDecorator, | ||
createInstanceDecorator: createInstanceDecorator, | ||
copyMetaData: copyMetaData | ||
}; | ||
module.exports = exports['default']; | ||
// Methods where the function is the last argument or the first | ||
// and all other arguments come before or after. | ||
// Partials are slightly different. They partial an existing function | ||
// on the object referenced by string name. | ||
// Wrap is a different case since the original function value | ||
// needs to be given to the wrap method. | ||
// Calls the function with key functions and the value |
@@ -7,4 +7,8 @@ 'use strict'; | ||
var _INSTANCE, _PROTO, _methods; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _defineProperty(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } | ||
var _lodash = require('lodash'); | ||
@@ -22,2 +26,6 @@ | ||
var _tap = require('./tap'); | ||
var _tap2 = _interopRequireDefault(_tap); | ||
var _decoratorFactory = require('./decoratorFactory'); | ||
@@ -29,18 +37,6 @@ | ||
var methods = { | ||
instance: { | ||
single: ['once'], | ||
pre: ['debounce', 'throttle', 'memoize'], | ||
post: ['after', 'before'] | ||
}, | ||
proto: { | ||
single: ['spread', 'rearg', 'negate'], | ||
pre: ['ary', 'curry', 'curryRight', 'restParam'], | ||
partial: ['partial', 'partialRight'], | ||
wrap: ['wrap'], | ||
compose: ['compose', 'flow', 'flowRight', 'backflow'], | ||
partialed: ['delay', 'defer'] | ||
} | ||
}; | ||
var _applyTypes = require('./applyTypes'); | ||
var methods = (_methods = {}, _defineProperty(_methods, _applyTypes.INSTANCE, (_INSTANCE = {}, _defineProperty(_INSTANCE, _applyTypes.SINGLE, ['once']), _defineProperty(_INSTANCE, _applyTypes.PRE, ['debounce', 'throttle', 'memoize']), _defineProperty(_INSTANCE, _applyTypes.POST, ['after', 'before']), _INSTANCE)), _defineProperty(_methods, _applyTypes.PROTO, (_PROTO = {}, _defineProperty(_PROTO, _applyTypes.SINGLE, ['spread', 'rearg', 'negate']), _defineProperty(_PROTO, _applyTypes.PRE, ['ary', 'curry', 'curryRight', 'restParam']), _defineProperty(_PROTO, _applyTypes.PARTIAL, ['partial', 'partialRight']), _defineProperty(_PROTO, _applyTypes.WRAP, ['wrap']), _defineProperty(_PROTO, _applyTypes.COMPOSE, ['compose', 'flow', 'flowRight', 'backflow']), _defineProperty(_PROTO, _applyTypes.PARTIALED, ['delay', 'defer']), _PROTO)), _methods); | ||
var result = {}; | ||
@@ -51,3 +47,3 @@ | ||
result = list.reduce(function (res, fnName) { | ||
res[fnName] = createType === 'instance' ? (0, _decoratorFactory.createInstanceDecorator)(_lodash2['default'], fnName, type) : (0, _decoratorFactory.createDecorator)(_lodash2['default'], fnName, type); | ||
res[fnName] = createType === _applyTypes.INSTANCE ? (0, _decoratorFactory.createInstanceDecorator)(_lodash2['default'][fnName], type) : (0, _decoratorFactory.createDecorator)(_lodash2['default'][fnName], type); | ||
@@ -62,3 +58,4 @@ return res; | ||
getter: _getter2['default'], | ||
bind: _bind2['default'] | ||
bind: _bind2['default'], | ||
tap: _tap2['default'] | ||
}); | ||
@@ -65,0 +62,0 @@ |
{ | ||
"name": "lodash-decorators", | ||
"author": "Steven Sojka", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"engines": { | ||
@@ -38,4 +38,4 @@ "node": ">=0.12.0" | ||
"dependencies": { | ||
"lodash": "^3.6.0" | ||
"lodash": "^3.9.0" | ||
} | ||
} |
@@ -71,2 +71,3 @@ # lodash-decorators | ||
- `negate` | ||
- `tap` | ||
@@ -76,2 +77,3 @@ #### Example | ||
```javascript | ||
import { uniqueId } from 'lodash'; | ||
import { once } from 'lodash-decorators' | ||
@@ -89,3 +91,12 @@ | ||
} | ||
@tap | ||
popIt(list) { | ||
list.pop(); | ||
} | ||
} | ||
const person = new Person(); | ||
person.popIt([1, 2, 3]); //=> [1, 2] | ||
``` | ||
@@ -92,0 +103,0 @@ |
20861
11
281
315
Updatedlodash@^3.9.0