Skip to content

Commit 5484f62

Browse files
committed
fix (scss): to-number mixin math div
1 parent 19b6906 commit 5484f62

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/scss/02-tools/_f-to-number.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@use "sass:map";
2+
@use "sass:math";
3+
@use "sass:string";
4+
5+
@function to-number($value) {
6+
@if type-of($value) == "number" {
7+
@return $value;
8+
} @else if type-of($value) != "string" {
9+
@error "Value for `to-number` should be a number or a string.";
10+
}
11+
12+
$result: 0;
13+
$digits: 0;
14+
$minus: string.slice($value, 1, 1) == "-";
15+
$numbers: ("0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9);
16+
17+
@for $i from if($minus, 2, 1) through str-length($value) {
18+
$character: string.slice($value, $i, $i);
19+
20+
@if (index(map-keys($numbers), $character) or $character == ".") {
21+
@if $character == "." {
22+
$digits: 1;
23+
} @else if $digits == 0 {
24+
$result: $result * 10 + map.get($numbers, $character);
25+
} @else {
26+
$digits: $digits * 10;
27+
$result: $result + math.div(map.get($numbers, $character), $digits);
28+
}
29+
}
30+
}
31+
32+
@return if($minus, -$result, $result);
33+
}

0 commit comments

Comments
 (0)