I have a SASS/SCSS string which contain two lists (separated by comma), and each list contain numbers (separated by space). How can i split string into two lists of numbers?
SCSS:
$values: "10px 20px 30px, 20px 30px 40px";
$begin: /* should be */ "10px", "20px", "30px";
$end: /* should be */ "20px", "30px", "40px";
// optionally it can be a map:
$begin: (10px, 20px, 30px);
$end: (20px, 30px, 40px);
Code on Sass Meister: http://sassmeister.com/gist/4d9c1bd741177636ae1b
Well, you can split the string with this function:
@function str-split($string, $separator) {
// empty array/list
$split-arr: ();
// first index of separator in string
$index : str-index($string, $separator);
// loop through string
@while $index != null {
// get the substring from the first character to the separator
$item: str-slice($string, 1, $index - 1);
// push item to array
$split-arr: append($split-arr, $item);
// remove item and separator from string
$string: str-slice($string, $index + 1);
// find new index of separator
$index : str-index($string, $separator);
}
// add the remaining string to list (the last item)
$split-arr: append($split-arr, $string);
@return $split-arr;
}
In your case you could use it as so:
$values: "10px 20px 30px, 20px 30px 40px";
$list: ();
$split-values: str-split($values, ", ");
@each $value in $split-values {
$list: append($list, str-split($value, " "));
}
As for converting the string values to numbers, check out Hugo Giraudel's function on SassMeister (or read his blog post)
A recursive function can work as well.
@function str-split($string, $separator) {
$i: str-index($string, $separator);
@if $i != null {
@return append(
str-slice($string, 1, $i - 1),
str-split(str-slice($string, $i + str-length($separator)), $separator)
);
}
@return $string
}
$values: '15px 10px 5px';
$result: str-split($values, ' '); /* $result equals '15px' '10px' '5px' */
The var $i
corresponds to the index of the first separator occurence in the given $string
.
The condition to end the recursivity is to ensure the separator does not exist in the remainder.
The function returns a list which contains the substring before the first separator occurence and the value returned by the str-split
function with the remaining substring as parameter.
Note that str-length
is used to enable separators of more than 1 character.
To remove the quotes, you can use unquote($string)
instead of $string
in the return statements.
You can achieve this with little custom SASS function
@function split-str($string, $separator) {
$index : str-index($string, $separator);
$str-1 : str-slice($string, 1, $index - 1);
$str-2 : str-slice($string, $index + 1);
@return $str-1 $str-2;
}
And call this function like this,
$values: "10px 20px 30px , 20px 30px 40px";
$list : split-str($values, ',');
$m-1 : nth($list, 1);
$m-2 : nth($list, 2);
And make sure to leave a space after and before the separator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With