vendor/assets/scss/util/_math.scss in foundation-rails-6.5.3.0 vs vendor/assets/scss/util/_math.scss in foundation-rails-6.6.1.0
- old
+ new
@@ -68,5 +68,80 @@
@function ratio-to-percentage($ratio) {
$w: nth($ratio, 1);
$h: nth($ratio, 3);
@return $h / $w * 100%;
}
+
+/// Parse the given `$fraction` to numerators and denumerators.
+///
+/// @param {*} $fraction - Value representing a fraction to parse. It can be formatted as `50%`, `1 of 2`, `1/2` or `50` (no denominator would be returned).
+///
+/// @return {List} List of parsed values with numerator at first position and denumerator as second. These values may be null.
+@function zf-parse-fraction($fraction) {
+
+ @if type-of($fraction) == 'number' {
+ // "50%"
+ @if unit($fraction) == '%' {
+ @return (strip-unit($fraction), 100);
+ }
+ @else if (unit($fraction) == '') {
+ // "0.5"
+ @if $fraction < 1 {
+ @return ($fraction * 100, 100);
+ }
+ // "50"
+ @else {
+ @return ($fraction, null);
+ }
+ }
+ }
+
+ @else if type-of($fraction) == 'list' {
+ // "50 of 100", "50/100"...
+ @if length($fraction) == 3
+ and type-of(nth($fraction, 1) == 'number')
+ and type-of(nth($fraction, 3) == 'number') {
+ @return (nth($fraction, 1), nth($fraction, 3));
+ }
+ }
+
+ @return (null, null);
+}
+
+/// Returns whether the given `$value` represents a fraction. Supports formats like `50%`, `1 of 2`, `1 per 2` or `1/2`.
+///
+/// @param {*} $value - Value to test.
+/// @param {Boolean} $allow-no-denominator [false] - If `true`, simple numbers without denominators like `50` are supported.
+///
+/// @return {Boolean} `true` if `$value` represents a fraction, `false` otherwise.
+@function zf-is-fraction($value, $allow-no-denominator: false) {
+ $parsed: zf-parse-fraction($value);
+ @return not(nth($parsed, 1) == null
+ or (nth($parsed, 2) == null and $allow-no-denominator == false));
+}
+
+/// Calculate a percentage from a given fraction.
+///
+/// @param {Number|List} $fraction - Value representing a fraction to use to calculate the percentage, formatted as `50` (relative to `$denominator`), `50%`, `1 of 2` or `1/2`.
+/// @param {Number|List} $denominator - Default value to use as denominator when `$fraction` represents an absolute value.
+@function fraction-to-percentage(
+ $fraction,
+ $denominator: null
+) {
+ $parsed: zf-parse-fraction($fraction);
+ $parsed-nominator: nth($parsed, 1);
+ $parsed-denominator: nth($parsed, 2);
+
+ @if $parsed-nominator == null {
+ @error 'Wrong syntax for "fraction-to-percentage()". Use a number, decimal, percentage, or "n of n" / "n/n".';
+ }
+ @if $parsed-denominator == null {
+ @if type-of($denominator) == 'number' {
+ $parsed-denominator: $denominator;
+ }
+ @else {
+ @error 'Error with "fraction-to-percentage()". A default "$denominator" is required to support absolute values';
+ }
+ }
+
+ @return percentage($parsed-nominator / $parsed-denominator);
+}