All files implementationSelector.js

81.82% Statements 9/11
100% Branches 0/0
100% Functions 3/3
80% Lines 8/10

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                      2x 2x       2x                         2x 2x                             1510x 1512x           1x  
'use strict';
 
/**
 * Implementation of Knuth's Algorithm P for random shuffling. We don't want to
 * reorder implementations in-place (for now), so this function returns indices
 * into the original list.
 *
 * @param {number} numberOfElements an array of implementations
 * @return {Array[Number]} randomly-sorted indices
 */
function randomlyShuffledIndices( numberOfElements ) {
	const resultIndices = [ ...Array( numberOfElements ).keys() ];
	for ( let i = resultIndices.length - 1; i > 0; --i ) {
		const j = Math.ceiling( Math.random() * i );
		[ resultIndices[ j ], resultIndices[ i ] ] = [ resultIndices[ i ], resultIndices[ j ] ];
	}
	return resultIndices;
}
 
class RandomImplementationSelector {
 
	/**
	 * Generator function that invokes random shuffling for implementations.
	 * Per shuffle, it returns (yields) the index value of the implementation shuffled.
	 *
	 * @param {Array} implementations array of implementations
	 * @yield {number} index of each implementation per execution of shuffle
	 */
	* generate( implementations ) {
		for ( const index of randomlyShuffledIndices( implementations.length ) ) {
			yield implementations[ index ];
		}
	}
 
}
 
class FirstImplementationSelector {
 
	/**
	 * Generator function that invokes and then returns (yields) the implementation result.
	 *
	 * @param {Array} implementations array of implementations
	 * @yield {Object} result from the implementation
	 */
	* generate( implementations ) {
		for ( const implementation of implementations ) {
			yield implementation;
		}
	}
 
}
 
module.exports = { RandomImplementationSelector, FirstImplementationSelector };