Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite compose utility function #3568

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/applyMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function applyMiddleware(
dispatch: (action, ...args) => dispatch(action, ...args)
}
const chain = middlewares.map(middleware => middleware(middlewareAPI))
dispatch = compose<typeof dispatch>(...chain)(store.dispatch)
dispatch = compose(...chain)(store.dispatch as typeof dispatch)

return {
...store,
Expand Down
208 changes: 123 additions & 85 deletions src/compose.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
type Func0<R> = () => R
type Func1<T1, R> = (a1: T1) => R
type Func2<T1, T2, R> = (a1: T1, a2: T2) => R
type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R
/**
* A generic representation of a function exposing more type information than
* the built-in `Function` type, allowing extraction of its input param types
*/
interface Fn extends Function {
<A extends unknown[]>(...args: A): unknown
<A>(...args: A[]): unknown
}

/**
* A type-level utility to get the type of the tail of a tuple of functions
*/
type Tail<Fns extends Fn[]> = (
...fns: Fns
) => unknown extends <A>(a: A, ...rest: infer Rest) => unknown ? Rest : never

/**
* A type-level utility to get the type of the last function from a tuple of
* functions
*/
type Last<Fns extends Fn[]> = Fns['length'] extends 0
? never
: Fns[Tail<Fns>['length']]

/**
* A type-level utility to get the return type of the composition of a tuple of
* functions
*/
type Composed<Fns extends Fn[]> = (
...args: Parameters<Last<Fns>>
) => ReturnType<Fns[0]>

/**
* Composes single-argument functions from right to left. The rightmost
Expand All @@ -13,90 +40,101 @@ type Func3<T1, T2, T3, R> = (a1: T1, a2: T2, a3: T3, ...args: any[]) => R
* to left. For example, `compose(f, g, h)` is identical to doing
* `(...args) => f(g(h(...args)))`.
*/
export default function compose(): <R>(a: R) => R

export default function compose<F extends Function>(f: F): F

/* two functions */
export default function compose<A, R>(f1: (b: A) => R, f2: Func0<A>): Func0<R>
export default function compose<A, T1, R>(
f1: (b: A) => R,
f2: Func1<T1, A>
): Func1<T1, R>
export default function compose<A, T1, T2, R>(
f1: (b: A) => R,
f2: Func2<T1, T2, A>
): Func2<T1, T2, R>
export default function compose<A, T1, T2, T3, R>(
f1: (b: A) => R,
f2: Func3<T1, T2, T3, A>
): Func3<T1, T2, T3, R>

/* three functions */
export default function compose<A, B, R>(
f1: (b: B) => R,
f2: (a: A) => B,
f3: Func0<A>
): Func0<R>
export default function compose<A, B, T1, R>(
f1: (b: B) => R,
f2: (a: A) => B,
f3: Func1<T1, A>
): Func1<T1, R>
export default function compose<A, B, T1, T2, R>(
f1: (b: B) => R,
f2: (a: A) => B,
f3: Func2<T1, T2, A>
): Func2<T1, T2, R>
export default function compose<A, B, T1, T2, T3, R>(
f1: (b: B) => R,
f2: (a: A) => B,
f3: Func3<T1, T2, T3, A>
): Func3<T1, T2, T3, R>

/* four functions */
export default function compose<A, B, C, R>(
f1: (b: C) => R,
f2: (a: B) => C,
f3: (a: A) => B,
f4: Func0<A>
): Func0<R>
export default function compose<A, B, C, T1, R>(
f1: (b: C) => R,
f2: (a: B) => C,
f3: (a: A) => B,
f4: Func1<T1, A>
): Func1<T1, R>
export default function compose<A, B, C, T1, T2, R>(
f1: (b: C) => R,
f2: (a: B) => C,
f3: (a: A) => B,
f4: Func2<T1, T2, A>
): Func2<T1, T2, R>
export default function compose<A, B, C, T1, T2, T3, R>(
f1: (b: C) => R,
f2: (a: B) => C,
f3: (a: A) => B,
f4: Func3<T1, T2, T3, A>
): Func3<T1, T2, T3, R>

/* rest */
export default function compose<R>(
f1: (b: any) => R,
...funcs: Function[]
): (...args: any[]) => R

export default function compose<R>(...funcs: Function[]): (...args: any[]) => R
// when given no args and no generic type params, infer args as tuple
export default function compose(): <A extends unknown[]>(...a: A) => A[0]
// allow specifying type param of args tuple
export default function compose<A extends unknown[]>(): (...a: A) => A[0]
// when given a single function, just return that function
export default function compose<A extends unknown[], B>(
fab: (...args: A) => B
): (...args: A) => B
// standard case, given 2 functions
export default function compose<A extends unknown[], B, C>(
fbc: (b: B) => C,
fab: (...args: A) => B
): (...args: A) => C
// standard case, given 3 functions
export default function compose<A extends unknown[], B, C, D>(
fcd: (c: C) => D,
fbc: (b: B) => C,
fab: (...args: A) => B
): (...args: A) => D
// standard case, given 4 functions
export default function compose<A extends unknown[], B, C, D, E>(
fde: (d: D) => E,
fcd: (c: C) => D,
fbc: (b: B) => C,
fab: (...args: A) => B
): (...args: A) => E
// standard case, given 5 functions
export default function compose<A extends unknown[], B, C, D, E, F>(
fef: (e: E) => F,
fde: (d: D) => E,
fcd: (c: C) => D,
fbc: (b: B) => C,
fab: (...args: A) => B
): (...args: A) => F
// extra overloads allowing functions other than the right-most function
// to take in more than one argument, though the extra arguments go unused
// 2 multi-arg functions
export default function compose<A extends unknown[], B extends unknown[], C>(
fbc: (...b: B) => C,
fab: (...args: A) => B[0]
): (...args: A) => C
// 3 multi-arg functions
export default function compose<
A extends unknown[],
B extends unknown[],
C extends unknown[],
D
>(
fcd: (...c: C) => D,
fbc: (...b: B) => C[0],
fab: (...args: A) => B[0]
): (...args: A) => D
// 4 multi-arg functions
export default function compose<
A extends unknown[],
B extends unknown[],
C extends unknown[],
D extends unknown[],
E
>(
fde: (...d: D) => E,
fcd: (...c: C) => D[0],
fbc: (...b: B) => C[0],
fab: (...args: A) => B[0]
): (...args: A) => E
// 5 multi-arg functions
export default function compose<
A extends unknown[],
B extends unknown[],
C extends unknown[],
D extends unknown[],
E extends unknown[],
F
>(
fef: (...e: E) => F,
fde: (...d: D) => E[0],
fcd: (...c: C) => D[0],
fbc: (...b: B) => C[0],
fab: (...args: A) => B[0]
): (...args: A) => F
// generic type signature for any number of functions
export default function compose<Fns extends Fn[]>(...funcs: Fns): Composed<Fns>
// generic base case type signature and function body implementation
export default function compose<Fns extends Fn[]>(...fns: Fns): Fn {
const len = fns.length

export default function compose(...funcs: Function[]) {
if (funcs.length === 0) {
// infer the argument type so it is usable in inference down the line
return <T>(arg: T) => arg
if (len === 0) {
return <A extends unknown[]>(...args: A): A[0] => args[0]
}

if (funcs.length === 1) {
return funcs[0]
if (len === 1) {
return fns[0]
}

return funcs.reduce((a, b) => (...args: any) => a(b(...args)))
// `Parameters<typeof b>` is equalavelnt to `unknown[]` in the below type,
// signature, but `Parameters<typeof b>` more clearly conveys intent
return fns.reduce((a, b) => (...args: Parameters<typeof b>) => a(b(...args)))
}
3 changes: 2 additions & 1 deletion test/compose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ describe('Utils', () => {
})

it('returns the first given argument if given no functions', () => {
expect(compose<number>()(1, 2)).toBe(1)
expect(compose<[number, number]>()(1, 2)).toBe(1)
expect(compose()('zero', 1, 2)).toBe('zero')
expect(compose()(3)).toBe(3)
expect(compose()(undefined)).toBe(undefined)
})
Expand Down