Dariusz Sikorski
Dariusz Sikorski

Reputation: 4407

How to split a string into two lists of numbers in 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

Upvotes: 10

Views: 14730

Answers (4)

Luc
Luc

Reputation: 173

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.

Upvotes: 6

dayenite
dayenite

Reputation: 211

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)

Upvotes: 21

patelarpan
patelarpan

Reputation: 7991

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

Upvotes: 2

cimmanon
cimmanon

Reputation: 68319

You don't, at least not without a 3rd party library (which will almost certainly require a custom function written in Ruby). Even if Sass had a native function for splitting strings, it does not have a way to convert a string to a number (and I suspect it never will).

If you want a list of list of numbers, use a list of list of numbers.

Upvotes: -3

Related Questions