# Code coverage report for lib/utils.js

## Statements: 100% (30 / 30) Branches: 100% (10 / 10) Functions: 100% (4 / 4) Lines: 100% (30 / 30) Ignored: none

All files » lib/ » utils.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
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67

    13
    132

    132
    24

    108
    248
    248
    248

    108

    13
    33417

    33417
    8376

    25041
    706
    706

    6862

    706

    24335

    13
    4420

    4420
    5768

    4420

    13
    8
    4

    4

    13

    13

     '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)) {
            var res = new Array(reply.length);
            for (i = 0; i < reply.length; i++) {
                // Recusivly call the function as slowlog returns deep nested replies
                res[i] = replyToStrings(reply[i]);
            }
            return res;
        }

        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;
    }

    function print (err, reply) {
        if (err) {
            console.log('Error: ' + err);
        } else {
            console.log('Reply: ' + reply);
        }
    }

    var redisErrCode = /^([A-Z]+)\s+(.+)$/;

    module.exports = {
        reply_to_strings: replyToStrings,
        reply_to_object: replyToObject,
        to_array: toArray,
        print: print,
        errCode: redisErrCode
    };