Code coverage report for lib/checker.js

Statements: 100% (23 / 23)      Branches: 100% (8 / 8)      Functions: 100% (5 / 5)      Lines: 100% (23 / 23)      Ignored: none     

All files » lib/ » checker.js
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                1 18 30 30   10                           1 9 9 18 18   10     9 2                           1 33 15 5   15   33 6       1 1 1
/**
 * 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
 */
function allMustPass(items, check, result) {
  items.forEach(function (item) {
    try {
      result.addSuccess(check(item));
    } catch (e) {
      result.addFailure(e.message);
    }
  });
}
 
/**
 * 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) {
  if (setup[attribute]) {
    if (!Array.isArray(setup[attribute])) {
      setup[attribute] = [setup[attribute]];
    }
    allMustPass(setup[attribute], check, result);
  }
  if (setup[attribute + '-or']) {
    oneMustPass(setup[attribute + '-or'], check, result);
  }
}
 
exports.allMustPass = allMustPass;
exports.oneMustPass = oneMustPass;
exports.checkAttribute = checkAttribute;