All files index.js

100% Statements 40/40
100% Branches 19/19
100% Functions 5/5
100% Lines 39/39

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 721x             1x 22x 1x     21x   2x 1x 2x       1x 1x 1x 1x   1x     19x 19x 19x 31x   31x 2x 2x 2x 4x   2x   29x 3x 3x 3x 3x 6x     26x 2x 2x 4x       24x     31x 2x     17x     1x     1x    
let opts = {
    seperator: '.',
    comma: ',',
    equal: '=',
    arrayMap: '*',
};
 
const dig = function (object, structure, options = opts) {
    if (!object) {
        return null;
    }
 
    if (typeof structure === 'object') {
 
        if (Array.isArray(structure)) {
            return structure.map(
                (struct) => dig(object, struct)
            );
        }
 
        const keys = Object.keys(structure);
        const result = {};
        for (const key of keys) {
            result[key] = dig(object, structure[key]);
        }
        return result;
    }
 
    let target = object;
    let args = structure.split(options.seperator);
    for (let index = 0; index < args.length; index += 1) {
        const notation = args[index];
 
        if (notation.indexOf(options.comma) !== -1) {
            const keys = notation.split(options.comma);
            const result = {};
            for (const key of keys) {
                result[key] = dig(object, key);
            }
            target = result;
        }
        else if (notation.indexOf(options.equal) !== -1) {
            const comparison = notation.split(options.equal);
            const key = comparison[0];
            const value = comparison[1];
            target = target.find(
                (t) => key ? t[key] == value : t == value
            );
        }
        else if (notation === options.arrayMap) {
            index += 1;
            target = target.map(
                (t) => dig(t, args.slice(index)[0])
            );
        }
        else {
            target = target[notation];
        }
 
        if (target == null || target == undefined) {
            return null;
        }
    }
    return target;
};
 
module.exports = {
    dig,
    setOptions: function (newOpts) {
        Object.assign(opts, newOpts);
    },
};