Skip to content

Commit

Permalink
Ignore values not parseable by reduce-css-calc
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Aug 9, 2019
1 parent f5fe518 commit b91f0ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions __tests__/negateValue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import negateValue from '../src/util/negateValue'

test('it negates numeric CSS values', () => {
expect(negateValue('5')).toEqual('-5')
expect(negateValue('10px')).toEqual('-10px')
expect(negateValue('18rem')).toEqual('-18rem')
expect(negateValue('-10')).toEqual('10')
expect(negateValue('-7ch')).toEqual('7ch')
})

test('it leaves keywords untouched', () => {
expect(negateValue('auto')).toEqual('auto')
expect(negateValue('cover')).toEqual('cover')
})
9 changes: 9 additions & 0 deletions src/util/negateValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import reduceCalc from 'reduce-css-calc'

export default function(value) {
try {
return reduceCalc(`calc(${value} * -1)`)
} catch (e) {
return value
}
}
4 changes: 2 additions & 2 deletions src/util/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isFunction from 'lodash/isFunction'
import defaults from 'lodash/defaults'
import map from 'lodash/map'
import toPath from 'lodash/toPath'
import reduceCalc from 'reduce-css-calc'
import negateValue from './negateValue'

const configUtils = {
negative(scale) {
Expand All @@ -12,7 +12,7 @@ const configUtils = {
.reduce(
(negativeScale, key) => ({
...negativeScale,
[`-${key}`]: reduceCalc(`calc(${scale[key]} * -1)`),
[`-${key}`]: negateValue(scale[key]),
}),
{}
)
Expand Down

0 comments on commit b91f0ef

Please sign in to comment.