From 4d54c5d3513521e7446fd833a95c8d8d40593c97 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Tue, 18 Oct 2016 16:48:42 -0700 Subject: [PATCH] Fix units for coercing number operations. --- lib/src/value/number.dart | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/src/value/number.dart b/lib/src/value/number.dart index b805df30..e7b6c49f 100644 --- a/lib/src/value/number.dart +++ b/lib/src/value/number.dart @@ -418,24 +418,24 @@ class SassNumber extends Value { throw new SassScriptException('Undefined operation "$this <= $other".'); } - Value modulo(Value other) { + SassNumber modulo(Value other) { if (other is SassNumber) { - return new SassNumber(_coerceUnits(other, (num1, num2) => num1 % num2)); + return _coerceNumber(other, (num1, num2) => num1 % num2); } throw new SassScriptException('Undefined operation "$this % $other".'); } - Value plus(Value other) { + SassNumber plus(Value other) { if (other is SassNumber) { - return new SassNumber(_coerceUnits(other, (num1, num2) => num1 + num2)); + return _coerceNumber(other, (num1, num2) => num1 + num2); } if (other is! SassColor) return super.plus(other); throw new SassScriptException('Undefined operation "$this + $other".'); } - Value minus(Value other) { + SassNumber minus(Value other) { if (other is SassNumber) { - return new SassNumber(_coerceUnits(other, (num1, num2) => num1 - num2)); + return _coerceNumber(other, (num1, num2) => num1 - num2); } if (other is! SassColor) return super.minus(other); throw new SassScriptException('Undefined operation "$this - $other".'); @@ -476,6 +476,20 @@ class SassNumber extends Value { Value unaryMinus() => new SassNumber(-value); + /// Converts [other]'s value to be compatible with this number's, calls + /// [operation] with the resulting numbers, and wraps the result in a + /// [SassNumber]. + /// + /// Throws a [SassScriptException] if the two numbers' units are incompatible. + SassNumber _coerceNumber( + SassNumber other, num operation(num num1, num num2)) { + var result = _coerceUnits(other, operation); + return new SassNumber.withUnits(result, + numeratorUnits: hasUnits ? this.numeratorUnits : other.numeratorUnits, + denominatorUnits: + hasUnits ? this.denominatorUnits : other.denominatorUnits); + } + /// Converts [other]'s value to be compatible with this number's, and calls /// [operation] with the resulting numbers. ///