All files index.js

100% Statements 61/61
94.12% Branches 32/34
100% Functions 6/6
100% Lines 60/60

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 1051x               1x   1x 41x 1x     40x   2x 1x 2x       1x 1x 1x 1x   1x     38x 38x 38x 53x   53x 1x 1x 1x 2x 2x 1x     1x 52x 4x 4x 4x 8x   4x 48x 3x 3x 3x 3x 6x   45x 4x 4x 8x       41x 6x 6x 6x 6x 4x     6x   35x       53x 5x     33x     1x     2x     1x       1x 1x     1x 1x  
const opts = {
    seperator: '.',
    comma: ',',
    equal: '=',
    arrayMap: '*',
    pipe: '|',
    invoke: '&',
};
const fmtr = {};
 
const dig = function (object, structure, options = opts, formatter = fmtr) {
    if (!object || !structure) {
        return null;
    }
 
    if (typeof structure === 'object') {
 
        if (Array.isArray(structure)) {
            return structure.map(
                (struct) => dig(object, struct, options, formatter)
            );
        }
 
        const keys = Object.keys(structure);
        const result = {};
        for (const key of keys) {
            result[key] = dig(object, structure[key], options, formatter);
        }
        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.pipe) !== -1) {
            const keys = notation.split(options.pipe);
            let result = null;
            for (const key of keys) {
                result = dig(target, key, options, formatter);
                if (result) {
                    break;
                }
            }
            target = result;
        } else if (notation.indexOf(options.comma) !== -1) {
            const keys = notation.split(options.comma);
            const result = {};
            for (const key of keys) {
                result[key] = dig(target, key, options, formatter);
            }
            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], options, formatter)
            );
        } else {
 
            if (notation.indexOf(options.invoke) !== -1) {
                const invokeList = notation.split(options.invoke);
                let result = dig(target, invokeList.shift(), options, formatter);
                for (const invoke of invokeList) {
                    if (typeof formatter[invoke] === 'function') {
                        result = formatter[invoke](result, object);
                    }
                }
                target = result;
            } else {
                target = target[notation];
            }
        }
 
        if (target == null || target == undefined) {
            return null;
        }
    }
    return target;
};
 
const output = {
    dig,
    setFormatter: function (newFormatter) {
        Object.assign(fmtr, newFormatter);
    },
    setOptions: function (newOpts) {
        Object.assign(opts, newOpts);
    },
};
 
Eif (typeof window !== 'undefined') {
    Object.assign(window, output);
}
 
Eif (typeof process !== 'undefined') {
    module.exports = output;
}