lodash-decorators
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "lodash-decorators", | ||
"author": "Steven Sojka", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=0.12.0" |
'use strict'; | ||
import { isFunction } from 'lodash'; | ||
import settings from './settings'; | ||
@@ -24,2 +25,4 @@ const TYPE_MAP = { | ||
TYPE_MAP.single = TYPE_MAP.pre; | ||
/** | ||
@@ -34,3 +37,5 @@ * Creates a generic decorator for a method on an object. | ||
function createDecorator(root, method, type = 'pre') { | ||
return function wrapper(...args) { | ||
return type === 'single' ? wrapper() : wrapper; | ||
function wrapper(...args) { | ||
return function decorator(target, name, descriptor) { | ||
@@ -52,24 +57,33 @@ const { value, get } = descriptor; | ||
function createInstanceDecorator(root, method, type = 'pre') { | ||
return function wrapper(...args) { | ||
return type === 'single' ? wrapper() : wrapper; | ||
function wrapper(...args) { | ||
return function decorator(target, name, descriptor) { | ||
const { value, get } = descriptor; | ||
const action = TYPE_MAP[type]; | ||
const getterAnnotation = `${settings.annotationPrefix}isGetter`; | ||
return { | ||
get: function getter() { | ||
const newDescriptor = { configurable: true, writable: true }; | ||
getter[getterAnnotation] = get[getterAnnotation]; | ||
if (isFunction(get)) { | ||
newDescriptor.get = action(root[method], this, get, ...args); | ||
Object.defineProperty(this, name, newDescriptor); | ||
return { get: getter }; | ||
return newDescriptor.get(); | ||
} | ||
function getter() { | ||
const isGetter = Boolean(getter[getterAnnotation]); | ||
const newDescriptor = { configurable: true }; | ||
newDescriptor.value = action(root[method], this, value, ...args); | ||
if (isFunction(get)) { | ||
const toWrap = isGetter ? get : get.call(this); | ||
newDescriptor.get = action(root[method], this, toWrap, ...args); | ||
Object.defineProperty(this, name, newDescriptor); | ||
return newDescriptor.value; | ||
return isGetter ? newDescriptor.get() : newDescriptor.get; | ||
} | ||
}; | ||
newDescriptor.value = action(root[method], this, value, ...args); | ||
Object.defineProperty(this, name, newDescriptor); | ||
return newDescriptor.value; | ||
} | ||
}; | ||
@@ -76,0 +90,0 @@ }; |
'use strict'; | ||
import _ from 'lodash'; | ||
import getter from './getter'; | ||
import { createDecorator, createInstanceDecorator } from './decoratorFactory'; | ||
import settings from './settings'; | ||
const methods = { | ||
instance: { | ||
single: [ | ||
'once' | ||
], | ||
pre: [ | ||
'debounce', | ||
'throttle', | ||
'memoize', | ||
'once' | ||
'memoize' | ||
], | ||
@@ -20,7 +24,9 @@ post: [ | ||
proto: { | ||
single: [ | ||
'rearg', | ||
'negate' | ||
], | ||
pre: [ | ||
'curry', | ||
'curryRight', | ||
'negate', | ||
'rearg' | ||
'curryRight' | ||
], | ||
@@ -35,3 +41,4 @@ partial: [ | ||
compose: [ | ||
'compose' | ||
'compose', | ||
'flow' | ||
] | ||
@@ -55,2 +62,10 @@ } | ||
export default result; | ||
// All other decorators | ||
_.assign(result, { getter }); | ||
// Provide aliases @memoize => @Memoize | ||
// This is for users who prefer capitalized decorators and | ||
// can prevent naming collissions. | ||
_.forOwn(result, (value, key) => result[_.capitalize(key)] = value); | ||
export default _.assign(result, { settings }); |
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
8684
16
229