'use strict';
const { addCondition, Report } = require( '../report.js' );
/**
* @instance
* @memberOf conditions
* @method forEach
* @desc Checks that a nested contract holds for each element of an array.
* @param {string} description
* @param {Array} array List of items.
* @param {Contract} nested First argument given to the callback
* is a Report object, and the second one is the array item in question.
* @returns {undefined}
*/
addCondition( 'forEach',
// TODO better name that rhymes with the ordered one (map/reduce?)
{ fun: 1, args: 2 },
(list, contract) => {
if (!Array.isArray(list))
return 'Expected a list, found a '.typeof(list);
if (list.length < 1)
return 0; // auto-pass
const ok = new Report();
list.forEach( (item, index) => ok.nested( 'item ' + index, item, contract ) );
return ok.done();
}
);
/**
* @instance
* @memberOf conditions
* @method ordered
* @desc Checks that a nested contract holds for each pair
* of adjacent element of an array (i.e. 1&2, 2&3, 3&4, ...).
* @param {string} description
* @param {Array} array List of items.
* @param {Contract} nested First argument given to the callback
* is a Report object, and the second and third ones
* are the array items in question.
* @returns {undefined}
*/
// TODO this is called "compliant chain" but better just say here
// "oh we're checking element order"
addCondition( 'ordered',
// TODO better name? pairwise? reduce?
{ fun: 1, args: 2 },
(list, contract) => {
if (!Array.isArray(list))
return 'Expected a list, found a '.typeof(list);
if (list.length < 2)
return 0; // auto-pass
const ok = new Report();
for (let n = 0; n < list.length - 1; n++)
ok.nested( 'items ' + n + ', ' + (n + 1), list[n], list[n + 1], contract);
return ok.done();
}
);