All files / web/bundles/pimui/js oro-app.js

96.43% Statements 54/56
94.12% Branches 32/34
100% Functions 9/9
96.43% Lines 54/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1701183x                   1183x                         1350x                   2742x 8721x 3785x 3785x 1646x   3785x   4936x 25x   4911x       2742x 2742x 2742x 4936x 4936x 4936x   4936x 4936x 4936x   2008x     2928x   4936x   4936x     2742x                     9872x 9872x   9872x                               2986x 2986x     17916x 17916x   17916x 7896x 7896x       2986x                     66204x 39467x 499x 998x       998x     499x     38968x   26737x 26737x   26737x 26530x     26737x 43467x 43467x 4023x       22714x                         61159x        
define(['jquery', 'underscore'],
    function ($, _) {
        'use strict';
 
        /**
         * Main Application
         *
         * @export oro/app
         * @name oro.app
         */
        return {
            /** @type {boolean} */
            debug: false,
 
            /**
             * Pack object to string
             *
             * Object {foo: 'x', 'bar': 'y'} will be converted to string "foo=x&bar=y".
             *
             * @param {Object} object
             * @return {String}
             */
            packToQueryString: function (object) {
                return $.param(object);
            },
 
            /**
             * Unpack string to object. Reverse from packToQueryString.
             *
             * @param {String} query
             * @return {Object}
             */
            unpackFromQueryString: function (query) {
                var setValue = function (root, path, value) {
                    if (path.length > 1) {
                        var dir = path.shift();
                        if (typeof root[dir] === 'undefined') {
                            root[dir] = path[0] === '' ? [] : {};
                        }
                        setValue(root[dir], path, value);
                    } else {
                        if (root instanceof Array) {
                            root.push(value);
                        } else {
                            root[path] = value;
                        }
                    }
                };
                var nvp = query.split('&');
                var data = {};
                for (var i = 0; i < nvp.length; i++) {
                    var pair  = nvp[i].split('=');
                    var name  = this._decodeComponent(pair[0]);
                    var value = this._decodeComponent(pair[1]);
 
                    var path = name.match(/(^[^\[]+)(\[.*\]$)?/);
                    var first = path[1];
                    if (path[2]) {
                        //case of 'array[level1]' || 'array[level1][level2]'
                        path = path[2].match(/(?=\[(.*)\]$)/)[1].split('][');
                    } else {
                        //case of 'name'
                        path = [];
                    }
                    path.unshift(first);
 
                    setValue(data, path, value);
                }
 
                return data;
            },
 
            /**
             * Decode URL encoded component
             *
             * @param {String} string
             * @return {String}
             * @protected
             */
            _decodeComponent: function (string) {
                var result = string.replace(/\+/g, '%20');
                result = decodeURIComponent(result);
 
                return result;
            },
 
            /**
             * Invert object keys.
             *
             * Example of usage:
             *
             * oro.app.invertKeys({foo: 'x', bar: 'y'}, {foo: 'f', bar: 'b'})
             * will return {f: 'x', b: 'y'}
             *
             * @param {Object} object
             * @param {Object} keys
             * @return {Object}
             */
            invertKeys: function (object, keys) {
                var result = _.extend({}, object);
                for (var key in keys) {
                    var mirrorKey;
                    var baseKey;
                    baseKey = key;
                    mirrorKey = keys[key];
 
                    if (baseKey in result) {
                        result[mirrorKey] = result[baseKey];
                        delete result[baseKey];
                    }
                }
 
                return result;
            },
 
            /**
             * Loosely compare two values
             *
             * @param {*} value1
             * @param {*} value2
             * @return {Boolean} TRUE if values are equal, otherwise - FALSE
             */
            isEqualsLoosely: function (value1, value2) {
                if (!_.isObject(value1)) {
                    if (_.isNumber(value1) || _.isNumber(value2)) {
                        var toNumber = function (v) {
                            Iif (_.isString(v) && v === '') {
                                return NaN;
                            }
 
                            return Number(v);
                        };
 
                        return (toNumber(value1) == toNumber(value2));
                    }
 
                    return ((value1 || '') == (value2 || ''));
 
                } else Eif (_.isObject(value1)) {
                    var valueKeys = _.keys(value1);
 
                    if (_.isObject(value2)) {
                        valueKeys = _.unique(valueKeys.concat(_.keys(value2)));
                    }
 
                    for (var index in valueKeys) {
                        var key = valueKeys[index];
                        if (!_.has(value2, key) || !this.isEqualsLoosely(value1[key], value2[key])) {
                            return false;
                        }
                    }
 
                    return true;
                } else {
                    return value1 == value2;
                }
            },
 
            /**
             * Deep clone a value
             *
             * @param {*} value
             * @return {*}
             */
            deepClone: function (value) {
                return $.extend(true, {}, value);
            }
        };
    });