Code coverage report for src/Common/Arr.js

Statements: 100% (25 / 25)      Branches: 100% (6 / 6)      Functions: 100% (7 / 7)      Lines: 100% (25 / 25)      Ignored: none     

All files » src/Common/ » Arr.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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143    1           1                         11 11   11 5   6                             3   3                           3                             4   4                                 7 7     7 3   4       7     9       7                                     7 7 7   7             1    
'use strict';
 
var _           = require("lodash"),
    Type        = require("./Type"),
    Safe        = require("./Safe");
 
 
 
var Arr = {
 
    /**
     * Get index value
     *
     * @param {Array}   array
     * @param {Number}  index
     *
     * @return {*}
     *
     */
    index: function(array, index){
 
        array = Safe.array(array);
        index = Safe.number(index);
 
        if(index < 0 || index > array.length - 1)
            return null;
 
        return array[index];
 
    },
 
    /**
     *
     * Get last value of the array
     *
     * @param {Array}   array
     *
     * @return {Object}
     *
     */
    last: function(array){
 
        array = Safe.array(array);
 
        return Arr.index(array, array.length - 1);
 
    },
 
    /*
     * Get first value of the array
     *
     * @param {Array}   array
     *
     * @return {Object}
     *
     */
    first: function(array){
 
        return Arr.index(array);
 
    },
 
    /**
     *
     * Returns the length of the array
     *
     * @param {Array}   array
     *
     * @return {Number}
     *
     */
    length: function(array){
 
        array = Safe.array(array);
 
        return array.length;
 
    },
 
    /**
     *
     * Inserts the value in the specified index of the array
     *
     * @param {Array}   array
     * @param {*}       value
     * @param {Number}  index
     *
     * @return {Array}
     *
     */
    insert: function(array, value, index){
 
        array = Safe.array(array);
        value = Safe.array(value);
 
        /* jshint -W041 */
        if(index == null){
            index = Arr.length(array);
        } else {
            index = Safe.number(index);
        }
 
        // merge the structures
        _.each(
            value.reverse(),
            function(item){
                array.splice(index, 0, item);
            }
        );
 
        return array;
 
    },
 
    /*
     * Remove index of the array. If no index is specified the first
     * element is removed.
     *
     * Returns the removed elements.
     *
     * @param {Array}   index
     * @param {Number}  index
     * @param {Number}  n
     *
     * @return {Array}
     *
     */
    remove: function(array, index, n){
 
        array   = Safe.array(array);
        index   = Safe.number(index, 0);
        n       = Safe.number(n, 1);
 
        return array.splice(index, n);
 
    }
 
};
 
 
module.exports = Arr;