Skip to content

Commit

Permalink
Merge pull request #39 from gyoshev/fix-subtraction-expressions
Browse files Browse the repository at this point in the history
Fix subtraction expressions
  • Loading branch information
Semigradsky authored Oct 12, 2017
2 parents ef5ea6a + e7fb818 commit 7fd230f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,24 @@ test(
'calc(500px/2px)',
'Cannot divide by "px", number expected'
)

test(
'should reduce substraction from zero',
testFixture,
'calc( 0 - 10px)',
'-10px'
)

test(
'should reduce subtracted expression from zero',
testFixture,
'calc( 0 - calc(1px + 1em) )',
'calc(-1px + -1em)'
)

test(
'should reduce nested expression',
testFixture,
'calc( (1em - calc( 10px + 1em)) / 2)',
'-5px'
)
30 changes: 24 additions & 6 deletions src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ function convertMathExpression(node, precision) {
return node
}

function flip(operator) {
return operator === '+' ? '-' : '+'
}

function flipValue(node) {
if (isValueType(node.type))
node.value = -node.value
else if (node.type == 'MathExpression') {
node.left = flipValue(node.left)
node.right = flipValue(node.right)
}
return node
}

function reduceAddSubExpression(node, precision) {
const {left, right, operator: op} = node

Expand All @@ -77,6 +91,10 @@ function reduceAddSubExpression(node, precision) {
if (left.value === 0 && op === "+")
return right

// 0 - something => -something
if (left.value === 0 && op === "-")
return flipValue(right)

// value + value
// value - value
if (left.type === right.type && isValueType(left.type)) {
Expand Down Expand Up @@ -115,13 +133,13 @@ function reduceAddSubExpression(node, precision) {
}
// value + (something + value) => (value + value) + something
// value + (something - value) => (value - value) + something
// value - (something + value) => (value + value) - something
// value - (something - value) => (value - value) - something
// value - (something + value) => (value - value) - something
// value - (something - value) => (value + value) - something
else if (left.type === right.right.type) {
node = Object.assign({ }, node)
node.left = reduce({
type: 'MathExpression',
operator: right.operator,
operator: op === '-' ? flip(right.operator) : right.operator,
left: left,
right: right.right
}, precision)
Expand Down Expand Up @@ -183,7 +201,7 @@ function reduceAddSubExpression(node, precision) {
return node
}

function reduceDivisionExpression(node) {
function reduceDivisionExpression(node, precision) {
if (!isValueType(node.right.type))
return node

Expand All @@ -201,7 +219,7 @@ function reduceDivisionExpression(node) {
) {
node.left.left.value /= node.right.value
node.left.right.value /= node.right.value
return node.left
return reduce(node.left, precision)
}
return node
}
Expand Down Expand Up @@ -255,7 +273,7 @@ function reduceMathExpression(node, precision) {
case "-":
return reduceAddSubExpression(node, precision)
case "/":
return reduceDivisionExpression(node)
return reduceDivisionExpression(node, precision)
case "*":
return reduceMultiplicationExpression(node)
}
Expand Down

0 comments on commit 7fd230f

Please sign in to comment.