round static method
Creates a round()
calculation with the given strategyOrNumber
,
numberOrStep
, and step
. Strategy must be either nearest, up, down or
to-zero.
Number and step must be either a SassNumber, a SassCalculation, an unquoted SassString, or a CalculationOperation.
This automatically simplifies the calculation, so it may return a SassNumber rather than a SassCalculation. It throws an exception if it can determine that the calculation will definitely produce invalid CSS.
This may be passed fewer than two arguments, but only if one of the
arguments is an unquoted var()
string.
Implementation
static Value round(Object strategyOrNumber,
[Object? numberOrStep, Object? step]) {
switch ((
_simplify(strategyOrNumber),
numberOrStep.andThen(_simplify),
step.andThen(_simplify)
)) {
case (SassNumber number, null, null):
return _matchUnits(number.value.round().toDouble(), number);
case (SassNumber number, SassNumber step, null)
when !number.hasCompatibleUnits(step):
_verifyCompatibleNumbers([number, step]);
return SassCalculation._("round", [number, step]);
case (SassNumber number, SassNumber step, null):
_verifyCompatibleNumbers([number, step]);
return _roundWithStep('nearest', number, step);
case (
SassString(text: 'nearest' || 'up' || 'down' || 'to-zero') &&
var strategy,
SassNumber number,
SassNumber step
)
when !number.hasCompatibleUnits(step):
_verifyCompatibleNumbers([number, step]);
return SassCalculation._("round", [strategy, number, step]);
case (
SassString(text: 'nearest' || 'up' || 'down' || 'to-zero') &&
var strategy,
SassNumber number,
SassNumber step
):
_verifyCompatibleNumbers([number, step]);
return _roundWithStep(strategy.text, number, step);
case (
SassString(text: 'nearest' || 'up' || 'down' || 'to-zero') &&
var strategy,
SassString rest,
null
):
return SassCalculation._("round", [strategy, rest]);
case (
SassString(text: 'nearest' || 'up' || 'down' || 'to-zero'),
_?,
null
):
throw SassScriptException("If strategy is not null, step is required.");
case (
SassString(text: 'nearest' || 'up' || 'down' || 'to-zero'),
null,
null
):
throw SassScriptException(
"Number to round and step arguments are required.");
case (SassString rest, null, null):
return SassCalculation._("round", [rest]);
case (var number, null, null):
throw SassScriptException(
"Single argument $number expected to be simplifiable.");
case (var number, var step?, null):
return SassCalculation._("round", [number, step]);
case (
(SassString(text: 'nearest' || 'up' || 'down' || 'to-zero') ||
SassString(isVar: true)) &&
var strategy,
var number?,
var step?
):
return SassCalculation._("round", [strategy, number, step]);
case (_, _?, _?):
throw SassScriptException(
"$strategyOrNumber must be either nearest, up, down or to-zero.");
case (_, null, _?):
// TODO(pamelalozano): Get rid of this case once dart-lang/sdk#52908 is solved.
// ignore: unreachable_switch_case
case (_, _, _):
throw SassScriptException("Invalid parameters.");
}
}