# All files / utils/objectHelpers objectHelpers.js

100% Statements 5/5
100% Branches 4/4
100% Functions 3/3
100% Lines 2/2

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

    33344x

    8259x

     /*
    * Allows access to nested JavaScript objects with a string key.
    * Example usage:
    * resolveToProperty("style.width", document.body)
    */
    const resolveToProperty = (path, obj) => (typeof path !== 'string' ? undefined : path.split('.').reduce((prev, curr) => (prev ? prev[curr] : undefined), obj));

    /**
     * Returns true if path is represented as a nested prop. Otherwise false.
     * @param {string} path the string representation of a path to the nested property.
     */
    const isKeyNestedProp = path => /[a-zA-Z_](\w*\.[a-z_]\w*)+/i.test(path);

    export { resolveToProperty, isKeyNestedProp };