lodash-decorators
Advanced tools
Comparing version 0.0.9 to 0.1.0
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports['default'] = bindDecorator; | ||
exports['default'] = bindWrapper; | ||
@@ -17,3 +17,45 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function bindDecorator(target, name, descriptor) { | ||
function bindWrapper() { | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return function bindDecorator() { | ||
for (var _len2 = arguments.length, properties = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
properties[_key2] = arguments[_key2]; | ||
} | ||
return properties.length === 1 ? bindClass.apply(undefined, properties.concat(args)) : bindMethod.apply(undefined, properties.concat(args)); | ||
}; | ||
} | ||
; | ||
function bindClass(target, name, descriptor) { | ||
for (var _len3 = arguments.length, args = Array(_len3 > 3 ? _len3 - 3 : 0), _key3 = 3; _key3 < _len3; _key3++) { | ||
args[_key3 - 3] = arguments[_key3]; | ||
} | ||
var keys = Reflect.ownKeys(target.prototype).forEach(function (key) { | ||
if (key !== 'constructor') { | ||
var _descriptor = Object.getOwnPropertyDescriptor(target.prototype, key); | ||
if ((0, _lodash.isFunction)(_descriptor.value)) { | ||
Object.defineProperty(target.prototype, key, bindMethod(target, key, _descriptor)); | ||
} | ||
} | ||
}); | ||
} | ||
function checkBindValue(value) { | ||
if (!(0, _lodash.isFunction)(value)) { | ||
throw new Error('Binds can not be used on getters, setters, or properties'); | ||
} | ||
} | ||
function bindMethod(target, name, descriptor) { | ||
for (var _len4 = arguments.length, args = Array(_len4 > 3 ? _len4 - 3 : 0), _key4 = 3; _key4 < _len4; _key4++) { | ||
args[_key4 - 3] = arguments[_key4]; | ||
} | ||
var value = descriptor.value; | ||
@@ -23,32 +65,25 @@ var get = descriptor.get; | ||
var boundFn = undefined; | ||
return { | ||
configurable: true, | ||
get: function bindGetter() { | ||
var thisValue = value; | ||
if ((0, _lodash.isFunction)(get)) { | ||
boundFn = (0, _lodash.bind)(get, this); | ||
thisValue = get.call(this); | ||
} | ||
Object.defineProperty(this, name, { | ||
configurable: true, | ||
get: boundFn | ||
}); | ||
checkBindValue(thisValue); | ||
return boundFn(); | ||
} else { | ||
boundFn = (0, _lodash.bind)(value, this); | ||
var boundFn = _lodash.bind.apply(undefined, [thisValue, this].concat(args)); | ||
Object.defineProperty(this, name, { | ||
writable: writable, | ||
configurable: true, | ||
value: boundFn | ||
}); | ||
Object.defineProperty(this, name, { | ||
writable: writable, | ||
configurable: true, | ||
value: boundFn | ||
}); | ||
return boundFn; | ||
} | ||
return boundFn; | ||
} | ||
}; | ||
} | ||
; | ||
module.exports = exports['default']; |
{ | ||
"name": "lodash-decorators", | ||
"author": "Steven Sojka", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">=0.12.0" |
@@ -159,4 +159,3 @@ # lodash-decorators | ||
person.getFirstName(); // 'Joe' | ||
person.getLastName(); // 'Smith' | ||
person.getUpperCaseName(); // JOE SMITH | ||
``` | ||
@@ -242,3 +241,3 @@ | ||
Bind takes no arguments and binds the `Function` to | ||
Bind takes arguments based on lodash's bind and binds the `Function` to | ||
the current instance object. | ||
@@ -257,6 +256,12 @@ | ||
@bind | ||
@bind() | ||
getName() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
// It can also function as a partial | ||
@bind('Joe') | ||
getUpperCaseName(name) { | ||
return name.toUpperCase(); | ||
} | ||
} | ||
@@ -267,5 +272,30 @@ | ||
person.getName.call(null); // Joe Smith | ||
person.getUpperCaseName(); // JOE | ||
``` | ||
You can also bind entire classes. | ||
#### Example | ||
```javascript | ||
import { bind } from 'lodash-decorators' | ||
@bind() | ||
class Person { | ||
constructor(firstName, lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
getName() { | ||
return `${this.firstName} ${this.lastName}`; | ||
} | ||
} | ||
const person = new Person('Joe', 'Smith'); | ||
person.getName.call(null); // Joe Smith | ||
``` | ||
Author: Steven Sojka | ||
MIT Licensed |
@@ -6,5 +6,28 @@ 'use strict'; | ||
export default function bindDecorator(target, name, descriptor) { | ||
export default function bindWrapper(...args) { | ||
return function bindDecorator(...properties) { | ||
return properties.length === 1 ? bindClass(...properties, ...args) : bindMethod(...properties, ...args); | ||
}; | ||
}; | ||
function bindClass(target, name, descriptor, ...args) { | ||
const keys = Reflect.ownKeys(target.prototype).forEach(key => { | ||
if (key !== 'constructor') { | ||
let descriptor = Object.getOwnPropertyDescriptor(target.prototype, key); | ||
if (isFunction(descriptor.value)) { | ||
Object.defineProperty(target.prototype, key, bindMethod(target, key, descriptor)); | ||
} | ||
} | ||
}); | ||
} | ||
function checkBindValue(value) { | ||
if (!isFunction(value)) { | ||
throw new Error('Binds can not be used on getters, setters, or properties'); | ||
} | ||
} | ||
function bindMethod(target, name, descriptor, ...args) { | ||
const { value, get, writable } = descriptor; | ||
let boundFn; | ||
@@ -14,24 +37,21 @@ return { | ||
get: function bindGetter() { | ||
let thisValue = value; | ||
if (isFunction(get)) { | ||
boundFn = bind(get, this); | ||
thisValue = get.call(this); | ||
} | ||
Object.defineProperty(this, name, { | ||
configurable: true, | ||
get: boundFn | ||
}); | ||
checkBindValue(thisValue); | ||
return boundFn(); | ||
} else { | ||
boundFn = bind(value, this); | ||
let boundFn = bind(thisValue, this, ...args); | ||
Object.defineProperty(this, name, { | ||
writable, | ||
configurable: true, | ||
value: boundFn | ||
}); | ||
Object.defineProperty(this, name, { | ||
writable, | ||
configurable: true, | ||
value: boundFn | ||
}); | ||
return boundFn; | ||
} | ||
return boundFn; | ||
} | ||
} | ||
}; | ||
} |
31676
693
298