-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.ts
262 lines (243 loc) · 8.99 KB
/
flow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/** @module flow.ts */
import { complement } from './function/complement'
import { curry } from './function/curry'
import { untypedCurry } from './function/untypedCurry'
import { Function1, Function2, FunctionV, Predicate } from './helper-types'
import { trace } from './logging'
import { Maybe } from './maybe'
export function when<A, B>( pred: Function1<A, boolean>, whenTrueFn: Function1<A, B>, input: A ): A | B
export function when<A, B>( pred: Function1<A, boolean>, whenTrueFn: Function1<A, B> ): ( input: A ) => A | B
export function when<A>( pred: Function1<A, boolean> ): <A2 extends A, B>( whenTrueFn: Function1<A2, B> ) => ( input: A2 ) => A2|B
/**
* Takes a predicate, a function, and a value. If pred(a) is true then will run function over value otherwise returns value.
* :: pred -> fn -> x -> x | fn(x)
*/
export function when( ...args ) {
return untypedCurry( ( pred, whenTrueFn, x ) => pred( x ) === true ? whenTrueFn( x ) : x )( ...args )
}
export function times <A>( callback: Function1<number, A>, repeat: number ): A[]
export function times <A>( callback: Function1<number, A> ): ( repeat: number ) => A[]
/**
* Takes a callback and a repeat number n, iterates over that function n times.
* :: ( fn -> n -> a ) -> n -> a[]
*/
export function times( ...args ) {
return untypedCurry( ( callback, numberOfTimes ) => {
const returnValues: any[] = []
for ( let index = 0; index < numberOfTimes; index++ ) {
returnValues.push( callback( index ) )
}
return returnValues
} )( ...args )
}
export function times_<A>( repeat: number, callback: Function1<number, A> ): A[]
export function times_( repeat: number ): <A>( callback: Function1<number, A> ) => A[ ]
/**
* Takes a callback and a repeat number n, iterates over that function n times.
* :: ( fn -> n -> a ) -> n -> a[]
*/
export function times_( ...args ) {
return untypedCurry( ( numberOfTimes, callback ) => {
const returnValues: any[] = []
for ( let index = 0; index < numberOfTimes; index++ ) {
returnValues.push( callback( index ) )
}
return returnValues
} )( ...args )
}
export function timesWithRepeat <A>( callback: Function2<number, number, A>, repeat: number ): A[]
export function timesWithRepeat<A>( callback: Function2<number, number, A> ): ( repeat: number ) => A[]
/**
* Takes a callback and a repeat number n, iterates over that function n times.
* :: ( fn -> n -> nt -> a ) -> nt -> a[]
*/
export function timesWithRepeat( ...args ) {
return untypedCurry( ( callback, numberOfTimes ) => {
const returnValues: any[] = []
for ( let index = 0; index < numberOfTimes; index++ ) {
returnValues.push( callback( index, numberOfTimes ) )
}
return returnValues
} )( ...args )
}
export function timesWithRepeat_<A>( repeat: number, callback: Function2<number, number, A> ): A[]
export function timesWithRepeat_( repeat: number ): <A>( callback: Function2<number, number, A> ) => A[]
/**
* Takes a callback and a repeat number n, iterates over that function n times.
* :: ( fn -> n -> nt -> a ) -> nt -> a[]
*/
export function timesWithRepeat_( ...args ) {
return untypedCurry( ( numberOfTimes, callback ) => {
const returnValues: any[] = []
for ( let index = 0; index < numberOfTimes; index++ ) {
returnValues.push( callback( index, numberOfTimes ) )
}
return returnValues
} )( ...args )
}
/**
* Alias to for `times`
* :: ( fn -> a ) -> n -> a[]
*/
export const loop = times
export function tryCatch <A>( failCallback: Function1<Error, A>, testCallback: FunctionV<( null|undefined ), A> ): A
export function tryCatch <A>( failCallback: Function1<Error, A> ): ( testCallback: FunctionV<( null|undefined ), A> ) => A
/**
* Takes failure callback f and test function t, runs t or f if t failes.
* :: ( f -> e -> a ) -> ( f -> a) -> a
*/
export function tryCatch( ...args ) {
return untypedCurry( ( failCallback, testCallback ) => {
try {
return testCallback()
} catch ( e ) {
return failCallback( e )
}
} )
}
export function tryCatchWithParams <A, B, C>( failCallback: Function1<Error, A>, testCallback: FunctionV<B, A> ): ( ...args: B[] ) => A
export function tryCatchWithParams <A, B>( failCallback: Function1<Error, A> ): ( testCallback: FunctionV<B, A> ) => ( ...args: B[] ) => A
/**
* Like tryCatch only test function receives arguments in a seperate callback
* :: ( f -> e -> a ) -> ( f -> a) -> a
*/
export function tryCatchWithParams( failCallback, testCallback? ) {
const doIt = ( _testCallback ) => ( ...args ) => {
try {
return _testCallback( ...args )
} catch ( e ) {
return failCallback( e )
}
}
if ( !testCallback ) {
return doIt
} else {
return doIt( testCallback )
}
}
/**
* Takes a value v, a glob of predicates, and a test a parameter "a", returns true if any predicate given "a" matches "v"
* :: v -> ...((a) -> bool) -> a -> bool
*/
export const logical = ( value: boolean ) => <A>( ...fns: Array<Predicate<A>> ) => ( x: A ) => {
for ( const fn of fns ) {
if ( fn( x ) === value ) {
return true
}
}
return false
}
/**
* Takes a glob of predicates "p" taking a value "a", then a value "a" and returns true if any of "p" given "a" is true
* :: v -> ...((a) -> bool) -> a -> bool
*/
export const either = logical( true )
/**
* Takes a glob of predicates "p" taking a value "a", then a value "a" and returns true if all of "p" given "a" is true
* :: v -> ...((a) -> bool) -> a -> bool
*/
export const and = <A>( ...fns: Array<Predicate<A>> ) => complement( logical( true )<A>( ...fns ) )
/* 900 */
/**
* ifElse
*/
const _ifElse = untypedCurry(
( predicate, trueCondition, falseCondition, input ) => predicate( input ) ? trueCondition( input ) : falseCondition( input ),
)
export function ifElse<A, B>( p: Function1<A, boolean>, t: Function1<A, B>, f: Function1<A, B>, i: A ): B
export function ifElse<A, B>( p: Function1<A, boolean>, t: Function1<A, B>, f: Function1<A, B> ): ( i: A ) => B
export function ifElse<A>( p: Function1<A, boolean> ): <A2 extends A, B>( t: Function1<A2, B> ) => <A3 extends A2, B2 extends B>( f: Function1<A3, B2> ) => ( i: A3 ) => B2
/**
* Functional If/Else branching
* Given predicate(input), execute trueCondition(input) or falseCondition(input).
*
* @example
*
* // remove lines starting with comments
* ifElse( test( /^\#/ ) , _ => '' , identity )
*
* :: predicate ( input A -> boolean ) ->
* trueCondition ( input A -> output1 B) ->
* falseCondition ( input A -> output2 B)->
* input ->
* output B
*/
export function ifElse( ...args ) {
return _ifElse( ...args )
}
export function gt( a: number ): ( b: number ) => boolean
export function gt( a: number, b: number ): boolean
/**
* Predicate, right associative greater-than. Check that the second parameter is greater-than the first
* @param a number which `b` must be greater-than
* @param b number which is checked to be greater-than `a`
* @example
* pipeline(
* 10,
* gt(11)
* ) // false
*/
export function gt( ...args ) {
return untypedCurry( ( a, b ) => b > a )( ...args )
}
export function gte( a: number ): ( b: number ) => boolean
export function gte( a: number, b: number ): boolean
/**
* Predicate, right associative greater-than or equal to. Check that the second parameter is greater-than or equal to the first
* @param a number which `b` must be greater than
* @param b number which is checked to be greater than `a`
* @example
* pipeline(
* 10,
* gt(11)
* ) // false
*/
export function gte( ...args ) {
return untypedCurry( ( a, b ) => b >= a )( ...args )
}
export function lt( a: number ): ( b: number ) => boolean
export function lt( a: number, b: number ): boolean
/**
* Predicate, right associative less-than. Check that the second parameter is less-than the first
* @param a number which `b` must be less-than
* @param b number which is checked to be less-than `a`
* @example
* pipeline(
* 10,
* gt(11)
* ) // false
*/
export function lt( ...args ) {
return untypedCurry( ( a, b ) => b < a )( ...args )
}
export function lte( a: number ): ( b: number ) => boolean
export function lte( a: number, b: number ): boolean
/**
* Predicate, right associative less-than or equal to. Check that the second parameter is less-than or equal to the first
* @param a number which `b` must be less-than
* @param b number which is checked to be less-than `a`
* @example
* pipeline(
* 10,
* gt(11)
* ) // false
*/
export function lte( ...args ) {
return untypedCurry( ( a, b ) => b <= a )( ...args )
}
export function eq<T>( a: T ): ( b: T ) => boolean
export function eq<T>( a: T, b: T ): boolean
/**
* Predicate checks that both given values `a` and `b` are the same. Strictly typed
*/
export function eq( ...args ) {
return untypedCurry( ( a, b ) => b === a )( ...args )
}
export function untypedEq( a: any ): ( b: any ) => boolean
export function untypedEq( a: any, b: any ): boolean
/**
* Predicate checks that both given values `a` and `b` are the same. Untyped.
*/
export function untypedEq( ...args ) {
return untypedCurry( ( a, b ) => b === a )( ...args )
}