module Data.List.Extra (trim, splitOn) where

import Data.Char (isSpace)
import Data.List (dropWhileEnd)

-- | Remove spaces from either side of a string. A combination of 'trimEnd' and 'trimStart'.
--
-- > trim      "  hello   " == "hello"
-- > trimStart "  hello   " == "hello   "
-- > trimEnd   "  hello   " == "  hello"
-- > \s -> trim s == trimEnd (trimStart s)
trim :: String -> String
trim :: String -> String
trim = String -> String
trimEnd (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
trimStart

-- | Remove spaces from the start of a string, see 'trim'.
trimStart :: String -> String
trimStart :: String -> String
trimStart = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace

-- | Remove spaces from the end of a string, see 'trim'.
trimEnd :: String -> String
trimEnd :: String -> String
trimEnd = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd Char -> Bool
isSpace

-- TODO: Use doctests after fixing: https://github.com/martijnbastiaan/doctest-parallel/issues/87

-- | Break a list into pieces separated by the first argument, consuming the delimiter.
--
-- > splitOn '.' "A.B"
-- ["A","B"]
-- > splitOn '.' "A.B.C"
-- ["A","B","C"]
-- > splitOn '.' "."
-- ["",""]
-- > splitOn '.' ""
-- [""]
splitOn :: Eq a => a -> [a] -> [[a]]
splitOn :: forall a. Eq a => a -> [a] -> [[a]]
splitOn a
needle [a]
haystack =
  case (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
needle) [a]
haystack of
    ([a]
chunk, []) -> [[a]
chunk]
    ([a]
chunk, a
_ : [a]
rest) -> [a]
chunk [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: a -> [a] -> [[a]]
forall a. Eq a => a -> [a] -> [[a]]
splitOn a
needle [a]
rest