1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/lib/utils.js
Ruben Bridgewater 5f261c5823 Minor changes
Move utility functions in lib/utils.js
Improve the js parser in cases the buffer is incomplete
Rename lib/parser to lib/parsers

Fix smaller issues with test suite and fix parser errors not being catched

Fixed wrong test for the new .end flush parameter
Fixed test suite options being partly mutated
Add some more tests
2015-09-25 02:12:48 +02:00

54 lines
1.1 KiB
JavaScript

'use strict';
// hgetall converts its replies to an Object. If the reply is empty, null is returned.
function replyToObject(reply) {
var obj = {}, j, jl, key, val;
if (reply.length === 0 || !Array.isArray(reply)) {
return null;
}
for (j = 0, jl = reply.length; j < jl; j += 2) {
key = reply[j].toString('binary');
val = reply[j + 1];
obj[key] = val;
}
return obj;
}
function replyToStrings(reply) {
var i;
if (Buffer.isBuffer(reply)) {
return reply.toString();
}
if (Array.isArray(reply)) {
for (i = 0; i < reply.length; i++) {
// Recusivly call the function as slowlog returns deep nested replies
reply[i] = replyToStrings(reply[i]);
}
return reply;
}
return reply;
}
function toArray(args) {
var len = args.length,
arr = new Array(len), i;
for (i = 0; i < len; i += 1) {
arr[i] = args[i];
}
return arr;
}
module.exports = {
reply_to_strings: replyToStrings,
reply_to_object: replyToObject,
to_array: toArray
};