All files / web/bundles/pimui/js/common property.js

85.71% Statements 12/14
100% Branches 8/8
60% Functions 3/5
92.31% Lines 12/13

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                  1161x 1161x                     32113x 32113x   32113x 32113x 5612x     26501x                             2027x   2027x 2027x       2027x                              
'use strict';
 
/**
 * Property accessor extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @copyright 2016 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define([], function () {
    return {
        /**
         * Access a property in an object
         *
         * @param {object} data
         * @param {string} path
         * @param {*}  defaultValue
         *
         * @return {*}
         */
        accessProperty: function (data, path, defaultValue) {
            defaultValue = defaultValue || null;
            const pathPart = path.split('.');
 
            const part = pathPart[0].replace(/__DOT__/g, '.');
            if (undefined === data[part]) {
                return defaultValue;
            }
 
            return 1 === pathPart.length ?
                data[part] :
                this.accessProperty(data[part], pathPart.slice(1).join('.'), defaultValue);
        },
 
        /**
         * Update a property in an object
         *
         * @param {object} data
         * @param {string} path
         * @param {*}  value
         *
         * @return {*}
         */
        updateProperty: function (data, path, value) {
            var pathPart = path.split('.');
 
            const part = pathPart[0].replace(/__DOT__/g, '.');
            data[part] = 1 === pathPart.length ?
                value :
                this.updateProperty(data[part], pathPart.slice(1).join('.'), value);
 
            return data;
        },
 
        /**
         * Create a safe path by concatenating escaped path segments to avoid dots of being incorrectly interpreted
         *
         * @param Array path
         *
         * @returns String
         */
        propertyPath: function(path) {
            return path.map(e => e.replace(/\./g, '__DOT__')).join('.');
        }
    };
});