-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (43 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @module cache-wrapper
*/
(function () {
"use strict";
var getAllMethods = function getAllMethods(object) {
return Object.getOwnPropertyNames(object).filter(function(property) {
return typeof object[property] == 'function';
});
};
var cacherTypes = {
'generator': 0,
'promise': 1
};
var detectCacherType = function detectCacherType(cacherType) {
return Number(cacherTypes[cacherType]);
};
var cachers = [require('co-cacher'),require('cacher-promise')];
/**
* Cahe wrapper
* @param {function} targetObject
* @param {number} expires
* @param {object} options now support 'cacherType' option ('generator' or 'promise'). By default = 'generator'
* @return {object}
*/
module.exports = function (targetObject, expires, options) {
var cacherIdx = 0,
salt = '';
if (typeof options == "object") {
cacherIdx = detectCacherType(options.cacherType);
salt = options.salt || '';
}
var redefinedMethods=getAllMethods(targetObject);
var object = {};
redefinedMethods.forEach(function(method) {
object[method] = function(){
var args = [].slice.call(arguments);
return cachers[cacherIdx](targetObject[method], args, { expires: expires, salt: method+salt });
};
});
return object;
}
}());