You've already forked node-redis
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:
29
lib/utils.js
29
lib/utils.js
@ -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
|
||||
};
|
||||
|
Reference in New Issue
Block a user