Skip to content
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
12 changes: 8 additions & 4 deletions src/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
export default function compose(...funcs) {
if (funcs.length === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I be using triple = ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. It's strict equality.

return arg => arg
} else {
const last = funcs[funcs.length - 1]
const rest = funcs.slice(0, -1)
return (...args) => rest.reduceRight((composed, f) => f(composed), last(...args))
}

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

const last = funcs[funcs.length - 1]
const rest = funcs.slice(0, -1)
return (...args) => rest.reduceRight((composed, f) => f(composed), last(...args))
}
6 changes: 6 additions & 0 deletions test/compose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ describe('Utils', () => {
expect(compose()(3)).toBe(3)
expect(compose()()).toBe(undefined)
})

it('returns the first function if given only one', () => {
const fn = () => {}

expect(compose(fn)).toBe(fn)
})
})
})