Skip to content

Commit

Permalink
Update modulo function to return SassNumber and corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Jul 11, 2023
1 parent 91277f0 commit dd86321
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
emitted in source order as much as possible, instead of always being emitted
after the CSS of all module dependencies.

* All functions defined in CSS Values and Units 4 are now parsed as calculation objects:
round(), mod(), rem(), sin(), cos(), tan(), asin(), acos(), atan(), atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), and sign().
* All functions defined in CSS Values and Units 4 are now parsed as calculation
objects: round(), mod(), rem(), sin(), cos(), tan(), asin(), acos(), atan(),
atan2(), pow(), sqrt(), hypot(), log(), exp(), abs(), and sign().

### JavaScript API

Expand Down
47 changes: 26 additions & 21 deletions lib/src/value/calculation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value sqrt(Object argument) =>
_singleArgument("sqrt", argument, number_lib.sqrt, false);
_singleArgument("sqrt", argument, number_lib.sqrt);

/// Creates a `sin()` calculation with the given [argument].
///
Expand All @@ -182,7 +182,8 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value sin(Object argument) =>
_singleArgument("sin", argument, number_lib.sin, true);
_singleArgument("sin", argument, number_lib.sin,
requireUnits: true, requireKnownUnits: true);

/// Creates a `cos()` calculation with the given [argument].
///
Expand All @@ -194,7 +195,8 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value cos(Object argument) =>
_singleArgument("cos", argument, number_lib.cos, true);
_singleArgument("cos", argument, number_lib.cos,
requireUnits: true, requireKnownUnits: true);

/// Creates a `tan()` calculation with the given [argument].
///
Expand All @@ -206,7 +208,8 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value tan(Object argument) =>
_singleArgument("tan", argument, number_lib.tan, true);
_singleArgument("tan", argument, number_lib.tan,
requireUnits: true, requireKnownUnits: true);

/// Creates an `atan()` calculation with the given [argument].
///
Expand All @@ -218,7 +221,7 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value atan(Object argument) =>
_singleArgument("atan", argument, number_lib.atan, false);
_singleArgument("atan", argument, number_lib.atan);

/// Creates an `asin()` calculation with the given [argument].
///
Expand All @@ -230,7 +233,7 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value asin(Object argument) =>
_singleArgument("asin", argument, number_lib.asin, false);
_singleArgument("asin", argument, number_lib.asin);

/// Creates an `acos()` calculation with the given [argument].
///
Expand All @@ -242,7 +245,7 @@ class SassCalculation extends Value {
/// [SassNumber] rather than a [SassCalculation]. It throws an exception if it
/// can determine that the calculation will definitely produce invalid CSS.
static Value acos(Object argument) =>
_singleArgument("acos", argument, number_lib.acos, false);
_singleArgument("acos", argument, number_lib.acos);

/// Creates an `abs()` calculation with the given [argument].
///
Expand Down Expand Up @@ -449,7 +452,7 @@ class SassCalculation extends Value {
var result = dividend.modulo(modulus);
if (modulus.value.signIncludingZero != dividend.value.signIncludingZero) {
if (modulus.value.isInfinite) return dividend;
if (result is SassNumber && result.value == 0) {
if (result.value == 0) {
return result.unaryMinus();
}
return result.minus(dividend);
Expand Down Expand Up @@ -543,25 +546,25 @@ class SassCalculation extends Value {
"Number to round and step arguments are required.");

case (
(SassString() || CalculationInterpolation()) && var strategy,
(SassString() || CalculationInterpolation()) && var rest,
null,
null
):
return SassCalculation._("round", [strategy]);
return SassCalculation._("round", [rest]);

case (var number, null, null):
throw SassScriptException(
"Single argument $number expected to be simplifiable.");

case (var strategy, var number?, null):
return SassCalculation._("round", [strategy, number]);
case (var number, var step?, null):
return SassCalculation._("round", [number, step]);

case (
(SassString(text: 'nearest' || 'up' || 'down' || 'to-zero') ||
SassString(isVar: true)) &&
var strategy,
Object number,
Object step
var number?,
var step?
):
return SassCalculation._("round", [strategy, number, step]);

Expand Down Expand Up @@ -644,9 +647,10 @@ class SassCalculation extends Value {
SassCalculation._(this.name, this.arguments);

// Returns [value] coerced to [number]'s units.
static SassNumber _matchUnits(double value, SassNumber number) {
return SassNumber.withUnits(value, numeratorUnits: number.numeratorUnits);
}
static SassNumber _matchUnits(double value, SassNumber number) =>
SassNumber.withUnits(value,
numeratorUnits: number.numeratorUnits,
denominatorUnits: number.denominatorUnits);

/// Returns a rounded [number] based on a selected rounding [strategy],
/// to the nearest integer multiple of [step].
Expand Down Expand Up @@ -766,14 +770,15 @@ class SassCalculation extends Value {

/// Returns a [Callable] named [name] that calls a single argument
/// math function.
static Value _singleArgument(String name, Object argument,
SassNumber mathFunc(SassNumber value), bool acceptsKnownUnits) {
static Value _singleArgument(
String name, Object argument, SassNumber mathFunc(SassNumber value),
{bool requireUnits = false, bool requireKnownUnits = false}) {
argument = _simplify(argument);
if (argument is! SassNumber ||
(acceptsKnownUnits && argument.hasUnit('%'))) {
(requireKnownUnits && argument.hasUnit('%'))) {
return SassCalculation._(name, [argument]);
}
if (!acceptsKnownUnits) argument.assertNoUnits();
if (!requireUnits) argument.assertNoUnits();
return mathFunc(argument);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/value/number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ abstract class SassNumber extends Value {

/// @nodoc
@internal
Value modulo(Value other) {
SassNumber modulo(Value other) {
if (other is SassNumber) {
return withValue(_coerceUnits(other, moduloLikeSass));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/value/number/unitless.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class UnitlessSassNumber extends SassNumber {
return super.lessThanOrEquals(other);
}

Value modulo(Value other) {
SassNumber modulo(Value other) {
if (other is SassNumber) {
return other.withValue(moduloLikeSass(value, other.value));
}
Expand Down

0 comments on commit dd86321

Please sign in to comment.