Advertisement
Guest User

Untitled

a guest
May 24th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. //
  2. // Returns list after splitting a string by a delimiter string.
  3. // Similar to PHP's explode function
  4. //
  5. @function explode($string, $delimiter: "", $separator: "space")
  6. @if type-of($string) != "string"
  7. @warn "`explode` function expecting a string; #{type-of($string)} given."
  8. @return null
  9. @if type-of($delimiter) != "string"
  10. @warn "`explode` function expecting a string; #{type-of($delimiter)} given."
  11. @return null
  12. $result: ()
  13. $length: str-length($string)
  14. @if not index("space" "comma", $separator)
  15. $separator: "space"
  16. @if str-length($delimiter) == 0
  17. @for $i from 1 through $length
  18. $result: append($result, str-slice($string, $i, $i))
  19. @return $result
  20. $running: true
  21. $remaining: $string
  22. @while $running
  23. $index: str-index($remaining, $delimiter)
  24. @if not $index
  25. $running: false
  26. @else
  27. $slice: str-slice($remaining, 1, $index - 1)
  28. $result: append($result, $slice, $separator)
  29. $remaining: str-slice($remaining, $index + str-length($delimiter))
  30. @return append($result, $remaining, $separator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement