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:
parent
f86c16b1ed
commit
920f21264c
4 changed files with 47 additions and 80 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue