Skip to content

Commit cb70719

Browse files
Merge pull request #31 from Jozty/typesv2
refine types
2 parents 67063d5 + 5e608c9 commit cb70719

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+1995
-443
lines changed

add.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry2 } from "./utils/types.ts"
2+
import { PH } from "./utils/types.ts"
3+
4+
// @types
5+
type Add_2 = ((b: number) => number)
6+
& ((b?: PH) => Add_2)
7+
8+
type Add_1 = ((a: number) => number)
9+
& ((a?: PH) => Add_1)
10+
11+
type Add = ((a: number, b: number) => number)
12+
& ((a: number, b?: PH) => Add_2)
13+
& ((a: PH, b: number) => Add_1)
14+
& ((a?: PH, b?: PH) => Add)
15+
316

417
function _add(a: number, b: number) {
518
return a + b
@@ -11,4 +24,4 @@ function _add(a: number, b: number) {
1124
* const add5 = Fae.add(5, Fae._)
1225
* const a = add5(4) // 9
1326
*/
14-
export const add: Curry2<number, number, number> = curryN(2, _add)
27+
export const add: Add = curryN(2, _add)

addIndex.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { concat } from './concat.ts'
2-
import { Curry1 } from "./utils/types.ts"
2+
import { PH } from "./utils/types.ts"
33
import curryN from './utils/curry_n.ts'
44
import { Func } from './utils/types.ts'
55
import { getFunctionLength } from "./utils/get.ts"
66

7+
// @types
8+
type AddIndex = ((fn: Func) => Func)
9+
& ((fn?: PH) => AddIndex)
10+
711
function _addIndex(fn: Func) {
812
return curryN(getFunctionLength(fn), function(this: any) {
913
let index = 0
@@ -21,7 +25,6 @@ function _addIndex(fn: Func) {
2125
})
2226
}
2327

24-
2528
/**
2629
* Returns a new iteration function from the passed function
2730
* by adding two more parameters to its callback function
@@ -35,4 +38,4 @@ function _addIndex(fn: Func) {
3538
* indexedMap((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r'])
3639
* // ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
3740
*/
38-
export const addIndex: Curry1<Func, ReturnType<Parameters<typeof _addIndex>[0]>> = curryN(1, _addIndex)
41+
export const addIndex: AddIndex = curryN(1, _addIndex)

adjust.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1-
import { Func, Curry3 } from "./utils/types.ts"
1+
import { Func, PH } from "./utils/types.ts"
22
import curryN from "./utils/curry_n.ts"
33

4-
function _adjust<T>(index: number, fn: Func, list: T[]) {
4+
// @types
5+
type Adjust_1<T> = ((index: number) => T[])
6+
& ((index?: PH) => Adjust_1<T>)
7+
8+
type Adjust_2<T> = ((fn: Func) => T[])
9+
& ((fn?: PH) => Adjust_2<T>)
10+
11+
type Adjust_3 = (<T>(list: T[]) => T[])
12+
& ((list?: PH) => Adjust_3)
13+
14+
type Adjust_2_3 = (<T>(fn: Func, list: T[]) => T[])
15+
& ((fn: Func, list?: PH) => Adjust_3)
16+
& (<T>(fn: PH, list: T[]) => Adjust_2<T>)
17+
& ((fn?: PH, list?: PH) => Adjust_2_3)
18+
19+
type Adjust_1_3 = (<T>(index: number, list: T[]) => T[])
20+
& ((index: number, list?: PH) => Adjust_3)
21+
& (<T>(index: PH, list: T[]) => Adjust_1<T>)
22+
& ((index?: PH, list?: PH) => Adjust_1_3)
23+
24+
type Adjust_1_2<T> = ((index: number, fn: Func) => T[])
25+
& ((index: number, fn?: PH) => Adjust_2<T>)
26+
& ((index: PH, fn: Func) => Adjust_1<T>)
27+
& ((index?: PH, fn?: PH) => Adjust_1_2<T>)
28+
29+
type Adjust = (<T>(index: number, fn: Func, list: T[]) => T[])
30+
& ((index?: PH, fn?: PH, list?: PH) => Adjust)
31+
& ((index: number, fn?: PH, list?: PH) => Adjust_2_3)
32+
& ((index: PH, fn: Func, list?: PH) => Adjust_1_3)
33+
& (<T>(index: PH, fn: PH, list: T[]) => Adjust_1_2<T>)
34+
& ((index: number, fn: Func, list?: PH) => Adjust_3)
35+
& (<T>(index: number, fn: PH, list: T[]) => Adjust_2<T>)
36+
& (<T>(index: PH, fn: Func, list: T[]) => Adjust_1<T>)
37+
38+
39+
function _adjust<T>(index: number, fn: Func, list: T[][]) {
540
const result = [...list]
641
const len = result.length
742
if(index >= len || index < -len) return result
@@ -18,4 +53,4 @@ function _adjust<T>(index: number, fn: Func, list: T[]) {
1853
* Fae.adjust(2, Fae.add(1), [0, 1, 2, 3]) // [0, 1, 3, 3]
1954
* Fae.adjust(-3, Fae.add(1), [0, 1, 2, 3]) // [0, 2, 2, 3]
2055
*/
21-
export const adjust: Curry3<number, Func, any[], any[]> = curryN(3, _adjust)
56+
export const adjust: Adjust = curryN(3, _adjust)

all.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry2, Predicate1 } from "./utils/types.ts"
2+
import { PH, Predicate1 } from "./utils/types.ts"
33
import { dispatch } from './utils/dispatch.ts'
44
import AllTransformer from "./utils/Transformers/all.ts"
55

6+
// @types
7+
type All_2<T> = ((functor: ArrayLike<T>) => boolean)
8+
& ((functor?: PH) => All_2<T>)
9+
10+
type All_1<T> = ((predicate: Predicate1<T>) => boolean)
11+
& ((predicate?: PH) => All_1<T>)
12+
13+
type All = (<T>(predicate: Predicate1<T>, functor: ArrayLike<T>) => boolean)
14+
& (<T>(predicate: Predicate1<T>, functor?: PH) => All_2<T>)
15+
& (<T>(predicate: PH, functor: ArrayLike<T>) => All_1<T>)
16+
& ((predicate?: PH, functor?: PH) => All)
17+
618
function _all<T>(predicate: Predicate1<T> , functor: ArrayLike <T> ) {
719
let index = 0
820
while (index < functor.length) {
@@ -22,4 +34,4 @@ const dispatchedAll = dispatch(AllTransformer, _all)
2234
*
2335
* Acts as a transducer if a transformer is passed in place of `functor`
2436
*/
25-
export const all: Curry2 <Predicate1, ArrayLike <any> , boolean> = curryN(2, dispatchedAll)
37+
export const all: All = curryN(2, dispatchedAll)

allPass.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry1, Func } from "./utils/types.ts"
3-
import { max } from './max.ts'
4-
import { pluck } from './pluck.ts'
5-
import { reduce } from './reduce.ts'
2+
import { Func, Predicate, PH } from "./utils/types.ts"
3+
import { getFunctionsLengths } from "./utils/get.ts"
64

5+
// @types
6+
type AllPass = (<T>(predicates: Predicate<T>[]) => Func)
7+
& ((predicates?: PH) => AllPass)
78

8-
function _allPass(preds: Array<any>) {
9-
let len = preds.length
10-
let fn = function(this: any) {
9+
function _allPass<T = any>(predicates: Predicate<T>[]) {
10+
const len = predicates.length
11+
const fn = function(this: any, ...args: T[]) {
1112
for(let idx = 0; idx < len; idx++){
12-
if (!preds[idx].apply(this, arguments)) {
13+
if (!predicates[idx].apply(this, args)) {
1314
return false
1415
}
1516
}
1617
return true
1718
}
18-
return curryN(reduce(max, 0, pluck('length', preds)), fn)
19+
20+
const noOfParams = getFunctionsLengths(predicates)
21+
22+
return curryN(
23+
Math.max(...noOfParams, 0),
24+
fn
25+
)
1926
}
2027

2128
/**
@@ -24,4 +31,4 @@ function _allPass(preds: Array<any>) {
2431
* by those arguments.
2532
*
2633
*/
27-
export const allPass: Curry1<Array<any>, Func> = curryN(1, _allPass)
34+
export const allPass: AllPass = curryN(1, _allPass)

always.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry1 } from "./utils/types.ts"
2+
import { PH } from "./utils/types.ts"
3+
4+
// @types
5+
type Always = (<T>(value: T) => () => T)
6+
& ((value?: PH) => Always)
37

48
function _always<T>(value: T) {
59
return function() {
@@ -13,4 +17,4 @@ function _always<T>(value: T) {
1317
* const f = Fae.always('Fae')
1418
* f() // 'Fae'
1519
*/
16-
export const always: Curry1<any, () => any> = curryN(1, _always)
20+
export const always: Always = curryN(1, _always)

and.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry2 } from "./utils/types.ts"
2+
import { PH } from "./utils/types.ts"
33

4-
function _and(a: any, b: any) {
4+
// @types
5+
type And_2<T1> = (<T2>(b: T2) => T1 | T2)
6+
& ((b?: PH) => And_2<T1>)
7+
8+
type And_1<T2> = (<T1>(a: T1) => T1 | T2)
9+
& ((a?: PH) => And_1<T2>)
10+
11+
type And = (<T1, T2>(a: T1, b: T2) => T1 | T2)
12+
& (<T1>(a: T1, b?: PH) => And_2<T1>)
13+
& (<T2>(a: PH, b: T2) => And_1<T2>)
14+
& ((a?: PH, b?: PH) => And)
15+
16+
17+
function _and<T1, T2>(a: T1, b: T2): T2 | T1 {
518
return a && b
619
}
720

@@ -13,4 +26,4 @@ function _and(a: any, b: any) {
1326
* Fae.and(false, true) //=> false
1427
* Fae.and(false, false) //=> false
1528
*/
16-
export const and: Curry2<any> = curryN(2, _and)
29+
export const and: And = curryN(2, _and)

andThen.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Func, Curry2 } from "./utils/types.ts"
2+
import { Func, PH } from "./utils/types.ts"
33
import { assertPromise } from "./utils/assert.ts"
44

5+
// @types
6+
type AndThen_2 = ((p: any) => PromiseLike<any>)
7+
& ((b?: PH) => AndThen_2)
8+
9+
type AndThen_1 = ((f: Func) => PromiseLike<any>)
10+
& ((a?: PH) => AndThen_1)
11+
12+
type AndThen = ((f: Func, p: any) => PromiseLike<any>)
13+
& ((f: Func, b?: PH) => AndThen_2)
14+
& ((a: PH, p: any) => AndThen_1)
15+
& ((a?: PH, b?: PH) => AndThen)
16+
517
function _andThen(f: Func, p: any) {
618
assertPromise('andThen', p)
719
return p.then(f)
@@ -12,4 +24,4 @@ function _andThen(f: Func, p: any) {
1224
* a successfully resolved promise. This is useful for working with promises
1325
* inside function compositions.
1426
*/
15-
export const andThen: Curry2<Func, any, Promise<any>> = curryN(2, _andThen)
27+
export const andThen: AndThen = curryN(2, _andThen)

any.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
import { Predicate1, Curry2 } from "./utils/types.ts"
1+
import { Predicate1, PH } from "./utils/types.ts"
22
import { dispatch } from './utils/dispatch.ts'
33
import AnyTransformer from "./utils/Transformers/any.ts"
44
import curryN from './utils/curry_n.ts'
55

6+
// @types
7+
type Any_2<T> = ((list: T[]) => boolean)
8+
& ((list?: PH) => Any_2<T>)
9+
10+
type Any_1<T> = ((predicate: Predicate1<T>) => boolean)
11+
& ((predicate?: PH) => Any_1<T>)
12+
13+
type Any = (<T>(predicate: Predicate1<T>, list: T[]) => boolean)
14+
& (<T>(predicate: Predicate1<T>, list?: PH) => Any_2<T>)
15+
& (<T>(predicate: PH, list: T[]) => Any_1<T>)
16+
& ((predicate?: PH, list?: PH) => Any)
17+
618
function _any<T>(predicate: Predicate1<T>, list: T[]) {
719
for(let i = 0; i < list.length; i++) {
820
if(predicate(list[i])) return true
@@ -13,9 +25,9 @@ function _any<T>(predicate: Predicate1<T>, list: T[]) {
1325
const dispatched = dispatch(AnyTransformer, _any)
1426

1527
/**
16-
* Return `true` if any the elements of the functor match `predicate`
28+
* Return `true` if any the elements of the list match `predicate`
1729
* `false` otherwise
1830
*
19-
* Acts as a transducer if a transformer is passed in place of `functor`
31+
* Acts as a transducer if a transformer is passed in place of `list`
2032
*/
21-
export const any: Curry2<Predicate1, any[]> = curryN(2, dispatched)
33+
export const any: Any = curryN(2, dispatched)

anyPass.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry1, Func } from "./utils/types.ts"
3-
import { max } from './max.ts'
4-
import { pluck } from './pluck.ts'
5-
import { reduce } from './reduce.ts'
2+
import { PH, Func, Predicate } from "./utils/types.ts"
3+
import { getFunctionsLengths } from "./utils/get.ts"
64

7-
function _anyPass(preds: any) {
8-
let len = preds.length
9-
let fn = function(this: any) {
5+
// @types
6+
type AnyPass = (<T>(predicates: Predicate<T>[]) => Func)
7+
& ((predicates?: PH) => AnyPass)
8+
9+
function _anyPass<T>(predicates: Predicate<T>[]) {
10+
const len = predicates.length
11+
const fn = function(this: any, ...args: T[]) {
1012
for(let idx = 0; idx < len; idx++){
11-
if (preds[idx].apply(this, arguments)) {
13+
if (predicates[idx].apply(this, args)) {
1214
return true
1315
}
1416
}
1517
return false
1618
}
17-
return curryN(reduce(max, 0, pluck('length', preds)), fn)
19+
20+
const noOfParams = getFunctionsLengths(predicates)
21+
22+
return curryN(
23+
Math.max(...noOfParams, 0),
24+
fn
25+
)
1826
}
1927

2028
/**
2129
* Takes a list of predicates and returns a predicate that returns true for a
2230
* given list of arguments if at least one of the provided predicates is
2331
* satisfied by those arguments.
2432
*/
25-
export const anyPass: Curry1<any, Func> = curryN(1, _anyPass)
33+
export const anyPass: AnyPass = curryN(1, _anyPass)

ap.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ function _ap<T, R>(
3030
}
3131

3232
return reduce(
33-
(acc: T[], f: Func) => concat(acc, map(f, applyX)),
33+
// @ts-ignore
34+
(acc: T[], f: Func) => concat(acc, map(f, applyX) as T[]),
3435
[],
3536
applyF
3637
)

aperture.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import { dispatch } from "./utils/dispatch.ts"
22
import ApertureTransformer from "./utils/Transformers/aperture.ts"
33
import curryN from "./utils/curry_n.ts"
4-
import { Curry2 } from "./utils/types.ts"
4+
import { PH } from "./utils/types.ts"
5+
6+
// @types
7+
type Aperture_2 = (<T>(list: T[]) => T[][])
8+
& ((list?: PH) => Aperture_2)
9+
10+
type Aperture_1<T> = ((n: number) => T[][])
11+
& ((n?: PH) => Aperture_1<T>)
12+
13+
type Aperture = (<T>(n: number, list: T[]) => T[][])
14+
& ((n: number, list?: PH) => Aperture_2)
15+
& (<T>(n: PH, list: T[]) => Aperture_1<T>)
16+
& ((n?: PH, list?: PH) => Aperture)
517

618
function _aperture<T>(n: number, list: T[]) {
719
const len = list.length - n + 1
@@ -22,4 +34,4 @@ const dispatched = dispatch(ApertureTransformer as any, _aperture)
2234
*
2335
* Acts as a transducer if a transformer is passed in place of `list`
2436
*/
25-
export const aperture: Curry2<number, any[]> = curryN(2, dispatched)
37+
export const aperture: Aperture = curryN(2, dispatched)

append.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import curryN from "./utils/curry_n.ts"
2-
import { Curry2 } from "./utils/types.ts"
2+
import { PH } from "./utils/types.ts"
3+
4+
// @types
5+
type Append_2<T> = ((list: T[]) => T[])
6+
& ((list?: PH) => Append_2<T>)
7+
8+
type Append_1<T> = ((el: T) => T[])
9+
& ((el?: PH) => Append_1<T>)
10+
11+
type Append = (<T>(el: T, list: T[]) => T[])
12+
& (<T>(el: T, list?: PH) => Append_2<T>)
13+
& (<T>(el: PH, list: T[]) => Append_1<T>)
14+
& ((el?: PH, list?: PH) => Append)
315

416
function _append<T>(el: T, list: T[]) {
517
return [...list, el]
@@ -13,4 +25,4 @@ function _append<T>(el: T, list: T[]) {
1325
* Fae.append('tests', []); //=> ['tests']
1426
* Fae.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
1527
*/
16-
export const append: Curry2<any, any[], any[]> = curryN(2, _append)
28+
export const append: Append = curryN(2, _append)

0 commit comments

Comments
 (0)