All files checker.js

3.03% Statements 1/33
0% Branches 0/14
0% Functions 0/9
3.03% Lines 1/33

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    1x                                                                                                                                                                                  
'use strict';
 
var async = require('async');
 
/**
 * All items must pass check function.
 * All successes and failures will be added to Result.
 *
 * @param {Array} items: an array of items to be checked
 * @param {Function} check: check function
 * @param {Object} result: check Result
 * @param {Function} cb: callback
 */
function allMustPass(items, check, result, cb) {
    // if 'check' is an async func with callback
    if (check.length === 2) {
        async.every(items, function (item, done) {
            check(item, function (err, res) {
                if (err) {
                    result.addFailure(err.message);
                    return done(null, false);
                }
                result.addSuccess(res);
                done(null, true);
            });
        }, function () {
            cb();
        });
    } else {
        items.forEach(function (item) {
            try {
                result.addSuccess(check(item));
            } catch (e) {
                result.addFailure(e.message);
            }
            cb();
        });
    }
}
 
/**
 * At least one item must pass check function.
 * All successes will be added to Result.
 * Failures will only be added to Result if there's no success.
 *
 * @param {Array} items: an array of items to be checked
 * @param {Function} check: check function
 * @param {Object} result: check Result
 */
function oneMustPass(items, check, result) {
    var failures = [];
    items.forEach(function (item) {
        try {
            result.addSuccess(check(item));
        } catch (e) {
            failures.push(e.message);
        }
    });
    if (!result.hasSuccess()) {
        result.addFailures(failures);
    }
}
 
/**
 * Check whether an attribute exists in setup file.
 * - if <attribute> exists in setup, then all attribute values must pass the check
 * - if <attribute>-or exists in setup, then either one of the attribute values must pass the check
 *
 * @param {String} attribute: attribute name to check in the setup
 * @param {Object} setup: health setup
 * @param {Function} check: check function
 * @param {Object} result: check Result
 */
function checkAttribute(attribute, setup, check, result, cb) {
    cb = cb || function () {};
 
    if (setup[attribute]) {
        if (!Array.isArray(setup[attribute])) {
            setup[attribute] = [setup[attribute]];
        }
        allMustPass(setup[attribute], check, result, cb);
    } else if (setup[attribute + '-or']) {
        oneMustPass(setup[attribute + '-or'], check, result);
    } else {
        cb();
    }
}
 
exports.allMustPass = allMustPass;
exports.oneMustPass = oneMustPass;
exports.checkAttribute = checkAttribute;