Skip to content

Commit

Permalink
should work with falsy values (Fix #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreBonaventure committed Mar 25, 2018
1 parent 98c9ab7 commit 809d324
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export function convertBreakpointsToMediaQueries(breakpoints) {

export function transformValuesFromBreakpoints(breakpoints, values, currentBreakpoint) {
const findClosestValue = (currentBreakpoint) => {
if (values[currentBreakpoint] !== undefined) return values[currentBreakpoint]
const index = breakpoints.findIndex(b => b === currentBreakpoint)
const newBreakpoint = index !== -1 || index !== 0 ? breakpoints[index-1] : null
if (!newBreakpoint) return values[index]
return values[newBreakpoint] || findClosestValue(newBreakpoint)
return values[newBreakpoint] !== undefined ? values[newBreakpoint] : findClosestValue(newBreakpoint)
}
const result = values[currentBreakpoint] || findClosestValue(currentBreakpoint)
return result
return findClosestValue(currentBreakpoint)
}

export function selectBreakpoints(breakpoints, currentBreakpoint) {
Expand Down
13 changes: 13 additions & 0 deletions test/specs/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ test('transformValuesFromBreakpoints should return values with mobile-first over
t.equal(result2, 1)
})

test('transformValuesFromBreakpoints should work with falsy values', (t) => {
t.plan(2)
const breakpoints = ['sm', 'md', 'lg']
const values = {
sm: false,
lg: true,
}
const result1 = transformValuesFromBreakpoints(breakpoints, values, 'sm')
t.equal(result1, false)
const result2 = transformValuesFromBreakpoints(breakpoints, values, 'md')
t.equal(result2, false)
})

test('selectBreakpoints', (t) => {
t.plan(1)
const breakpoints = ['sm', 'md', 'lg']
Expand Down

0 comments on commit 809d324

Please sign in to comment.