Skip to content

Fix compose() to have a less surprising API #669

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

Merged
merged 1 commit into from
Sep 1, 2015
Merged
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
26 changes: 11 additions & 15 deletions docs/api/compose.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# `compose(...functions)`

Composes functions from left to right.
Composes functions from right to left.

This is a functional programming utility, and is included in Redux as a convenience.
You might want to use it to apply several [store enhancers](../Glossary.md#store-enhancer) in a row.

#### Arguments

1. (*arguments*): The functions to compose. Each function is expected to accept a function as an argument and to return a function.
1. (*arguments*): The functions to compose. Each function is expected to accept a single parameter. Its return value will be provided as an argument to the function standing to the left, and so on.

#### Returns

(*Function*): The final function obtained by composing the given functions from left to right.
(*Function*): The final function obtained by composing the given functions from right to left.

#### Example

Expand Down Expand Up @@ -39,20 +39,16 @@ if (process.env.NODE_ENV === 'production') {
require('redux-devtools').devTools(),
require('redux-devtools').persistState(
window.location.href.match(/[?&]debug_session=([^&]+)\b/)
),
createStore
);
)
)(createStore);

// Same code without the compose helper:
// Same code without the `compose` helper:
//
// finalCreateStore =
// applyMiddleware(middleware)(
// devTools()(
// persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))(
// createStore
// )
// )
// );
// finalCreateStore = applyMiddleware(middleware)(
// require('redux-devtools').devTools()(
// require('redux-devtools').persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))()
// )
// )(createStore);
}

let store = finalCreateStore(reducer);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function applyMiddleware(...middlewares) {
dispatch: (action) => dispatch(action)
};
chain = middlewares.map(middleware => middleware(middlewareAPI));
dispatch = compose(...chain, store.dispatch);
dispatch = compose(...chain)(store.dispatch);

return {
...store,
Expand Down
11 changes: 5 additions & 6 deletions src/utils/compose.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* Composes functions from left to right.
* Composes single-argument functions from right to left.
*
* @param {...Function} funcs - The functions to compose. Each is expected to
* accept a function as an argument and to return a function.
* @returns {Function} A function obtained by composing functions from left to
* right.
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing functions from right to
* left. For example, compose(f, g, h) is identical to x => h(g(f(x))).
*/
export default function compose(...funcs) {
return funcs.reduceRight((composed, f) => f(composed));
return arg => funcs.reduceRight((composed, f) => f(composed), arg);
}
16 changes: 12 additions & 4 deletions test/utils/compose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { compose } from '../../src';

describe('Utils', () => {
describe('compose', () => {
it('composes functions from left to right', () => {
it('composes from right to left', () => {
const double = x => x * 2;
const square = x => x * x;
expect(compose(square)(5)).toBe(25);
expect(compose(square, double)(5)).toBe(100);
expect(compose(double, square, double)(5)).toBe(200);
});

it('composes functions from right to left', () => {
const a = next => x => next(x + 'a');
const b = next => x => next(x + 'b');
const c = next => x => next(x + 'c');
const final = x => x;

expect(compose(a, b, c, final)('')).toBe('abc');
expect(compose(b, c, a, final)('')).toBe('bca');
expect(compose(c, a, b, final)('')).toBe('cab');
expect(compose(a, b, c)(final)('')).toBe('abc');
expect(compose(b, c, a)(final)('')).toBe('bca');
expect(compose(c, a, b)(final)('')).toBe('cab');
});
});
});