All files helpers.js

96.55% Statements 28/29
60% Branches 6/10
100% Functions 5/5
96.3% Lines 26/27

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 105 106 107 108 109 110 111                          48x 48x   48x 2262x       2262x     42x                         8x 8x   8x 506x 506x       8x                     2712x 2712x 2712x                           871x   871x   2732x 261x       2731x 266x     2731x 2712x       870x                   3265x   1x              
/**
 * Map Iterable
 * ==============
 * Allows any iterable object to be mapped with correct callback parameters
 * eg. callback(currentValue, index, sourceIterable)
 * @ignore
 * @param {any[]} iterable
 * @param {Function} callback
 * @param {Object} [options]
 * @param {Boolean} [options.useEmptyElements=true] - use empty elements of the array ?
 * @return {any[]}
 */
function mapIterable(iterable, callback, { useEmptyElements = true } = {}) {
	const tasks = [],
		length = iterable.length;
 
	for (let index = 0; index < length; index++) {
		Iif (!useEmptyElements && !(index in iterable)) {
			continue;
		}
 
		tasks.push(callback(iterable[index], index, iterable));
	}
 
	return tasks;
}
 
/**
 * Filter Iterable
 * ===============
 * Allows any iterable object to be filtered using an array as a check list
 * @ignore
 * @param {any[]} iterable
 * @param {any[]} checks
 * @return {any[]}
 */
function filterIterable(iterable, checks) {
	const result = [];
	let index = 0;
 
	for (let item of iterable) {
		Eif (checks[index++]) {
			result.push(item);
		}
	}
 
	return result;
}
 
/**
 * Swap items in array
 * @ignore
 * @param {any[]} items
 * @param {Number} leftIndex
 * @param {Number} rightIndex
 */
function swapItems(items, leftIndex, rightIndex) {
	const leftItem = items[leftIndex];
	items[leftIndex] = items[rightIndex];
	items[rightIndex] = leftItem;
}
 
/**
 * Async partition an array for quick sort
 * @ignore
 * @async
 * @param {any[]} items
 * @param {Number} leftIndex
 * @param {Number} rightIndex
 * @param {Function} compare
 * @return {Promise<Number>} leftIndex after partition
 */
async function asyncPartition(items, leftIndex, rightIndex, compare) {
	const pivot = items[Math.floor((leftIndex + rightIndex) / 2)];
 
	while (leftIndex <= rightIndex) {
		// eslint-disable-next-line no-await-in-loop
		while ((await compare(items[leftIndex], pivot)) < 0) {
			leftIndex++;
		}
 
		// eslint-disable-next-line no-await-in-loop
		while ((await compare(items[rightIndex], pivot)) > 0) {
			rightIndex--;
		}
 
		if (leftIndex <= rightIndex) {
			swapItems(items, leftIndex++, rightIndex--);
		}
	}
 
	return leftIndex;
}
 
/**
 * Compares two items by unicode
 * @ignore
 * @param {any} a
 * @param {any} b
 * @return {Number} -1, 0, 1
 */
const compareByUnicode = (a, b) => String(a).localeCompare(String(b));
 
module.exports = {
	mapIterable,
	filterIterable,
	swapItems,
	asyncPartition,
	compareByUnicode
};