Skip to content

Instantly share code, notes, and snippets.

@danielpchen
Created February 19, 2016 05:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danielpchen/3677421ea15dcf2579ff to your computer and use it in GitHub Desktop.
Save danielpchen/3677421ea15dcf2579ff to your computer and use it in GitHub Desktop.
Sass explode()
// @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