/** * Convenience utility takes a type-safe mapping function, and returns a list mapping function * @param {(param: T) => U} mappingFunction maps a single type T to type U * @return {(array: Array) => Array} */ const arrayMap = (mappingFunction: (param: T) => U): ((array: Array) => Array) => (array = []) => array.map(mappingFunction); /** * Convenience utility takes a type-safe filter function, and returns a list filtering function * @param {(param: T) => boolean} filtrationFunction * @return {(array: Array) => Array} */ const arrayFilter = (filtrationFunction: (param: T) => boolean): ((array: Array) => Array) => (array = []) => array.filter(filtrationFunction); /** * Composable reducer abstraction, curries a reducing iteratee and returns a reducing function that takes a list * @template U * @param {(acc: U) => U} iteratee * @param {U} init the initial value in the reduction sequence * @return {(arr: Array) => U} */ const arrayReduce = ( iteratee: (accumulator: U, element: T, index: number, collection: Array) => U, init: U ): ((arr: Array) => U) => (array = []) => array.reduce(iteratee, init); /** * Duplicate check using every to short-circuit iteration * @template T * @param {Array} [list = []] list to check for dupes * @return {boolean} true is unique */ const isListUnique = (list: Array = []): boolean => new Set(list).size === list.length; /** * Extracts all non falsey values from a list. * @template T * @param {Array} list the list of items to compact * @return {Array} */ const compact = (list: Array = []): Array => list.filter(item => item); export { arrayMap, arrayFilter, arrayReduce, isListUnique, compact };