Skip to content

Commit 920f212

Browse files
RobinMalfaitbradlcadamwathan
authored
Simplify negate value (#5389)
* simplify `negateValue` Co-authored-by: Brad Cornes <[email protected]> * ensure we have the exact same behaviour * Simplify/loosen regex to be more future-proof Co-authored-by: Brad Cornes <[email protected]> Co-authored-by: Adam Wathan <[email protected]>
1 parent f86c16b commit 920f212

File tree

4 files changed

+47
-80
lines changed

4 files changed

+47
-80
lines changed

package-lock.json

Lines changed: 0 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"postcss-selector-parser": "^6.0.6",
8787
"postcss-value-parser": "^4.1.0",
8888
"quick-lru": "^5.1.1",
89-
"reduce-css-calc": "^2.1.8",
9089
"resolve": "^1.20.0",
9190
"tmp": "^0.2.1"
9291
},

src/util/negateValue.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import reduceCalc from 'reduce-css-calc'
2-
31
export default function (value) {
4-
try {
5-
return reduceCalc(`calc(${value} * -1)`)
6-
} catch (e) {
7-
return value
2+
value = `${value}`
3+
4+
// Flip sign of numbers
5+
if (/^[+-]?(\d+|\d*\.\d+)(e[+-]?\d+)?(%|\w+)?$/.test(value)) {
6+
return value.replace(/^[+-]?/, (sign) => (sign === '-' ? '' : '-'))
87
}
8+
9+
if (value.includes('var(') || value.includes('calc(')) {
10+
return `calc(${value} * -1)`
11+
}
12+
13+
return value
914
}

tests/resolveConfig.test.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,13 +1320,16 @@ test('custom properties are multiplied by -1 for negative values', () => {
13201320
const userConfig = {
13211321
theme: {
13221322
spacing: {
1323+
0: 0,
13231324
1: '1px',
13241325
2: '2px',
13251326
3: '3px',
13261327
4: '4px',
1328+
auto: 'auto',
13271329
foo: 'var(--foo)',
13281330
bar: 'var(--bar, 500px)',
13291331
baz: 'calc(50% - 10px)',
1332+
qux: '10poops',
13301333
},
13311334
margin: (theme, { negative }) => ({
13321335
...theme('spacing'),
@@ -1345,38 +1348,39 @@ test('custom properties are multiplied by -1 for negative values', () => {
13451348

13461349
const result = resolveConfig([userConfig, defaultConfig])
13471350

1348-
expect(result).toMatchObject({
1349-
prefix: '-',
1350-
important: false,
1351-
separator: ':',
1352-
theme: {
1353-
spacing: {
1354-
1: '1px',
1355-
2: '2px',
1356-
3: '3px',
1357-
4: '4px',
1358-
foo: 'var(--foo)',
1359-
bar: 'var(--bar, 500px)',
1360-
baz: 'calc(50% - 10px)',
1361-
},
1362-
margin: {
1363-
1: '1px',
1364-
2: '2px',
1365-
3: '3px',
1366-
4: '4px',
1367-
foo: 'var(--foo)',
1368-
bar: 'var(--bar, 500px)',
1369-
baz: 'calc(50% - 10px)',
1370-
'-1': '-1px',
1371-
'-2': '-2px',
1372-
'-3': '-3px',
1373-
'-4': '-4px',
1374-
'-foo': 'calc(var(--foo) * -1)',
1375-
'-bar': 'calc(var(--bar, 500px) * -1)',
1376-
'-baz': 'calc(-50% - -10px)',
1377-
},
1378-
},
1379-
variants: {},
1351+
expect(result.theme.spacing).toEqual({
1352+
0: 0,
1353+
1: '1px',
1354+
2: '2px',
1355+
3: '3px',
1356+
4: '4px',
1357+
auto: 'auto',
1358+
foo: 'var(--foo)',
1359+
bar: 'var(--bar, 500px)',
1360+
baz: 'calc(50% - 10px)',
1361+
qux: '10poops',
1362+
})
1363+
expect(result.theme.margin).toEqual({
1364+
0: 0,
1365+
1: '1px',
1366+
2: '2px',
1367+
3: '3px',
1368+
4: '4px',
1369+
auto: 'auto',
1370+
foo: 'var(--foo)',
1371+
bar: 'var(--bar, 500px)',
1372+
baz: 'calc(50% - 10px)',
1373+
qux: '10poops',
1374+
'-0': '-0',
1375+
'-1': '-1px',
1376+
'-2': '-2px',
1377+
'-3': '-3px',
1378+
'-4': '-4px',
1379+
'-auto': 'auto',
1380+
'-foo': 'calc(var(--foo) * -1)',
1381+
'-bar': 'calc(var(--bar, 500px) * -1)',
1382+
'-baz': 'calc(calc(50% - 10px) * -1)',
1383+
'-qux': '-10poops',
13801384
})
13811385
})
13821386

0 commit comments

Comments
 (0)