Skip to content

Add support to compose only functions in utils/compose #2073

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 2 commits into from
Nov 2, 2016
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
2 changes: 2 additions & 0 deletions src/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function compose(...funcs) {
return arg => arg
}

funcs = funcs.filter(func => typeof func === 'function')

if (funcs.length === 1) {
return funcs[0]
}
Expand Down
11 changes: 11 additions & 0 deletions test/compose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ describe('Utils', () => {
expect(compose(c, a, b)(final)('')).toBe('cab')
})

it('composes only functions', () => {
const square = x => x * x
const add = (x, y) => x + y

expect(compose(square, add, false)(1, 2)).toBe(9)
expect(compose(square, add, undefined)(1, 2)).toBe(9)
expect(compose(square, add, true)(1, 2)).toBe(9)
expect(compose(square, add, NaN)(1, 2)).toBe(9)
expect(compose(square, add, '42')(1, 2)).toBe(9)
})

it('can be seeded with multiple arguments', () => {
const square = x => x * x
const add = (x, y) => x + y
Expand Down