diff --git a/__tests__/list.test.scss b/__tests__/list.test.scss new file mode 100644 index 0000000..751ac07 --- /dev/null +++ b/__tests__/list.test.scss @@ -0,0 +1,31 @@ +@use 'true' as *; +@use "../src/utils/list"; + +// TODO: Need more tests.. +$simpleList: (ab cd efgh ijk); +$dupList: (ab cd efgh efgh efgh ijk); + +@include test-module("Remove at list [fn]") { + @include test("simple") { + @include assert-equal( + list.remove($simpleList, "cd"), + (ab efgh ijk) + ); + } + + @include test("duplicate") { + @include assert-equal( + list.remove($dupList, "efgh"), + (ab cd ijk) + ); + } +} + +@include test-module("Convert to str [fn]") { + @include test("simple") { + @include assert-equal( + list.to-string($simpleList), + (((null ab) cd) efgh) ijk + ); + } +} diff --git a/src/utils/_list.scss b/src/utils/_list.scss new file mode 100644 index 0000000..933f923 --- /dev/null +++ b/src/utils/_list.scss @@ -0,0 +1,36 @@ +// https://kittygiraudel.com/2013/08/08/advanced-sass-list-functions/ +// https://gist.github.com/Jakobud/ec056b52f3673cc369dc97f2c2428424 + +@function remove($list, $value, $recursive: false) { + $result: (); + + @for $i from 1 through length($list) { + @if type-of(nth($list, $i)) == list and $recursive { + $result: append($result, remove(nth($list, $i), $value, $recursive)); + } @else if nth($list, $i) != $value { + $result: append($result, nth($list, $i)); + } + } + + @return $result; +} + +@function to-string($list, $glue: '', $is-nested: false) { + $result: null; + + @for $i from 1 through length($list) { + $e: nth($list, $i); + + @if type-of($e) == list { + $result: $result#{to-string($e, $glue, true)}; + } @else { + $result: if( + $i != length($list) or $is-nested, + $result#{$e}#{$glue}, + $result#{$e} + ); + } + } + + @return $result; +}