Skip to content

Fix subtraction expressions #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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