# all files / src/js/base/core/ lists.js

87.14% Statements 61/70
75% Branches 15/20
88.89% Functions 16/18
90.91% Lines 60/66

    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
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    2111×
    1×

    1×
    2271×

    1×
    1118×

    1×
    1×

    1×
    1232×

    1×
    79×
    80×
    80×
    77×

    1×
    506×
    532×
    326×

    180×

    1×
    124×

    1×
    124×

    1×
    2×
    2×
    6×

    1×
    1211×
    1211×
    1211×
    1211×
    2234×

    1211×

    1×
    342×

    1×
    10×
    10×
    10×
    8×
    8×
    3×

    5×

    8×

    1×
    2×
    2×
    9×

    2×

    1×
    32×

    32×
    76×
    40×

    32×

    1×

    1×

    1×

     import $ from 'jquery';
    import func from './func';

    /**
     * returns the first item of an array.
     *
     * @param {Array} array
     */
    function head(array) {
      return array[0];
    }

    /**
     * returns the last item of an array.
     *
     * @param {Array} array
     */
    function last(array) {
      return array[array.length - 1];
    }

    /**
     * returns everything but the last entry of the array.
     *
     * @param {Array} array
     */
    function initial(array) {
      return array.slice(0, array.length - 1);
    }

    /**
     * returns the rest of the items in an array.
     *
     * @param {Array} array
     */
    function tail(array) {
      return array.slice(1);
    }

    /**
     * returns item of array
     */
    function find(array, pred) {
      for (let idx = 0, len = array.length; idx < len; idx++) {
        const item = array[idx];
        if (pred(item)) {
          return item;
        }
      }
    }

    /**
     * returns true if all of the values in the array pass the predicate truth test.
     */
    function all(array, pred) {
      for (let idx = 0, len = array.length; idx < len; idx++) {
        if (!pred(array[idx])) {
          return false;
        }
      }
      return true;
    }

    /**
     * returns index of item
     */
    function indexOf(array, item) {
      return $.inArray(item, array);
    }

    /**
     * returns true if the value is present in the list.
     */
    function contains(array, item) {
      return indexOf(array, item) !== -1;
    }

    /**
     * get sum from a list
     *
     * @param {Array} array - array
     * @param {Function} fn - iterator
     */
    function sum(array, fn) {
      fn = fn || func.self;
      return array.reduce(function(memo, v) {
        return memo + fn(v);
      }, 0);
    }

    /**
     * returns a copy of the collection with array type.
     * @param {Collection} collection - collection eg) node.childNodes, ...
     */
    function from(collection) {
      const result = [];
      const length = collection.length;
      let idx = -1;
      while (++idx < length) {
        result[idx] = collection[idx];
      }
      return result;
    }

    /**
     * returns whether list is empty or not
     */
    function isEmpty(array) {
      return !array || !array.length;
    }

    /**
     * cluster elements by predicate function.
     *
     * @param {Array} array - array
     * @param {Function} fn - predicate function for cluster rule
     * @param {Array[]}
     */
    function clusterBy(array, fn) {
      Iif (!array.length) { return []; }
      const aTail = tail(array);
      return aTail.reduce(function(memo, v) {
        const aLast = last(memo);
        if (fn(last(aLast), v)) {
          aLast[aLast.length] = v;
        } else {
          memo[memo.length] = [v];
        }
        return memo;
      }, [[head(array)]]);
    }

    /**
     * returns a copy of the array with all false values removed
     *
     * @param {Array} array - array
     * @param {Function} fn - predicate function for cluster rule
     */
    function compact(array) {
      const aResult = [];
      for (let idx = 0, len = array.length; idx < len; idx++) {
        if (array[idx]) { aResult.push(array[idx]); }
      }
      return aResult;
    }

    /**
     * produces a duplicate-free version of the array
     *
     * @param {Array} array
     */
    function unique(array) {
      const results = [];

      for (let idx = 0, len = array.length; idx < len; idx++) {
        if (!contains(results, array[idx])) {
          results.push(array[idx]);
        }
      }

      return results;
    }

    /**
     * returns next item.
     * @param {Array} array
     */
    function next(array, item) {
      const idx = indexOf(array, item);
      if (idx === -1) { return null; }

      return array[idx + 1];
    }

    /**
     * returns prev item.
     * @param {Array} array
     */
    function prev(array, item) {
      const idx = indexOf(array, item);
      if (idx === -1) { return null; }

      return array[idx - 1];
    }

    /**
     * @class core.list
     *
     * list utils
     *
     * @singleton
     * @alternateClassName list
     */
    export default {
      head,
      last,
      initial,
      tail,
      prev,
      next,
      find,
      contains,
      all,
      sum,
      from,
      isEmpty,
      clusterBy,
      compact,
      unique
    };