Code coverage report for app/utils/object_utils.js

Statements: 85.21% (121 / 142)      Branches: 68.99% (89 / 129)      Functions: 91.3% (21 / 23)      Lines: 87.05% (121 / 139)      Ignored: none     

All files » app/utils/ » object_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 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305    1   1                                     1   1   20                 1     14 88 20 6       8       4 1   3 1 5 5 7 5   2     5   3       1420 1420 1 1733 1733       1733 301     1432       1432 3     1429       1429       1429       1429       1429 12409   12409         1429 3070 1 3069     3069     313 313 313 5   308 308 308   2756 1356   1400       67     1420       1420 1420 1420 1420 1360       60       3 1   2 1 6 6 7 3   4     6   2                     22 22 22 22 22     22 3 3   3 3                             19 19 39     19   40 25   15 1   14           22                       6 6   6 1 2 2 2 1 1         1 1 1       1     5 11 4 7 4   3                     8 16 7   9   8 9 9                   2 2        
'use strict';
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
 
;require.register("utils/object_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.
   */
 
  var stringUtils = require('utils/string_utils');
 
  var types = {
    'get': function get(prop) {
      return Object.prototype.toString.call(prop);
    },
    'object': '[object Object]',
    'array': '[object Array]',
    'string': '[object String]',
    'boolean': '[object Boolean]',
    'number': '[object Number]'
  };
 
  module.exports = {
 
    isChild: function isChild(obj) {
      for (var k in obj) {
        if (obj.hasOwnProperty(k)) {
          if (obj[k] instanceof Object) {
            return false;
          }
        }
      }
      return true;
    },
 
    recursiveKeysCount: function recursiveKeysCount(obj) {
      if (!(obj instanceof Object)) {
        return null;
      }
      var self = this;
      function r(obj) {
        var count = 0;
        for (var k in obj) {
          if (self.isChild(obj[k])) {
            count++;
          } else {
            count += r(obj[k]);
          }
        }
        return count;
      }
      return r(obj);
    },
 
    deepEqual: function deepEqual() {
      var i, l, leftChain, rightChain;
      var values = arguments;
      function compare2Objects(x, y) {
        var p;
        Iif (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') {
          return true;
        }
 
        if (x === y) {
          return true;
        }
 
        Iif (typeof x === 'function' && typeof y === 'function' || x instanceof Date && y instanceof Date || x instanceof RegExp && y instanceof RegExp || x instanceof String && y instanceof String || x instanceof Number && y instanceof Number) {
          return x.toString() === y.toString();
        }
 
        if (!(x instanceof Object && y instanceof Object)) {
          return false;
        }
 
        Iif (x.isPrototypeOf(y) || y.isPrototypeOf(x)) {
          return false;
        }
 
        Iif (x.constructor !== y.constructor) {
          return false;
        }
 
        Iif (x.prototype !== y.prototype) {
          return false;
        }
 
        Iif (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) {
          return false;
        }
 
        for (p in y) {
          Iif (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
            return false;
          } else Iif (_typeof(y[p]) !== _typeof(x[p])) {
            return false;
          }
        }
 
        for (p in x) {
          if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) {
            return false;
          } else Iif (_typeof(y[p]) !== _typeof(x[p])) {
            return false;
          }
          switch (_typeof(x[p])) {
            case 'object':
            case 'function':
              leftChain.push(x);
              rightChain.push(y);
              if (!compare2Objects(x[p], y[p])) {
                return false;
              }
              leftChain.pop();
              rightChain.pop();
              break;
            default:
              if (x[p] !== y[p]) {
                return false;
              }
              break;
          }
        }
 
        return true;
      }
 
      Iif (arguments.length < 1) {
        return true;
      }
 
      for (i = 1, l = arguments.length; i < l; i++) {
        leftChain = [];
        rightChain = [];
        if (!compare2Objects(arguments[0], arguments[i])) {
          return false;
        }
      }
 
      return true;
    },
 
    recursiveTree: function recursiveTree(obj) {
      if (!(obj instanceof Object)) {
        return null;
      }
      var self = this;
      function r(obj, parent) {
        var leaf = '';
        for (var k in obj) {
          if (self.isChild(obj[k])) {
            leaf += k + ' (' + parent + ')' + '<br/>';
          } else {
            leaf += r(obj[k], parent + '/' + k);
          }
        }
        return leaf;
      }
      return r(obj, '');
    },
 
    /**
     *
     * @param {object|array|object[]} target
     * @param {object|array|object[]} source
     * @param {function} handler
     * @returns {object|array|object[]}
     */
    deepMerge: function deepMerge(target, source, handler) {
      Iif ((typeof target === 'undefined' ? 'undefined' : _typeof(target)) !== 'object' || (typeof source === 'undefined' ? 'undefined' : _typeof(source)) !== 'object') return target;
      var handlerOpts = Array.prototype.slice.call(arguments, 3);
      var isArray = Em.isArray(source);
      var ret = handler && typeof handler.apply(this, [target, source].concat(handlerOpts)) !== 'undefined' ? handler(target, source) : isArray ? [] : {};
      var self = this;
 
      // handle array
      if (isArray) {
        target = target || [];
        ret = ret.concat(target);
 
        Eif (types.object === types.get(target[0])) {
          ret = self.smartArrayObjectMerge(target, source);
        } else {
          for (var i = 0; i < source.length; i++) {
            if (typeof ret[i] === 'undefined') {
              ret[i] = source[i];
            } else if (_typeof(source[i]) === 'object') {
              ret[i] = this.deepMerge(target[i], source[i], handler, target, source);
            } else {
              if (target.indexOf(source[i]) === -1) {
                ret.push(source[i]);
              }
            }
          }
        }
      } else {
        Eif (target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object') {
          Em.keys(target).forEach(function (key) {
            ret[key] = target[key];
          });
        }
        Em.keys(source).forEach(function (key) {
          // handle value which is not Array or Object
          if (_typeof(source[key]) !== 'object' || !source[key]) {
            ret[key] = source[key];
          } else {
            if (!target[key]) {
              ret[key] = source[key];
            } else {
              ret[key] = self.deepMerge(target[key], source[key], handler, target, source);
            }
          }
        });
      }
 
      return ret;
    },
 
    /**
     * Find objects by index key (@see detectIndexedKey) and merge them.
     *
     * @param {object[]} target
     * @param {object[]} source
     * @returns {object[]}
     */
    smartArrayObjectMerge: function smartArrayObjectMerge(target, source) {
      // keep the first object and take all keys that contains primitive value
      var id = this.detectIndexedKey(target);
      var self = this;
      // when uniq key not found let's merge items by the key itself
      if (!id) {
        source.forEach(function (obj) {
          Em.keys(obj).forEach(function (objKey) {
            var ret = self.objectByRoot(objKey, target);
            if (!Em.isNone(ret)) {
              Eif ([types.object, types.array].contains(types.get(ret))) {
                target[objKey] = self.deepMerge(obj[objKey], ret);
              } else {
                target[objKey] = ret;
              }
            } else {
              var _obj = {};
              _obj[objKey] = obj[objKey];
              target.push(_obj);
            }
          });
        });
        return target;
      }
 
      return target.mapProperty(id).concat(source.mapProperty(id)).uniq().map(function (value) {
        if (!target.someProperty(id, value)) {
          return source.findProperty(id, value);
        } else if (!source.someProperty(id, value)) {
          return target.findProperty(id, value);
        }
        return self.deepMerge(target.findProperty(id, value), source.findProperty(id, value));
      });
    },
 
    /**
     * Determines key with uniq value. This key will be used to find correct objects in target and source to merge.
     *
     * @param {object} target
     * @returns {string|undefined}
     */
    detectIndexedKey: function detectIndexedKey(target) {
      var keys = Em.keys(target[0]).map(function (key) {
        if ([types.object, types.array].contains(types.get(target[0][key]))) {
          return null;
        }
        return key;
      }).compact();
      return keys.filter(function (key) {
        var values = target.mapProperty(key);
        return values.length === values.uniq().length;
      })[0];
    },
 
    /**
     *
     * @param {string} rootKey
     * @param {object[]} target
     */
    objectByRoot: function objectByRoot(rootKey, target) {
      return target.map(function (item) {
        return item[rootKey] || null;
      }).compact()[0];
    }
  };
});