Skip to content

Commit 64aec70

Browse files
committed
Refactor support for using url instead of options to reduce cyclomatic complexity
1 parent 89920ef commit 64aec70

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

index.js

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function redisStore(args) {
7575

7676
try {
7777
// allow undefined only if allowed by isCacheableValue
78-
if(! ( (result === undefined || result === "undefined") && typeof args.isCacheableValue === 'function' && args.isCacheableValue(result))) {
78+
if(! ( (result === undefined || result === 'undefined') && typeof args.isCacheableValue === 'function' && args.isCacheableValue(result))) {
7979
result = JSON.parse(result);
8080
}
8181
} catch (e) {
@@ -97,35 +97,55 @@ function redisStore(args) {
9797
* but with host, port, db, ttl and auth_pass properties overridden by those provided in args.url.
9898
*/
9999
function getFromUrl(args) {
100-
if (!args || !args.url || typeof args.url !== 'string') return args;
100+
if (!args || typeof args.url !== 'string') {
101+
return args;
102+
}
101103

102104
try {
103105
var options = redisUrl.parse(args.url);
104-
// make a copy so we don't change input args
105-
var newArgs = {};
106-
for(var key in args){
107-
if (key && args.hasOwnProperty(key)) {
108-
newArgs[key] = args[key];
109-
}
110-
}
111-
112-
newArgs.isCacheableValue = args.isCacheableValue && args.isCacheableValue.bind(newArgs);
113-
newArgs.host = options.hostname;
114-
newArgs.port = parseInt(options.port, 10);
115-
newArgs.db = parseInt(options.database, 10);
116-
newArgs.auth_pass = options.password;
117-
if(options.query && options.query.ttl){
118-
newArgs.ttl = parseInt(options.query.ttl, 10);
119-
}
120-
121-
return newArgs;
106+
// make a clone so we don't change input args
107+
return applyOptionsToArgs(args, options);
122108
} catch (e) {
123109
//url is unparsable so returning original
124110
return args;
125111
}
126112

127113
}
128114

115+
/**
116+
* Clones args'es own properties to a new object and sets isCacheableValue on the new object
117+
* @param {Object} args
118+
* @returns {Object} a clone of the args object
119+
*/
120+
function cloneArgs(args) {
121+
var newArgs = {};
122+
for(var key in args){
123+
if (key && args.hasOwnProperty(key)) {
124+
newArgs[key] = args[key];
125+
}
126+
}
127+
newArgs.isCacheableValue = args.isCacheableValue && args.isCacheableValue.bind(newArgs);
128+
return newArgs;
129+
}
130+
131+
/**
132+
* Apply some options like hostname , port, db, ttl auth_pass from options to newArgs host, port, db, auth_pass and ttl and return clone of args
133+
* @param {Object} args
134+
* @param {Object} options
135+
* @returns {Object} clone of args param with properties set to those of options
136+
*/
137+
function applyOptionsToArgs(args, options) {
138+
var newArgs = cloneArgs(args);
139+
newArgs.host = options.hostname;
140+
newArgs.port = parseInt(options.port, 10);
141+
newArgs.db = parseInt(options.database, 10);
142+
newArgs.auth_pass = options.password;
143+
if(options.query && options.query.ttl){
144+
newArgs.ttl = parseInt(options.query.ttl, 10);
145+
}
146+
return newArgs;
147+
}
148+
129149
/**
130150
* Get a value for a given key.
131151
* @method get

0 commit comments

Comments
 (0)