Simplify negate value (#5389)

* simplify `negateValue`

Co-authored-by: Brad Cornes <bradlc41@gmail.com>

* ensure we have the exact same behaviour

* Simplify/loosen regex to be more future-proof

Co-authored-by: Brad Cornes <bradlc41@gmail.com>
Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
This commit is contained in:
Robin Malfait 2021-09-06 20:26:32 +02:00 • committed by GitHub
parent f86c16b1ed
commit 920f21264c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 80 deletions

View file

@ -1,9 +1,14 @@
import reduceCalc from 'reduce-css-calc'
export default function (value) {
try {
return reduceCalc(`calc(${value} * -1)`)
} catch (e) {
return value
value = `${value}`
// Flip sign of numbers
if (/^[+-]?(\d+|\d*\.\d+)(e[+-]?\d+)?(%|\w+)?$/.test(value)) {
return value.replace(/^[+-]?/, (sign) => (sign === '-' ? '' : '-'))
}
if (value.includes('var(') || value.includes('calc(')) {
return `calc(${value} * -1)`
}
return value
}