diff options
author | Volpeon <git@volpeon.ink> | 2024-06-24 16:43:38 +0200 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2024-06-24 16:43:38 +0200 |
commit | 3293e6fe59141e4322cf56aacfa4a67a5b5dc58c (patch) | |
tree | fa19951d32d089b76788b38377f19563abddde04 | |
parent | Add linear easing func (diff) | |
download | iro-sass-3293e6fe59141e4322cf56aacfa4a67a5b5dc58c.tar.gz iro-sass-3293e6fe59141e4322cf56aacfa4a67a5b5dc58c.tar.bz2 iro-sass-3293e6fe59141e4322cf56aacfa4a67a5b5dc58c.zip |
Add to-number and to-length
-rw-r--r-- | src/_functions.scss | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/_functions.scss b/src/_functions.scss index 7d4a695..8a1a613 100644 --- a/src/_functions.scss +++ b/src/_functions.scss | |||
@@ -16,6 +16,26 @@ | |||
16 | @use 'sass:meta'; | 16 | @use 'sass:meta'; |
17 | @use './vars'; | 17 | @use './vars'; |
18 | 18 | ||
19 | $numbers: ('0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9); | ||
20 | |||
21 | $units: ( | ||
22 | 'px': 1px, | ||
23 | 'cm': 1cm, | ||
24 | 'mm': 1mm, | ||
25 | '%': 1%, | ||
26 | 'ch': 1ch, | ||
27 | 'pc': 1pc, | ||
28 | 'in': 1in, | ||
29 | 'em': 1em, | ||
30 | 'rem': 1rem, | ||
31 | 'pt': 1pt, | ||
32 | 'ex': 1ex, | ||
33 | 'vw': 1vw, | ||
34 | 'vh': 1vh, | ||
35 | 'vmin': 1vmin, | ||
36 | 'vmax': 1vmax | ||
37 | ); | ||
38 | |||
19 | /// | 39 | /// |
20 | /// Replace a substring with a new string. | 40 | /// Replace a substring with a new string. |
21 | /// | 41 | /// |
@@ -290,6 +310,61 @@ | |||
290 | @return math.div($size, $base) * 1rem; | 310 | @return math.div($size, $base) * 1rem; |
291 | } | 311 | } |
292 | 312 | ||
313 | /// | ||
314 | /// Casts a string into a number | ||
315 | /// | ||
316 | /// @param {string|number} $value | ||
317 | /// | ||
318 | /// @return {number} | ||
319 | /// | ||
320 | @function to-number($value) { | ||
321 | @if meta.type-of($value) == 'number' { | ||
322 | @return $value; | ||
323 | } | ||
324 | @if meta.type-of($value) != 'string' { | ||
325 | @error 'Value for `to-number` should be a number or a string.'; | ||
326 | } | ||
327 | |||
328 | $result: 0; | ||
329 | $digits: 0; | ||
330 | $minus: string.slice($value, 1, 1) == '-'; | ||
331 | |||
332 | @for $i from if($minus, 2, 1) through string.length($value) { | ||
333 | $character: string.slice($value, $i, $i); | ||
334 | |||
335 | @if not list.index(map.keys($numbers), $character) or $character == '.' { | ||
336 | @return to-length(if($minus, -$result, $result), string.slice($value, $i)) | ||
337 | } | ||
338 | |||
339 | @if $character == '.' { | ||
340 | $digits: 1; | ||
341 | } @else if $digits == 0 { | ||
342 | $result: $result * 10 + map.get($numbers, $character); | ||
343 | } @else { | ||
344 | $digits: $digits * 10; | ||
345 | $result: $result + math.div(map.get($numbers, $character), $digits); | ||
346 | } | ||
347 | } | ||
348 | |||
349 | @return if($minus, -$result, $result); | ||
350 | } | ||
351 | |||
352 | /// | ||
353 | /// Add $unit to $value | ||
354 | /// | ||
355 | /// @param {number} $value - Value to add unit to | ||
356 | /// @param {string} $unit - String representation of the unit | ||
357 | /// | ||
358 | /// @return {number} $value expressed in $unit | ||
359 | /// | ||
360 | @function to-length($value, $unit) { | ||
361 | @if not list.index(map.keys($units), $unit) { | ||
362 | @error 'Invalid unit `#{$unit}`.'; | ||
363 | } | ||
364 | |||
365 | @return $value * map.get($units, $unit); | ||
366 | } | ||
367 | |||
293 | /// | 368 | /// |
294 | /// A mixin with the sole purpose of letting you use temporary variables without polluting the global namespace. | 369 | /// A mixin with the sole purpose of letting you use temporary variables without polluting the global namespace. |
295 | /// | 370 | /// |