Skip to content

Commit ed13d01

Browse files
mdoXhmikosR
authored andcommitted
Update the divide() function
1 parent 290b9ee commit ed13d01

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

scss/_functions.scss

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,43 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003
221221
}
222222

223223
@function divide($dividend, $divisor, $precision: 10) {
224-
$sign: if($dividend > 0 and $divisor > 0, 1, -1);
224+
$sign: if($dividend > 0 and $divisor > 0 or $dividend < 0 and $divisor < 0, 1, -1);
225225
$dividend: abs($dividend);
226226
$divisor: abs($divisor);
227-
$quotient: 0;
228-
$remainder: $dividend;
229227
@if $dividend == 0 {
230228
@return 0;
231229
}
232230
@if $divisor == 0 {
233231
@error "Cannot divide by 0";
234232
}
235-
@if $divisor == 1 {
236-
@return $dividend;
237-
}
238-
@while $remainder >= $divisor {
239-
$quotient: $quotient + 1;
240-
$remainder: $remainder - $divisor;
233+
$remainder: $dividend;
234+
$result: 0;
235+
$factor: 10;
236+
@while ($remainder > 0 and $precision >= 0) {
237+
$quotient: 0;
238+
@while ($remainder >= $divisor) {
239+
$remainder: $remainder - $divisor;
240+
$quotient: $quotient + 1;
241+
}
242+
$result: $result * 10 + $quotient;
243+
$factor: $factor * .1;
244+
$remainder: $remainder * 10;
245+
$precision: $precision - 1;
246+
@if ($precision < 0 and $remainder >= $divisor * 5) {
247+
$result: $result + 1;
248+
}
241249
}
242-
@if $remainder > 0 and $precision > 0 {
243-
$remainder: divide($remainder * 10, $divisor, $precision - 1) * .1;
250+
$result: $result * $factor * $sign;
251+
$dividend-unit: unit($dividend);
252+
$divisor-unit: unit($divisor);
253+
$unit-map: (
254+
"px": 1px,
255+
"rem": 1rem,
256+
"em": 1em,
257+
"%": 1%
258+
);
259+
@if ($dividend-unit != $divisor-unit and map-has-key($unit-map, $dividend-unit)) {
260+
$result: $result * map-get($unit-map, $dividend-unit);
244261
}
245-
@return ($quotient + $remainder) * $sign;
262+
@return $result;
246263
}

scss/mixins/_grid.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@mixin make-col($size: false, $columns: $grid-columns) {
3030
@if $size {
3131
flex: 0 0 auto;
32-
width: percentage(divide($size, $columns));
32+
width: divide(100%, divide($columns, $size));
3333

3434
} @else {
3535
flex: 1 1 0;

0 commit comments

Comments
 (0)