From 80878e73acf7fea6731a78ea5165909dc8bae3b9 Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Sat, 6 Apr 2019 18:41:47 -0700 Subject: Refactor pipe to check functions upfront --- src/sunlight-theme.js | 11 ++++++----- 1 file 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() -- cgit v1.2.3