From b91f0ef628a27892b5f83cacba610fd93ca22de6 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 9 Aug 2019 16:10:41 -0400 Subject: [PATCH] Ignore values not parseable by reduce-css-calc --- __tests__/negateValue.test.js | 14 ++++++++++++++ src/util/negateValue.js | 9 +++++++++ src/util/resolveConfig.js | 4 ++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 __tests__/negateValue.test.js create mode 100644 src/util/negateValue.js diff --git a/__tests__/negateValue.test.js b/__tests__/negateValue.test.js new file mode 100644 index 000000000..d382db6a4 --- /dev/null +++ b/__tests__/negateValue.test.js @@ -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') +}) diff --git a/src/util/negateValue.js b/src/util/negateValue.js new file mode 100644 index 000000000..bf9111f3e --- /dev/null +++ b/src/util/negateValue.js @@ -0,0 +1,9 @@ +import reduceCalc from 'reduce-css-calc' + +export default function(value) { + try { + return reduceCalc(`calc(${value} * -1)`) + } catch (e) { + return value + } +} diff --git a/src/util/resolveConfig.js b/src/util/resolveConfig.js index 4406e4cd5..41e41526d 100644 --- a/src/util/resolveConfig.js +++ b/src/util/resolveConfig.js @@ -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) { @@ -12,7 +12,7 @@ const configUtils = { .reduce( (negativeScale, key) => ({ ...negativeScale, - [`-${key}`]: reduceCalc(`calc(${scale[key]} * -1)`), + [`-${key}`]: negateValue(scale[key]), }), {} )