memoizee-decorator
Advanced tools
Comparing version 1.0.0 to 1.0.2
@@ -6,2 +6,7 @@ 'use strict'; | ||
}); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; /** | ||
* @copyright 2015, Andrey Popp <8mayday@gmail.com> | ||
*/ | ||
exports.default = memoize; | ||
@@ -15,12 +20,3 @@ | ||
function transformDescriptor(target, name, descriptor) { | ||
if (typeof descriptor.value == 'function') { | ||
descriptor.value = (0, _memoizee2.default)(descriptor.value, this.options); | ||
} else if (typeof descriptor.get == 'function') { | ||
descriptor.get = (0, _memoizee2.default)(descriptor.get, this.options); | ||
} else { | ||
throw new Error('decorator can only be applied to methods or getters'); | ||
} | ||
return descriptor; | ||
} | ||
var SENTINEL = {}; | ||
@@ -31,11 +27,61 @@ function memoize() { | ||
// no arguments | ||
if (arguments.length == 3) { | ||
return transformDescriptor.apply(scope, arguments); | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (args.length == 3) { | ||
return _memoize.apply(scope, args); | ||
// has options argument | ||
} else if (arguments.length == 1) { | ||
scope.options = arguments[0]; | ||
return transformDescriptor.bind(scope); | ||
} else if (args.length == 1) { | ||
scope.options = args[0]; | ||
return _memoize.bind(scope); | ||
} else { | ||
throw new Error('Unknown number of arguments'); | ||
} | ||
}; | ||
}; | ||
function _memoize(target, name, descriptor) { | ||
if (typeof descriptor.value === 'function') { | ||
return _memoizeMethod(target, name, descriptor, this.options); | ||
} else if (typeof descriptor.get === 'function') { | ||
return _memoizeGetter(target, name, descriptor, this.options); | ||
} else { | ||
throw new Error('@memoize decorator can be applied to methods or getters, got ' + String(descriptor.value) + ' instead'); | ||
} | ||
} | ||
function _memoizeGetter(target, name, descriptor, options) { | ||
var memoizedName = '_memoized_' + name; | ||
var _get = descriptor.get; | ||
target[memoizedName] = SENTINEL; | ||
return _extends({}, descriptor, { | ||
get: function get() { | ||
if (this[memoizedName] === SENTINEL) { | ||
this[memoizedName] = (0, _memoizee2.default)(_get, options); | ||
} | ||
return this[memoizedName].call(this); | ||
} | ||
}); | ||
} | ||
function _memoizeMethod(target, name, descriptor, options) { | ||
var memoizedName = '_memoized_' + name; | ||
var _value = descriptor.value; | ||
target[memoizedName] = SENTINEL; | ||
return _extends({}, descriptor, { | ||
value: function value() { | ||
var _memoizedName; | ||
if (this[memoizedName] === SENTINEL) { | ||
this[memoizedName] = (0, _memoizee2.default)(_value, options); | ||
} | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return (_memoizedName = this[memoizedName]).call.apply(_memoizedName, [this].concat(args)); | ||
} | ||
}); | ||
} |
@@ -7,3 +7,3 @@ 'use strict'; | ||
var _index = require('./index.js'); | ||
var _index = require('./index'); | ||
@@ -46,4 +46,7 @@ var _index2 = _interopRequireDefault(_index); | ||
var Foo = (_dec = (0, _index2.default)({ length: 1 }), (_class = function () { | ||
function Foo() { | ||
function Foo(num) { | ||
_classCallCheck(this, Foo); | ||
this._myNum = num; | ||
this.calculateCount = 0; | ||
} | ||
@@ -54,40 +57,59 @@ | ||
value: function bar1() { | ||
var num = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 42; | ||
var num = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 999; | ||
console.log('Complicated calculations...'); | ||
return num; | ||
this.calculateCount++; | ||
return this._myNum + '-' + num; | ||
} | ||
}, { | ||
key: 'bar2', | ||
value: function bar2() { | ||
var num = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 95; | ||
value: function bar2(object) { | ||
console.log('Complicated calculations...'); | ||
return num; | ||
this.calculateCount++; | ||
return this._myNum + '-' + JSON.stringify(object); | ||
} | ||
}, { | ||
key: 'bar3', | ||
key: 'myNum', | ||
get: function get() { | ||
console.log('Complicated calculations...'); | ||
return 42; | ||
this.calculateCount++; | ||
return this._myNum; | ||
} | ||
}, { | ||
key: 'calcCount', | ||
get: function get() { | ||
return "(" + this.calculateCount + ")"; | ||
} | ||
}]); | ||
return Foo; | ||
}(), (_applyDecoratedDescriptor(_class.prototype, 'bar1', [_dec], Object.getOwnPropertyDescriptor(_class.prototype, 'bar1'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'bar2', [_index2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'bar2'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'bar3', [_index2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'bar3'), _class.prototype)), _class)); | ||
}(), (_applyDecoratedDescriptor(_class.prototype, 'bar1', [_dec], Object.getOwnPropertyDescriptor(_class.prototype, 'bar1'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'bar2', [_index2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'bar2'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'myNum', [_index2.default], Object.getOwnPropertyDescriptor(_class.prototype, 'myNum'), _class.prototype)), _class)); | ||
var foo = new Foo(); | ||
var foo = new Foo(100); | ||
var foo2 = new Foo(50); | ||
console.log('Testing: bar1'); | ||
console.log(foo.bar1()); | ||
console.log(foo.bar1(1)); | ||
console.log(foo.bar1(1)); | ||
console.log('>> Testing: foo.bar1'); | ||
console.log(foo.bar1(), foo.calcCount); | ||
console.log(foo.bar1(), foo.calcCount); | ||
console.log(foo.bar1(3), foo.calcCount); | ||
console.log('Testing: bar2'); | ||
console.log(foo.bar2(5)); | ||
console.log(foo.bar2(2)); //will produce incorrect result -- you need to specify argument length option for bar2 because optional arguments are ignored by default | ||
console.log('>> Testing: foo2.bar1'); | ||
console.log(foo2.bar1(4), foo2.calcCount); | ||
console.log(foo2.bar1(4), foo2.calcCount); | ||
console.log(foo2.bar1(5), foo2.calcCount); | ||
console.log('Testing: bar3'); | ||
console.log(foo.bar3); | ||
console.log(foo.bar3); | ||
var obj = { hello: 'world' }; | ||
console.log('>> Testing: foo.bar2'); | ||
console.log(foo.bar2(obj), foo.calcCount); | ||
console.log(foo.bar2(obj), foo.calcCount); | ||
console.log(foo.bar2({ farewell: 'world' }), foo.calcCount); | ||
console.log('>> Testing: foo.myNum'); | ||
console.log(foo.myNum, foo.calcCount); | ||
console.log(foo.myNum, foo.calcCount); | ||
console.log('>> Testing: foo2.myNum'); | ||
console.log(foo2.myNum, foo2.calcCount); | ||
console.log(foo2.myNum, foo2.calcCount); |
{ | ||
"name": "memoizee-decorator", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"description": "ES7 decorator for memoizee library", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
@@ -20,2 +19,3 @@ "build": "babel src --out-dir dist --copy-files", | ||
], | ||
"main": "lib/index.js", | ||
"author": "zwigglers <zwigglers@gmail.com>", | ||
@@ -27,10 +27,12 @@ "license": "MIT", | ||
"homepage": "https://github.com/zwigglers/memoizee-decorator#readme", | ||
"dependencies": { | ||
"memoizee": "^0.4.11" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel": "^5.0.12", | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-transform-decorators-legacy": "^1.3.4", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-es2015": "^6.24.1" | ||
}, | ||
"dependencies": { | ||
"memoizee": "^0.4.11" | ||
} | ||
} |
# memoizee-decorator | ||
ES7 decorator for memoizee library | ||
ES7 function decorator wrapper for the memoizee library (https://github.com/medikoo/memoizee). | ||
Based on memoize-decorator (https://github.com/andreypopp/memoize-decorator). Using memoizee enables support for functions with arguments. | ||
Also passes through memoizee options. For functions with parameters with default values, you have to set the length option explicitly, or use the option { length: false } | ||
## Usage | ||
```js | ||
import memoize from 'memoizee-decorator'; | ||
class Foo { | ||
@memoize | ||
get bar() { | ||
console.log('Complicated calculations...'); | ||
return 42; | ||
} | ||
@memoize({length:1}) | ||
someFunc(num=21) { | ||
console.log('Complicated calculations...'); | ||
return num; | ||
} | ||
} | ||
``` | ||
Can be used either plain ___@memoize___ or with options ___@memoize({...options})___ | ||
As with all decorators, you have to use a transpiler. Refer to https://github.com/andreypopp/autobind-decorator#autobind-decorator for prerequisites. |
8942
154
31
5
5