All files / src/helper util.js

75% Statements 9/12
100% Branches 4/4
40% Functions 2/5
90% Lines 9/10
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                2x         2x 4x 1x     3x 1x     2x     2x                 2x  
/**
 * Simple findBy recursive utility
 *
 * @param {array} array The array to look into
 * @param {string} by The key to compare
 * @param {string} value The value to compare
 * @returns {null|*} Null or the value found
 */
export const findBy = (array, by, value) => {
	/**
	 * @param {number} index The current index
	 * @returns {null|*} Null or the value found
	 */
	const r = index => {
		if (array[index][by] === value) {
			return array[index]
		}
 
		if (++index > array.length - 1) {
			return null
		}
 
		return r(index)
	}
 
	return r(0)
}
 
/**
 * Pipe async functions
 *
 * @param {object} fns The fns
 * @returns {function(*=): *} The result
 */
export const pipe = (...fns) => x =>
	fns.reduce((prev, f) => prev.then(f), Promise.resolve(x))