From 828a12ffac60b2fb7e6b0931f610b7ca9f53ffc2 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 22 Jun 2024 08:14:44 +0200 Subject: Props: Support recursive var() if referring to subtree --- src/_functions.scss | 71 ++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) (limited to 'src/_functions.scss') diff --git a/src/_functions.scss b/src/_functions.scss index 9dd14b1..7d4a695 100644 --- a/src/_functions.scss +++ b/src/_functions.scss @@ -10,7 +10,10 @@ //// @use 'sass:map'; +@use 'sass:list'; @use 'sass:math'; +@use 'sass:string'; +@use 'sass:meta'; @use './vars'; /// @@ -23,10 +26,10 @@ /// @return {string} A string with all instances of $search replaced with $replace /// @function str-replace($string, $search, $replace) { - $index: str-index($string, $search); + $index: string.index($string, $search); @if $index { - @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); + @return string.slice($string, 1, $index - 1) + $replace + str-replace(string.slice($string, $index + string.length($search)), $search, $replace); } @return $string; @@ -44,9 +47,9 @@ $result: ''; @each $item in $list { - $result: $result + if(length($item) > 1, str-implode($item, $glue), $item); + $result: $result + if(list.length($item) > 1, str-implode($item, $glue), $item); - @if $item != nth($list, length($list)) { + @if $item != list.nth($list, list.length($list)) { $result: $result + $glue; } } @@ -63,13 +66,13 @@ /// /// @return {list} A slice of the list /// -@function list-slice($list, $start: 1, $end: length($list)) { +@function list-slice($list, $start: 1, $end: list.length($list)) { $result: (); @if $end >= $start { @for $i from $start through $end { @if $i != 0 { - $result: append($result, nth($list, $i), list-separator($list)); + $result: list.append($result, list.nth($list, $i), list.separator($list)); } } } @@ -86,11 +89,11 @@ /// @return {list} A list with $value at the beginning, followed by the other items /// @function list-prepend($list, $value) { - $result: append((), $value, list-separator($list)); + $result: list.append((), $value, list.separator($list)); - @if length($list) > 0 { - @for $i from 1 through length($list) { - $result: append($result, nth($list, $i), list-separator($list)); + @if list.length($list) > 0 { + @for $i from 1 through list.length($list) { + $result: list.append($result, list.nth($list, $i), list.separator($list)); } } @@ -105,13 +108,13 @@ /// @return {list} Teh reversed list /// @function list-reverse($list) { - @if length($list) == 0 { + @if list.length($list) == 0 { @return $list; } $result: (); - @for $i from length($list) * -1 through -1 { - $result: append($result, nth($list, abs($i))); + @for $i from list.length($list) * -1 through -1 { + $result: list.append($result, list.nth($list, math.abs($i))); } @return $result; } @@ -126,11 +129,11 @@ /// /// @return {list} Sorted list /// -@function quicksort($l, $left: 1, $right: length($l)) { +@function quicksort($l, $left: 1, $right: list.length($l)) { @if $left < $right { $pvr: quicksort-partition($l, $left, $right); - $pivot: nth($pvr, 1); - $l: nth($pvr, 2); + $pivot: list.nth($pvr, 1); + $l: list.nth($pvr, 2); $l: quicksort($l, $left, $pivot); $l: quicksort($l, $pivot + 1, $right); } @@ -145,30 +148,30 @@ $start: true; $i: $left; $j: $right - 1; - $pivot: nth($l, $right); + $pivot: list.nth($l, $right); @while ($i < $j) or $start { - @while (nth($l, $i) < $pivot) and ($i < $right - 1) { + @while (list.nth($l, $i) < $pivot) and ($i < $right - 1) { $i: $i + 1; } - @while (nth($l, $j)>= $pivot) and ($j > $left) { + @while (list.nth($l, $j)>= $pivot) and ($j > $left) { $j: $j - 1; } @if $i < $j { - $i-val: nth($l, $i); - $l: set-nth($l, $i, nth($l, $j)); - $l: set-nth($l, $j, $i-val); + $i-val: list.nth($l, $i); + $l: list.set-nth($l, $i, list.nth($l, $j)); + $l: list.set-nth($l, $j, $i-val); } $start: false; } - @if nth($l, $i) > $pivot { - $i-val: nth($l, $i); - $l: set-nth($l, $i, nth($l, $right)); - $l: set-nth($l, $right, $i-val); + @if list.nth($l, $i) > $pivot { + $i-val: list.nth($l, $i); + $l: list.set-nth($l, $i, list.nth($l, $right)); + $l: list.set-nth($l, $right, $i-val); } @return $i $l; @@ -185,10 +188,10 @@ /// @return {any} Either the value assigned to $key or $default /// @function map-get-default($map, $key, $keys...) { - $default: nth($keys, length($keys)); - $keys: list-slice($keys, 1, length($keys) - 1); + $default: list.nth($keys, list.length($keys)); + $keys: list-slice($keys, 1, list.length($keys) - 1); - @return if(map-has-key($map, $key, $keys...), map-get($map, $key, $keys...), $default); + @return if(map.has-key($map, $key, $keys...), map.get($map, $key, $keys...), $default); } /// @@ -204,11 +207,11 @@ @each $key, $value in $map { $value-str: ''; - @if type-of($value) == map { + @if meta.type-of($value) == map { $value-str: '[ ' + map-print($value) + ' ]'; - } @else if type-of($value) == list { + } @else if meta.type-of($value) == list { $value-str: '[ ' + str-implode($value, ', ') + ' ]'; - } @else if type-of($value) == string { + } @else if meta.type-of($value) == string { $value-str: '\'' + $value + '\''; } @else { $value-str: $value; @@ -243,8 +246,8 @@ @if not $sel-match { $suf-match: true; - @for $i from 1 through length($suffix) { - @if $suf-match and (nth($sel, -$i) != nth($suffix, -$i)) { + @for $i from 1 through list.length($suffix) { + @if $suf-match and (list.nth($sel, -$i) != list.nth($suffix, -$i)) { $suf-match: false; } } -- cgit v1.2.3-54-g00ecf