diff options
author | Nick Van Doorn <vandoorn.nick@gmail.com> | 2019-04-06 18:41:47 -0700 |
---|---|---|
committer | Nick Van Doorn <vandoorn.nick@gmail.com> | 2019-04-06 18:41:47 -0700 |
commit | 80878e73acf7fea6731a78ea5165909dc8bae3b9 (patch) | |
tree | 84c84ae982043494d6186f499db17576a98619ca | |
parent | f75b84605198c04d96ed285960ce8d029bcf0175 (diff) |
Refactor pipe to check functions upfront
-rw-r--r-- | src/sunlight-theme.js | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/sunlight-theme.js b/src/sunlight-theme.js index e640003..a65f6e6 100644 --- a/src/sunlight-theme.js +++ b/src/sunlight-theme.js @@ -2,13 +2,14 @@ import React from 'react' import { useSunlight } from 'use-sunlight' import { ThemeProvider } from 'emotion-theming' -const isFn = t => typeof t === 'function' +const notFn = t => typeof t !== 'function' -const pipeReducer = (a, b) => arg => { - if (!isFn(a) || !isFn(b)) throw new Error('Middleware has to be function') - return b(a(arg)) +const pipeReducer = (a, b) => arg => b(a(arg)) + +const pipe = (...fns) => { + if (fns.some(notFn)) throw new Error('All middleware must be a function') + return fns.reduce(pipeReducer, k => k) } -const pipe = (...ops) => ops.reduce(pipeReducer) export const SunlightTheme = ({ children, middleware }) => { const [lightLevel] = useSunlight() |