UNPKG

6.84 kBTypeScriptView Raw
1/**
2 * Higher order functions
3 *
4 * These utility functions are exported, but are subject to change without notice.
5 *
6 * @packageDocumentation
7 */
8import { Predicate } from './common';
9/**
10 * Returns a new function for [Partial Application](https://en.wikipedia.org/wiki/Partial_application) of the original function.
11 *
12 * Given a function with N parameters, returns a new function that supports partial application.
13 * The new function accepts anywhere from 1 to N parameters. When that function is called with M parameters,
14 * where M is less than N, it returns a new function that accepts the remaining parameters. It continues to
15 * accept more parameters until all N parameters have been supplied.
16 *
17 *
18 * This contrived example uses a partially applied function as an predicate, which returns true
19 * if an object is found in both arrays.
20 * @example
21 * ```
22 * // returns true if an object is in both of the two arrays
23 * function inBoth(array1, array2, object) {
24 * return array1.indexOf(object) !== -1 &&
25 * array2.indexOf(object) !== 1;
26 * }
27 * let obj1, obj2, obj3, obj4, obj5, obj6, obj7
28 * let foos = [obj1, obj3]
29 * let bars = [obj3, obj4, obj5]
30 *
31 * // A curried "copy" of inBoth
32 * let curriedInBoth = curry(inBoth);
33 * // Partially apply both the array1 and array2
34 * let inFoosAndBars = curriedInBoth(foos, bars);
35 *
36 * // Supply the final argument; since all arguments are
37 * // supplied, the original inBoth function is then called.
38 * let obj1InBoth = inFoosAndBars(obj1); // false
39 *
40 * // Use the inFoosAndBars as a predicate.
41 * // Filter, on each iteration, supplies the final argument
42 * let allObjs = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7 ];
43 * let foundInBoth = allObjs.filter(inFoosAndBars); // [ obj3 ]
44 *
45 * ```
46 *
47 * @param fn
48 * @returns {*|function(): (*|any)}
49 */
50export declare function curry(fn: Function): Function;
51/**
52 * Given a varargs list of functions, returns a function that composes the argument functions, right-to-left
53 * given: f(x), g(x), h(x)
54 * let composed = compose(f,g,h)
55 * then, composed is: f(g(h(x)))
56 */
57export declare function compose(): () => any;
58/**
59 * Given a varargs list of functions, returns a function that is composes the argument functions, left-to-right
60 * given: f(x), g(x), h(x)
61 * let piped = pipe(f,g,h);
62 * then, piped is: h(g(f(x)))
63 */
64export declare function pipe(...funcs: Function[]): (obj: any) => any;
65/**
66 * Given a property name, returns a function that returns that property from an object
67 * let obj = { foo: 1, name: "blarg" };
68 * let getName = prop("name");
69 * getName(obj) === "blarg"
70 */
71export declare const prop: (name: string) => (obj: any) => any;
72/**
73 * Given a property name and a value, returns a function that returns a boolean based on whether
74 * the passed object has a property that matches the value
75 * let obj = { foo: 1, name: "blarg" };
76 * let getName = propEq("name", "blarg");
77 * getName(obj) === true
78 */
79export declare const propEq: Function;
80/**
81 * Given a dotted property name, returns a function that returns a nested property from an object, or undefined
82 * let obj = { id: 1, nestedObj: { foo: 1, name: "blarg" }, };
83 * let getName = prop("nestedObj.name");
84 * getName(obj) === "blarg"
85 * let propNotFound = prop("this.property.doesnt.exist");
86 * propNotFound(obj) === undefined
87 */
88export declare const parse: (name: string) => any;
89/**
90 * Given a function that returns a truthy or falsey value, returns a
91 * function that returns the opposite (falsey or truthy) value given the same inputs
92 */
93export declare const not: (fn: Predicate<any>) => Predicate<any>;
94/**
95 * Given two functions that return truthy or falsey values, returns a function that returns truthy
96 * if both functions return truthy for the given arguments
97 */
98export declare function and(fn1: Predicate<any>, fn2: Predicate<any>): Predicate<any>;
99/**
100 * Given two functions that return truthy or falsey values, returns a function that returns truthy
101 * if at least one of the functions returns truthy for the given arguments
102 */
103export declare function or(fn1: Predicate<any>, fn2: Predicate<any>): Predicate<any>;
104/**
105 * Check if all the elements of an array match a predicate function
106 *
107 * @param fn1 a predicate function `fn1`
108 * @returns a function which takes an array and returns true if `fn1` is true for all elements of the array
109 */
110export declare const all: (fn1: Predicate<any>) => (arr: any[]) => boolean;
111export declare const any: (fn1: Predicate<any>) => (arr: any[]) => boolean;
112/** Given a class, returns a Predicate function that returns true if the object is of that class */
113export declare const is: <T>(ctor: new (...args: any[]) => T) => (obj: any) => obj is T;
114/** Given a value, returns a Predicate function that returns true if another value is === equal to the original value */
115export declare const eq: (comp: any) => Predicate<any>;
116/** Given a value, returns a function which returns the value */
117export declare const val: <T>(v: T) => () => T;
118export declare function invoke(fnName: string): Function;
119export declare function invoke(fnName: string, args: any[]): Function;
120/**
121 * Sorta like Pattern Matching (a functional programming conditional construct)
122 *
123 * See http://c2.com/cgi/wiki?PatternMatching
124 *
125 * This is a conditional construct which allows a series of predicates and output functions
126 * to be checked and then applied. Each predicate receives the input. If the predicate
127 * returns truthy, then its matching output function (mapping function) is provided with
128 * the input and, then the result is returned.
129 *
130 * Each combination (2-tuple) of predicate + output function should be placed in an array
131 * of size 2: [ predicate, mapFn ]
132 *
133 * These 2-tuples should be put in an outer array.
134 *
135 * @example
136 * ```
137 *
138 * // Here's a 2-tuple where the first element is the isString predicate
139 * // and the second element is a function that returns a description of the input
140 * let firstTuple = [ angular.isString, (input) => `Heres your string ${input}` ];
141 *
142 * // Second tuple: predicate "isNumber", mapfn returns a description
143 * let secondTuple = [ angular.isNumber, (input) => `(${input}) That's a number!` ];
144 *
145 * let third = [ (input) => input === null, (input) => `Oh, null...` ];
146 *
147 * let fourth = [ (input) => input === undefined, (input) => `notdefined` ];
148 *
149 * let descriptionOf = pattern([ firstTuple, secondTuple, third, fourth ]);
150 *
151 * console.log(descriptionOf(undefined)); // 'notdefined'
152 * console.log(descriptionOf(55)); // '(55) That's a number!'
153 * console.log(descriptionOf("foo")); // 'Here's your string foo'
154 * ```
155 *
156 * @param struct A 2D array. Each element of the array should be an array, a 2-tuple,
157 * with a Predicate and a mapping/output function
158 * @returns {function(any): *}
159 */
160export declare function pattern(struct: Function[][]): Function;