range.down()
The math.range-down()
or range.down()
function returns the range of numbers where $from
is higher than $to
.
// Sass.
@use 'sass:list';
// Functions.
@use '../../meta/meta.of-type.function';
// The `math.range-down()` or `range.down()` function.
@function down($from, $to, $step: 1, $except: null, $separator: auto) {
@if meta.of-type(number, $from, $to, $step) and $from > $to {
$result: ();
$number: $from;
@if $step > 0 {
$step: $step * -1;
}
@while $number >= $to {
$result: if(
not $except or ($except and not list.index($except, $number)),
list.append($result, $number, $separator),
$result
);
$number: $number + $step;
}
@return if(list.length($result) > 0, $result, null);
}
@return null;
}
https://github.com/angular-package/sass/blob/main/math/range/_range.down.function.scss
// Sass.
@use 'sass:list';
// Functions.
@use '../../meta/meta.of-type.function';
// Status: DONE
// The `range.down()` function returns the range of numbers where `$from` is higher than `$to`.
// @param `$from` The value of the number type indicates the beginning of the range.
// @param `$to` The value of the number type indicates the end of the range.
// @param `$step` The value of the number type indicates each increment of the range. By default, it's 1.
// @param `$except` The value of a number or list of numbers not belonging to the range.
// @param `$separator` A separator between the given range numbers.
// @return The return value is the list of numbers of the given range.
@function down($from, $to, $step: 1, $except: null, $separator: auto) {
@if meta.of-type(number, $from, $to, $step) and $from > $to {
$result: ();
$number: $from;
@if $step > 0 {
$step: $step * -1;
}
@while $number >= $to {
$result: if(not $except or ($except and not list.index($except, $number)), list.append($result, $number, $separator), $result);
$number: $number + $step;
}
@return if(list.length($result) > 0, $result, null);
}
@return null;
}
// Examples.
// @debug down(20, 15); // 20 19 18 17 16 15
// step 0.1
// @debug down(1, 0.1, 0.1); // 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
// step 2
// @debug down(15, -3, 2); // 15 13 11 9 7 5 3 1 -1 -3
// except
// @debug down(35, 20, 1, 20 35); // 34 33 32 31 30 29 28 27 26 25 24 23 22 21
// separator
// @debug down(25, 20, $separator: comma); // 25, 24, 23, 22, 21, 20
// null
// @debug down(20, 35); // null
Parameters
$from
$from
The value of the number
type indicates the beginning of the range.
$to
$to
The value of number
type indicates the end of the range.
$step: 1
$step: 1
The value of number
type indicates each increment of the range. By default, it's 1
.
$except: null
$except: null
The value of a number
or list
of numbers not belonging to the range.
$separator: auto
$separator: auto
A separator between the given range numbers.
Return
The return value is the list of numbers of the given range.
Examples
// Use.
@use '@angular-package/sass/math';
@use '@angular-package/sass/math/range';
// Examples.
@debug math.range-down(20, 15); // 20 19 18 17 16 15
// step 0.1
@debug math.range-down(0.1, 1, 0.1); // 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
// step 2
@debug math.range-down(15, -3, 2); // 15 13 11 9 7 5 3 1 -1 -3
// except
@debug math.range-down(35, 20, 1, 20 35); // 34 33 32 31 30 29 28 27 26 25 24 23 22 21
// separator
@debug math.range-down(25, 20, $separator: comma); // 25, 24, 23, 22, 21, 20
// null
@debug math.range-down(20, 35); // null
Last updated
Was this helpful?