Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string into two lists of numbers in SASS?

Tags:

css

sass

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

like image 654
Dariusz Sikorski Avatar asked Sep 03 '15 13:09

Dariusz Sikorski


3 Answers

Well, you can split the string with this function:

str-split

@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;
}


Usage

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, " "));
}



Converting to numbers

As for converting the string values to numbers, check out Hugo Giraudel's function on SassMeister (or read his blog post)

like image 80
dayenite Avatar answered Nov 04 '22 21:11

dayenite


A recursive function can work as well.

Function

@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
}

Usage

$values: '15px 10px 5px';
$result: str-split($values, ' '); /* $result equals '15px' '10px' '5px' */

Explanation

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.

like image 5
Luc Avatar answered Nov 04 '22 21:11

Luc


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

like image 1
patelarpan Avatar answered Nov 04 '22 19:11

patelarpan