adds typesafe iteratee-first-data-last util partition

This commit is contained in:
Seyi Adebajo 2018-07-24 13:33:27 -07:00
parent 1ba61e2fa5
commit 7a74e5b86c

View File

@ -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 * 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 = <T>(n: number = 0) => (list: Array<T>): Array<T> => Array.prototype
/** /**
* Convenience utility takes a type-safe mapping function, and returns a list mapping function * 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<T>) => Array<U>} * @return {(array: Array<T>) => Array<U>}
*/ */
const arrayMap = <T, U>(mappingFunction: (param: T) => U): ((array: Array<T>) => Array<U>) => (array = []) => const arrayMap = <T, U>(predicate: (param: T) => U): ((array: Array<T>) => Array<U>) => (array = []) =>
array.map(mappingFunction); 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<T>) => [Array<U>, Array<Exclude<T, U>>])}
*/
const arrayPartition = <T, U extends T>(
predicate: (param: T) => param is U
): ((array: Array<T>) => [Array<U>, Array<Exclude<T, U>>]) => (array = []) => [
array.filter(predicate),
array.filter<Exclude<T, U>>((v: T): v is Exclude<T, U> => not(predicate)(v))
];
/** /**
* Convenience utility takes a type-safe filter function, and returns a list filtering function * 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<T>) => Array<T>} * @return {(array: Array<T>) => Array<T>}
*/ */
const arrayFilter = <T>(filtrationFunction: (param: T) => boolean): ((array: Array<T>) => Array<T>) => (array = []) => const arrayFilter = <T>(predicate: (param: T) => boolean): ((array: Array<T>) => Array<T>) => (array = []) =>
array.filter(filtrationFunction); array.filter(predicate);
/** /**
* Type safe utility `iterate-first data-last` function for array every * Type safe utility `iterate-first data-last` function for array every
* @template T * @template T
* @param {(param: T) => boolean} filter * @param {(param: T) => boolean} predicate
* @returns {((array: Array<T>) => boolean)} * @returns {((array: Array<T>) => boolean)}
*/ */
const arrayEvery = <T>(filter: (param: T) => boolean): ((array: Array<T>) => boolean) => (array = []) => const arrayEvery = <T>(predicate: (param: T) => boolean): ((array: Array<T>) => boolean) => (array = []) =>
array.every(filter); array.every(predicate);
/** /**
* Type safe utility `iterate-first data-last` function for array some * Type safe utility `iterate-first data-last` function for array some
* @template T * @template T
* @param {(param: T) => boolean} filter * @param {(param: T) => boolean} predicate
* @return {(array: Array<T>) => boolean} * @return {(array: Array<T>) => boolean}
*/ */
const arraySome = <T>(filter: (param: T) => boolean): ((array: Array<T>) => boolean) => (array = []) => const arraySome = <T>(predicate: (param: T) => boolean): ((array: Array<T>) => boolean) => (array = []) =>
array.some(filter); array.some(predicate);
/** /**
* Composable reducer abstraction, curries a reducing iteratee and returns a reducing function that takes a list * Composable reducer abstraction, curries a reducing iteratee and returns a reducing function that takes a list
@ -228,9 +244,10 @@ export { Many, Iteratee };
export { export {
take, take,
arrayMap, arrayMap,
arrayPipe,
arrayFilter, arrayFilter,
arrayReduce, arrayReduce,
arrayPipe, arrayPartition,
isListUnique, isListUnique,
compact, compact,
arrayEvery, arrayEvery,