Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Returns list after splitting a string by a delimiter string.
- // Similar to PHP's explode function
- //
- @function explode($string, $delimiter: "", $separator: "space")
- @if type-of($string) != "string"
- @warn "`explode` function expecting a string; #{type-of($string)} given."
- @return null
- @if type-of($delimiter) != "string"
- @warn "`explode` function expecting a string; #{type-of($delimiter)} given."
- @return null
- $result: ()
- $length: str-length($string)
- @if not index("space" "comma", $separator)
- $separator: "space"
- @if str-length($delimiter) == 0
- @for $i from 1 through $length
- $result: append($result, str-slice($string, $i, $i))
- @return $result
- $running: true
- $remaining: $string
- @while $running
- $index: str-index($remaining, $delimiter)
- @if not $index
- $running: false
- @else
- $slice: str-slice($remaining, 1, $index - 1)
- $result: append($result, $slice, $separator)
- $remaining: str-slice($remaining, $index + str-length($delimiter))
- @return append($result, $remaining, $separator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement