-
-
Save vushe/53db419670fceaea03bb64cfc98c45b6 to your computer and use it in GitHub Desktop.
Sass explode()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @function explode() -- split a string into a list of strings | |
// {string} $string: the string to be split | |
// {string} $delimiter: the boundary string | |
// @return {list} the result list | |
@function explode($string, $delimiter) { | |
$result: (); | |
@if $delimiter == "" { | |
@for $i from 1 through str-length($string) { | |
$result: append($result, str-slice($string, $i, $i)); | |
} | |
@return $result; | |
} | |
$exploding: true; | |
@while $exploding { | |
$d-index: str-index($string, $delimiter); | |
@if $d-index { | |
@if $d-index > 1 { | |
$result: append($result, str-slice($string, 1, $d-index - 1)); | |
$string: str-slice($string, $d-index + str-length($delimiter)); | |
} @else if $d-index == 1 { | |
$string: str-slice($string, 1, $d-index + str-length($delimiter)); | |
} @else { | |
$result: append($result, $string); | |
$exploding: false; | |
} | |
} @else { | |
$result: append($result, $string); | |
$exploding: false; | |
} | |
} | |
@return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment