Created
August 17, 2021 19:33
-
-
Save jakejarvis/f48bce449392b5ef56db15f28525b46c to your computer and use it in GitHub Desktop.
SCSS string split function
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
// Returns a list of strings from a given string separated by a given | |
// separator (defaults to comma). | |
// https://stackoverflow.com/a/65853667/1438024 | |
@function str-split($str, $delimiter: ",") { | |
// return immediately if this function isn't necessary (aka separator not found) | |
@if (string.index("#{$str}", "#{$delimiter}") == null) { | |
@return $str; | |
} | |
$str-list: (); | |
@while string.index("#{$str}", "#{$delimiter}") != null { | |
@if (string.index("#{$str}", "#{$delimiter}") > 1) { | |
$str-list: list.append($str-list, string.slice("#{$str}", 1, string.index("#{$str}", "#{$delimiter}") - 1)); | |
} | |
$str: string.slice("#{$str}", string.index("#{$str}", "#{$delimiter}") + 1, string.length("#{$str}")); | |
} | |
@if (string.slice("#{$str}", 1, string.length("#{$str}")) != "") { | |
$str-list: list.append($str-list, string.slice("#{$str}", 1, string.length("#{$str}"))); | |
} | |
@return $str-list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment