Code coverage report for app/utils/array_utils.js

Statements: 93.94% (31 / 33)      Branches: 85% (17 / 20)      Functions: 80% (8 / 10)      Lines: 93.94% (31 / 33)      Ignored: none     

All files » app/utils/ » array_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 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    1                                     1 36 124       1           1               2 2 8 8 5     2                     15 15 15   15 57     15                     18 18 18 18 18 18 52 7   45 9     2 1   1            
'use strict';
 
;require.register("utils/array_utils", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  function _parseId(id) {
    return id.replace(/[^\d|\.]/g, '').split('.').map(function (i) {
      return parseInt(i, 10);
    });
  }
 
  var flatten = function flatten(list) {
    return list.reduce(function (a, b) {
      return a.concat(Array.isArray(b) ? flatten(b) : b);
    }, []);
  };
 
  module.exports = {
    /**
     *
     * @param arr {Array}
     * @param id
     * @return result {Array}
     */
    uniqObjectsbyId: function uniqObjectsbyId(arr, id) {
      var result = [];
      arr.forEach(function (item) {
        var isIdPresent = result.someProperty(id, item[id]);
        if (!isIdPresent) {
          result.pushObject(item);
        }
      });
      return result;
    },
 
    /**
     * intersect two arrays and return the common values
     *
     * @param  {Array} arr1 - first array
     * @param  {Array} arr2 - second array
     * @return {Array} intersection - the intersection of arr1 and arr2
     */
    intersect: function intersect(arr1, arr2) {
      var intersection = [];
      var shortest = arr1.length >= arr2.length ? arr2 : arr1;
      var longest = arr1.length >= arr2.length ? arr1 : arr2;
 
      shortest.forEach(function (entry) {
        longest.indexOf(entry) > -1 ? intersection.push(entry) : null;
      });
 
      return intersection;
    },
 
    /**
     * Callback for sorting models with `id`-property equal to something like version number: 'HDP-1.2.3', '4.2.52' etc
     *
     * @param {{id: string}} obj1
     * @param {{id: string}} obj2
     * @returns {number}
     */
    sortByIdAsVersion: function sortByIdAsVersion(obj1, obj2) {
      var id1 = _parseId(Em.get(obj1, 'id'));
      var id2 = _parseId(Em.get(obj2, 'id'));
      var lId1 = id1.length;
      var lId2 = id2.length;
      var limit = lId1 > lId2 ? lId2 : lId1;
      for (var i = 0; i < limit; i++) {
        if (id1[i] > id2[i]) {
          return 1;
        }
        if (id1[i] < id2[i]) {
          return -1;
        }
      }
      if (lId1 === lId2) {
        return 0;
      }
      return lId1 > lId2 ? 1 : -1;
    },
 
    flatten: flatten
 
  };
});