Skip to content

Commit

Permalink
♻️ Use fallbackTo in getters
Browse files Browse the repository at this point in the history
  • Loading branch information
exah committed Jul 16, 2018
1 parent 9294e3d commit c8f3cbf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/utils/fns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const noop = () => null
const identity = (val) => val

const fallbackTo = (...args) =>
args.reduce((prev, val) => prev == null ? val : prev, null)

const curry = (fn, ...args) => (
args.length === fn.length
? fn(...args)
Expand Down Expand Up @@ -31,6 +34,7 @@ const combine = (...fns) => (...args) => fns.map((fn) => fn(...args))

export {
noop,
fallbackTo,
identity,
combine,
once,
Expand Down
13 changes: 7 additions & 6 deletions src/utils/getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import { isStr, isArr, isNum, isObj, isFn, isBool } from './is'
import { spaceValue, path } from './helpers'
import { fallbackTo } from './fns'

const themeDefaultPaletteName = path([ DEFAULT_KEY, PALETTE_KEY ], DEFAULT_KEY)
const themeSpaces = path(SPACE_KEY, {})
Expand All @@ -23,10 +24,10 @@ const fromTheme = (key, fallback) =>
const getPalette = (theme, name) => {
const palettes = themePalettes(theme)

return (
palettes[name] ||
palettes[themeDefaultPaletteName(theme)] ||
palettes[DEFAULT_KEY] ||
return fallbackTo(
palettes[name],
palettes[themeDefaultPaletteName(theme)],
palettes[DEFAULT_KEY],
{}
)
}
Expand Down Expand Up @@ -88,15 +89,15 @@ const getSpace = (theme, step) => (mediaKey, exact = false) => {
return size
}
} else if ((exact === true && mediaKey === DEFAULT_KEY) || !exact) {
return themeSize == null ? step : themeSize
return fallbackTo(themeSize, step)
}
return null
}

const spaces = themeSpaces(theme)
const spaceSizes = isArr(spaces)
? spaces
: spaces[mediaKey] || spaces[exact || DEFAULT_KEY]
: fallbackTo(spaces[mediaKey], spaces[exact || DEFAULT_KEY])

if (!spaceSizes) return null

Expand Down

0 comments on commit c8f3cbf

Please sign in to comment.