Skip to content

Commit

Permalink
Merge pull request #31 from Jozty/typesv2
Browse files Browse the repository at this point in the history
refine types
  • Loading branch information
singla-shivam authored Jun 30, 2020
2 parents 67063d5 + 5e608c9 commit cb70719
Show file tree
Hide file tree
Showing 139 changed files with 1,995 additions and 443 deletions.
17 changes: 15 additions & 2 deletions add.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import curryN from "./utils/curry_n.ts"
import { Curry2 } from "./utils/types.ts"
import { PH } from "./utils/types.ts"

// @types
type Add_2 = ((b: number) => number)
& ((b?: PH) => Add_2)

type Add_1 = ((a: number) => number)
& ((a?: PH) => Add_1)

type Add = ((a: number, b: number) => number)
& ((a: number, b?: PH) => Add_2)
& ((a: PH, b: number) => Add_1)
& ((a?: PH, b?: PH) => Add)


function _add(a: number, b: number) {
return a + b
Expand All @@ -11,4 +24,4 @@ function _add(a: number, b: number) {
* const add5 = Fae.add(5, Fae._)
* const a = add5(4) // 9
*/
export const add: Curry2<number, number, number> = curryN(2, _add)
export const add: Add = curryN(2, _add)
9 changes: 6 additions & 3 deletions addIndex.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { concat } from './concat.ts'
import { Curry1 } from "./utils/types.ts"
import { PH } from "./utils/types.ts"
import curryN from './utils/curry_n.ts'
import { Func } from './utils/types.ts'
import { getFunctionLength } from "./utils/get.ts"

// @types
type AddIndex = ((fn: Func) => Func)
& ((fn?: PH) => AddIndex)

function _addIndex(fn: Func) {
return curryN(getFunctionLength(fn), function(this: any) {
let index = 0
Expand All @@ -21,7 +25,6 @@ function _addIndex(fn: Func) {
})
}


/**
* Returns a new iteration function from the passed function
* by adding two more parameters to its callback function
Expand All @@ -35,4 +38,4 @@ function _addIndex(fn: Func) {
* indexedMap((val, idx) => idx + '-' + val, ['f', 'o', 'o', 'b', 'a', 'r'])
* // ['0-f', '1-o', '2-o', '3-b', '4-a', '5-r']
*/
export const addIndex: Curry1<Func, ReturnType<Parameters<typeof _addIndex>[0]>> = curryN(1, _addIndex)
export const addIndex: AddIndex = curryN(1, _addIndex)
41 changes: 38 additions & 3 deletions adjust.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import { Func, Curry3 } from "./utils/types.ts"
import { Func, PH } from "./utils/types.ts"
import curryN from "./utils/curry_n.ts"

function _adjust<T>(index: number, fn: Func, list: T[]) {
// @types
type Adjust_1<T> = ((index: number) => T[])
& ((index?: PH) => Adjust_1<T>)

type Adjust_2<T> = ((fn: Func) => T[])
& ((fn?: PH) => Adjust_2<T>)

type Adjust_3 = (<T>(list: T[]) => T[])
& ((list?: PH) => Adjust_3)

type Adjust_2_3 = (<T>(fn: Func, list: T[]) => T[])
& ((fn: Func, list?: PH) => Adjust_3)
& (<T>(fn: PH, list: T[]) => Adjust_2<T>)
& ((fn?: PH, list?: PH) => Adjust_2_3)

type Adjust_1_3 = (<T>(index: number, list: T[]) => T[])
& ((index: number, list?: PH) => Adjust_3)
& (<T>(index: PH, list: T[]) => Adjust_1<T>)
& ((index?: PH, list?: PH) => Adjust_1_3)

type Adjust_1_2<T> = ((index: number, fn: Func) => T[])
& ((index: number, fn?: PH) => Adjust_2<T>)
& ((index: PH, fn: Func) => Adjust_1<T>)
& ((index?: PH, fn?: PH) => Adjust_1_2<T>)

type Adjust = (<T>(index: number, fn: Func, list: T[]) => T[])
& ((index?: PH, fn?: PH, list?: PH) => Adjust)
& ((index: number, fn?: PH, list?: PH) => Adjust_2_3)
& ((index: PH, fn: Func, list?: PH) => Adjust_1_3)
& (<T>(index: PH, fn: PH, list: T[]) => Adjust_1_2<T>)
& ((index: number, fn: Func, list?: PH) => Adjust_3)
& (<T>(index: number, fn: PH, list: T[]) => Adjust_2<T>)
& (<T>(index: PH, fn: Func, list: T[]) => Adjust_1<T>)


function _adjust<T>(index: number, fn: Func, list: T[][]) {
const result = [...list]
const len = result.length
if(index >= len || index < -len) return result
Expand All @@ -18,4 +53,4 @@ function _adjust<T>(index: number, fn: Func, list: T[]) {
* Fae.adjust(2, Fae.add(1), [0, 1, 2, 3]) // [0, 1, 3, 3]
* Fae.adjust(-3, Fae.add(1), [0, 1, 2, 3]) // [0, 2, 2, 3]
*/
export const adjust: Curry3<number, Func, any[], any[]> = curryN(3, _adjust)
export const adjust: Adjust = curryN(3, _adjust)
16 changes: 14 additions & 2 deletions all.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import curryN from "./utils/curry_n.ts"
import { Curry2, Predicate1 } from "./utils/types.ts"
import { PH, Predicate1 } from "./utils/types.ts"
import { dispatch } from './utils/dispatch.ts'
import AllTransformer from "./utils/Transformers/all.ts"

// @types
type All_2<T> = ((functor: ArrayLike<T>) => boolean)
& ((functor?: PH) => All_2<T>)

type All_1<T> = ((predicate: Predicate1<T>) => boolean)
& ((predicate?: PH) => All_1<T>)

type All = (<T>(predicate: Predicate1<T>, functor: ArrayLike<T>) => boolean)
& (<T>(predicate: Predicate1<T>, functor?: PH) => All_2<T>)
& (<T>(predicate: PH, functor: ArrayLike<T>) => All_1<T>)
& ((predicate?: PH, functor?: PH) => All)

function _all<T>(predicate: Predicate1<T> , functor: ArrayLike <T> ) {
let index = 0
while (index < functor.length) {
Expand All @@ -22,4 +34,4 @@ const dispatchedAll = dispatch(AllTransformer, _all)
*
* Acts as a transducer if a transformer is passed in place of `functor`
*/
export const all: Curry2 <Predicate1, ArrayLike <any> , boolean> = curryN(2, dispatchedAll)
export const all: All = curryN(2, dispatchedAll)
27 changes: 17 additions & 10 deletions allPass.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import curryN from "./utils/curry_n.ts"
import { Curry1, Func } from "./utils/types.ts"
import { max } from './max.ts'
import { pluck } from './pluck.ts'
import { reduce } from './reduce.ts'
import { Func, Predicate, PH } from "./utils/types.ts"
import { getFunctionsLengths } from "./utils/get.ts"

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

function _allPass(preds: Array<any>) {
let len = preds.length
let fn = function(this: any) {
function _allPass<T = any>(predicates: Predicate<T>[]) {
const len = predicates.length
const fn = function(this: any, ...args: T[]) {
for(let idx = 0; idx < len; idx++){
if (!preds[idx].apply(this, arguments)) {
if (!predicates[idx].apply(this, args)) {
return false
}
}
return true
}
return curryN(reduce(max, 0, pluck('length', preds)), fn)

const noOfParams = getFunctionsLengths(predicates)

return curryN(
Math.max(...noOfParams, 0),
fn
)
}

/**
Expand All @@ -24,4 +31,4 @@ function _allPass(preds: Array<any>) {
* by those arguments.
*
*/
export const allPass: Curry1<Array<any>, Func> = curryN(1, _allPass)
export const allPass: AllPass = curryN(1, _allPass)
8 changes: 6 additions & 2 deletions always.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import curryN from "./utils/curry_n.ts"
import { Curry1 } from "./utils/types.ts"
import { PH } from "./utils/types.ts"

// @types
type Always = (<T>(value: T) => () => T)
& ((value?: PH) => Always)

function _always<T>(value: T) {
return function() {
Expand All @@ -13,4 +17,4 @@ function _always<T>(value: T) {
* const f = Fae.always('Fae')
* f() // 'Fae'
*/
export const always: Curry1<any, () => any> = curryN(1, _always)
export const always: Always = curryN(1, _always)
19 changes: 16 additions & 3 deletions and.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import curryN from "./utils/curry_n.ts"
import { Curry2 } from "./utils/types.ts"
import { PH } from "./utils/types.ts"

function _and(a: any, b: any) {
// @types
type And_2<T1> = (<T2>(b: T2) => T1 | T2)
& ((b?: PH) => And_2<T1>)

type And_1<T2> = (<T1>(a: T1) => T1 | T2)
& ((a?: PH) => And_1<T2>)

type And = (<T1, T2>(a: T1, b: T2) => T1 | T2)
& (<T1>(a: T1, b?: PH) => And_2<T1>)
& (<T2>(a: PH, b: T2) => And_1<T2>)
& ((a?: PH, b?: PH) => And)


function _and<T1, T2>(a: T1, b: T2): T2 | T1 {
return a && b
}

Expand All @@ -13,4 +26,4 @@ function _and(a: any, b: any) {
* Fae.and(false, true) //=> false
* Fae.and(false, false) //=> false
*/
export const and: Curry2<any> = curryN(2, _and)
export const and: And = curryN(2, _and)
16 changes: 14 additions & 2 deletions andThen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import curryN from "./utils/curry_n.ts"
import { Func, Curry2 } from "./utils/types.ts"
import { Func, PH } from "./utils/types.ts"
import { assertPromise } from "./utils/assert.ts"

// @types
type AndThen_2 = ((p: any) => PromiseLike<any>)
& ((b?: PH) => AndThen_2)

type AndThen_1 = ((f: Func) => PromiseLike<any>)
& ((a?: PH) => AndThen_1)

type AndThen = ((f: Func, p: any) => PromiseLike<any>)
& ((f: Func, b?: PH) => AndThen_2)
& ((a: PH, p: any) => AndThen_1)
& ((a?: PH, b?: PH) => AndThen)

function _andThen(f: Func, p: any) {
assertPromise('andThen', p)
return p.then(f)
Expand All @@ -12,4 +24,4 @@ function _andThen(f: Func, p: any) {
* a successfully resolved promise. This is useful for working with promises
* inside function compositions.
*/
export const andThen: Curry2<Func, any, Promise<any>> = curryN(2, _andThen)
export const andThen: AndThen = curryN(2, _andThen)
20 changes: 16 additions & 4 deletions any.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { Predicate1, Curry2 } from "./utils/types.ts"
import { Predicate1, PH } from "./utils/types.ts"
import { dispatch } from './utils/dispatch.ts'
import AnyTransformer from "./utils/Transformers/any.ts"
import curryN from './utils/curry_n.ts'

// @types
type Any_2<T> = ((list: T[]) => boolean)
& ((list?: PH) => Any_2<T>)

type Any_1<T> = ((predicate: Predicate1<T>) => boolean)
& ((predicate?: PH) => Any_1<T>)

type Any = (<T>(predicate: Predicate1<T>, list: T[]) => boolean)
& (<T>(predicate: Predicate1<T>, list?: PH) => Any_2<T>)
& (<T>(predicate: PH, list: T[]) => Any_1<T>)
& ((predicate?: PH, list?: PH) => Any)

function _any<T>(predicate: Predicate1<T>, list: T[]) {
for(let i = 0; i < list.length; i++) {
if(predicate(list[i])) return true
Expand All @@ -13,9 +25,9 @@ function _any<T>(predicate: Predicate1<T>, list: T[]) {
const dispatched = dispatch(AnyTransformer, _any)

/**
* Return `true` if any the elements of the functor match `predicate`
* Return `true` if any the elements of the list match `predicate`
* `false` otherwise
*
* Acts as a transducer if a transformer is passed in place of `functor`
* Acts as a transducer if a transformer is passed in place of `list`
*/
export const any: Curry2<Predicate1, any[]> = curryN(2, dispatched)
export const any: Any = curryN(2, dispatched)
28 changes: 18 additions & 10 deletions anyPass.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import curryN from "./utils/curry_n.ts"
import { Curry1, Func } from "./utils/types.ts"
import { max } from './max.ts'
import { pluck } from './pluck.ts'
import { reduce } from './reduce.ts'
import { PH, Func, Predicate } from "./utils/types.ts"
import { getFunctionsLengths } from "./utils/get.ts"

function _anyPass(preds: any) {
let len = preds.length
let fn = function(this: any) {
// @types
type AnyPass = (<T>(predicates: Predicate<T>[]) => Func)
& ((predicates?: PH) => AnyPass)

function _anyPass<T>(predicates: Predicate<T>[]) {
const len = predicates.length
const fn = function(this: any, ...args: T[]) {
for(let idx = 0; idx < len; idx++){
if (preds[idx].apply(this, arguments)) {
if (predicates[idx].apply(this, args)) {
return true
}
}
return false
}
return curryN(reduce(max, 0, pluck('length', preds)), fn)

const noOfParams = getFunctionsLengths(predicates)

return curryN(
Math.max(...noOfParams, 0),
fn
)
}

/**
* Takes a list of predicates and returns a predicate that returns true for a
* given list of arguments if at least one of the provided predicates is
* satisfied by those arguments.
*/
export const anyPass: Curry1<any, Func> = curryN(1, _anyPass)
export const anyPass: AnyPass = curryN(1, _anyPass)
3 changes: 2 additions & 1 deletion ap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function _ap<T, R>(
}

return reduce(
(acc: T[], f: Func) => concat(acc, map(f, applyX)),
// @ts-ignore
(acc: T[], f: Func) => concat(acc, map(f, applyX) as T[]),
[],
applyF
)
Expand Down
16 changes: 14 additions & 2 deletions aperture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { dispatch } from "./utils/dispatch.ts"
import ApertureTransformer from "./utils/Transformers/aperture.ts"
import curryN from "./utils/curry_n.ts"
import { Curry2 } from "./utils/types.ts"
import { PH } from "./utils/types.ts"

// @types
type Aperture_2 = (<T>(list: T[]) => T[][])
& ((list?: PH) => Aperture_2)

type Aperture_1<T> = ((n: number) => T[][])
& ((n?: PH) => Aperture_1<T>)

type Aperture = (<T>(n: number, list: T[]) => T[][])
& ((n: number, list?: PH) => Aperture_2)
& (<T>(n: PH, list: T[]) => Aperture_1<T>)
& ((n?: PH, list?: PH) => Aperture)

function _aperture<T>(n: number, list: T[]) {
const len = list.length - n + 1
Expand All @@ -22,4 +34,4 @@ const dispatched = dispatch(ApertureTransformer as any, _aperture)
*
* Acts as a transducer if a transformer is passed in place of `list`
*/
export const aperture: Curry2<number, any[]> = curryN(2, dispatched)
export const aperture: Aperture = curryN(2, dispatched)
16 changes: 14 additions & 2 deletions append.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import curryN from "./utils/curry_n.ts"
import { Curry2 } from "./utils/types.ts"
import { PH } from "./utils/types.ts"

// @types
type Append_2<T> = ((list: T[]) => T[])
& ((list?: PH) => Append_2<T>)

type Append_1<T> = ((el: T) => T[])
& ((el?: PH) => Append_1<T>)

type Append = (<T>(el: T, list: T[]) => T[])
& (<T>(el: T, list?: PH) => Append_2<T>)
& (<T>(el: PH, list: T[]) => Append_1<T>)
& ((el?: PH, list?: PH) => Append)

function _append<T>(el: T, list: T[]) {
return [...list, el]
Expand All @@ -13,4 +25,4 @@ function _append<T>(el: T, list: T[]) {
* Fae.append('tests', []); //=> ['tests']
* Fae.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
*/
export const append: Curry2<any, any[], any[]> = curryN(2, _append)
export const append: Append = curryN(2, _append)
Loading

0 comments on commit cb70719

Please sign in to comment.