ts-prime
Version:
A utility library for JavaScript and Typescript.
47 lines (46 loc) • 1.32 kB
text/typescript
/**
* Creates a function with `data-first` and `data-last` signatures.
*
* `purry` is a dynamic function and it's not type safe. It should be wrapped by a function that have proper typings.
* Refer to the example below for correct usage.
*
* @param fn the function to purry.
* @param args the arguments
* @signature P.purry(fn, arguments);
* @example
* function _findIndex(array, fn) {
* for (let i = 0; i < array.length; i++) {
* if (fn(array[i])) {
* return i;
* }
* }
* return -1;
* }
*
* // data-first
* function findIndex<T>(array: T[], fn: (item: T) => boolean): number;
*
* // data-last
* function findIndex<T>(fn: (item: T) => boolean): (array: T[]) => number;
*
* function findIndex() {
* return P.purry(_findIndex, arguments);
* }
* @category Function
*/
export function purry(fn: any, args: IArguments | readonly any[], lazy?: any) {
const diff = fn.length - args.length;
const arrayArgs = Array.from(args);
if (diff === 0) {
return fn(...arrayArgs);
}
if (diff === 1) {
const ret: any = (data: any) => fn(data, ...arrayArgs);
if (lazy || fn.lazy) {
ret.lazy = lazy || fn.lazy;
ret.lazyArgs = args;
}
return ret;
}
throw new Error('Wrong number of arguments');
}