diff --git a/wherehows-web/app/utils/array.ts b/wherehows-web/app/utils/array.ts index abe87760c0..8edbb89596 100644 --- a/wherehows-web/app/utils/array.ts +++ b/wherehows-web/app/utils/array.ts @@ -1,4 +1,4 @@ -import { identity } from 'wherehows-web/utils/helpers/functions'; +import { identity, not } from 'wherehows-web/utils/helpers/functions'; /** * Composable function that will in turn consume an item from a list an emit a result of equal or same type @@ -22,37 +22,53 @@ const take = (n: number = 0) => (list: Array): Array => Array.prototype /** * 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 + * @param {(param: T) => U} predicate 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); +const arrayMap = (predicate: (param: T) => U): ((array: Array) => Array) => (array = []) => + array.map(predicate); + +/** + * Partitions an array into a tuple containing elements that meet the predicate in the zeroth index, + * and excluded elements in the next + * `iterate-first data-last` function + * @template T type of source element list + * @template U subtype of T in first partition + * @param {(param: T) => param is U} predicate is a type guard function + * @returns {((array: Array) => [Array, Array>])} + */ +const arrayPartition = ( + predicate: (param: T) => param is U +): ((array: Array) => [Array, Array>]) => (array = []) => [ + array.filter(predicate), + array.filter>((v: T): v is Exclude => not(predicate)(v)) +]; /** * Convenience utility takes a type-safe filter function, and returns a list filtering function - * @param {(param: T) => boolean} filtrationFunction + * @param {(param: T) => boolean} predicate * @return {(array: Array) => Array} */ -const arrayFilter = (filtrationFunction: (param: T) => boolean): ((array: Array) => Array) => (array = []) => - array.filter(filtrationFunction); +const arrayFilter = (predicate: (param: T) => boolean): ((array: Array) => Array) => (array = []) => + array.filter(predicate); /** * Type safe utility `iterate-first data-last` function for array every * @template T - * @param {(param: T) => boolean} filter + * @param {(param: T) => boolean} predicate * @returns {((array: Array) => boolean)} */ -const arrayEvery = (filter: (param: T) => boolean): ((array: Array) => boolean) => (array = []) => - array.every(filter); +const arrayEvery = (predicate: (param: T) => boolean): ((array: Array) => boolean) => (array = []) => + array.every(predicate); /** * Type safe utility `iterate-first data-last` function for array some * @template T - * @param {(param: T) => boolean} filter + * @param {(param: T) => boolean} predicate * @return {(array: Array) => boolean} */ -const arraySome = (filter: (param: T) => boolean): ((array: Array) => boolean) => (array = []) => - array.some(filter); +const arraySome = (predicate: (param: T) => boolean): ((array: Array) => boolean) => (array = []) => + array.some(predicate); /** * Composable reducer abstraction, curries a reducing iteratee and returns a reducing function that takes a list @@ -228,9 +244,10 @@ export { Many, Iteratee }; export { take, arrayMap, + arrayPipe, arrayFilter, arrayReduce, - arrayPipe, + arrayPartition, isListUnique, compact, arrayEvery,