Skip to content

Instantly share code, notes, and snippets.

@monkeymonk
Forked from danielpchen/sass-explode.scss
Created November 5, 2020 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkeymonk/c2559ed64c9a8e526ad32d57026782fd to your computer and use it in GitHub Desktop.
Save monkeymonk/c2559ed64c9a8e526ad32d57026782fd 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