@hgargg-0710/one
Version:
A tiny npm library purposed for providing beautiful solutions to frequent miniature (one-line/one-expression) tasks for various JS datatypes.
95 lines (94 loc) • 4.04 kB
TypeScript
import { Tuple } from "../array/array.js";
/**
* Provided with a list of functions, lazily executes them in sequence
* and returns the first truthy result.
* If all results are falsy - returns the last one
*/
export declare const or: (...fs: Function[]) => (...x: any[]) => any;
/**
* Provided with a list of functions, lazily executes them in sequence
* and returns the first falsy result.
* If all results are truthy - returns the last one
*/
export declare const and: (...fs: Function[]) => (...x: any[]) => any;
/**
* Returns the function composition of the `fs` functions.
*/
export declare const trivialCompose: (...fs: Function[]) => (...x: any[]) => any;
/**
* Returns the array, containing the values of `f(j * i)` for `0 <= i <= floor(n / j)`
*/
export declare const iterations: (f: Function, n: number, j?: number) => any[];
/**
* Creates a new array `X` = [init],
* that is then filled with `n` iterations of `f(last(X), i, X)`,
* and returned
*/
export declare const sequence: (f: Function, n: number) => (init: any) => any[];
/**
* Makes the calls `f(0), f(1), ..., f(n)`
*/
export declare const repeat: (f: Function, n: number) => void;
/**
* Returns a composition, such that expected output of the given functions are arrays
* intended to be spread out as inputs for the next function.
*/
export declare const arrayCompose: (...fs: Function[]) => (...x: any[]) => any;
/**
* Creates and returns a `Map`, filled with key-value pairs of `[x, f(x)]` with `x` being in `keys`
*/
export declare const cache: <KeyType = any, FunctionType extends (...args: any[]) => any = (...args: any[]) => any>(f: FunctionType, keys: KeyType[]) => Map<KeyType, ReturnType<typeof f>>;
/**
* Breaks the given inputs' array `x` into (possibly intersecting) segments using `x.slice(inds[i])`,
* then - calls the respective function with each segment, and returns all of the calls'
* return values in an array.
*
* A missing `x.slice`-interval defaults to `[]` (whole signature copying)
*/
export declare const tupleSlice: <FunctionTuple extends Function[] = Function[]>(...fs: FunctionTuple) => (...inds: ([number?, number?] | undefined | null)[]) => (...x: any[]) => Tuple<any, FunctionTuple["length"]>;
/**
* Similar to `tupleSlice`, only difference being that `inds` are now predicates,
* using which the signatures for each particular function are `x.filter(inds[i])`-ed
*
* Like with `tupleSlice`, the default is copying of the entire signature `.filter(T)`
*/
export declare const tuplePick: <FunctionTuple extends Function[] = Function[]>(...fs: FunctionTuple) => (...inds: (((value?: any, index?: number, array?: any[]) => any) | null | undefined)[]) => (...x: any[]) => Tuple<any, FunctionTuple["length"]>;
/**
* Returns a function, the inputs of which are cached on each call.
*/
export declare const cached: (base: Function) => {
(x: any): any;
cache: Map<any, any>;
};
/**
* Identity function
*/
export declare const id: <Type = any>(x: Type) => Type;
/**
* No-op function
*/
export declare const nil: () => void;
/**
* Creates a new function calling `f`, with arguments at positions defined by `indexes` Set
* filled with values from `values`, and the remaining arguments filled with values from 'x'.
*/
export declare const argFiller: (f: Function) => (...indexes: number[]) => (...values: any[]) => (...x: any[]) => any;
/**
* Returns `f.bind(bound)`. `bound` defaults to `null`
*/
export declare const copy: (f: Function, bound?: any) => Function;
/**
* Returns a function returning `set.has(x)`
*/
export declare const has: (set: Set<any>) => (x: any) => boolean;
/**
* Returns a function that removes last `n` arguments
* (or, all of them, if it is greater than `args.length`) from `args`,
* and calls `f` on the result
*/
export declare const argWaster: (f: Function) => (n?: number) => (...args: any[]) => any;
/**
* Returns `(...x) => !f(...x)`
*/
export declare const negate: (f: (...args: any[]) => any) => ((...args: any[]) => boolean);
export * from "./constant.js";