1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Use a own clone function instead of using JSON.parse(JSON.stringify())

This will also clone functions
This commit is contained in:
Ruben Bridgewater
2016-01-21 22:36:07 +01:00
parent 60eee34de1
commit 518e46dcc7
2 changed files with 34 additions and 8 deletions

View File

@ -38,9 +38,36 @@ function print (err, reply) {
var redisErrCode = /^([A-Z]+)\s+(.+)$/;
// Deep clone arbitrary objects with arrays. Can't handle cyclic structures (results in a range error)
function clone (obj) {
if (obj) {
var copy;
if (obj.constructor === Array) {
copy = new Array(obj.length);
for (var i = 0; i < obj.length; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}
if (obj.constructor === Object) {
copy = {};
for (var elem in obj) {
if (!obj.hasOwnProperty(elem)) {
// Do not add non own properties to the cloned object
continue;
}
copy[elem] = clone(obj[elem]);
}
return copy;
}
}
return obj;
}
module.exports = {
reply_to_strings: replyToStrings,
reply_to_object: replyToObject,
print: print,
err_code: redisErrCode
err_code: redisErrCode,
clone: clone
};