module Stitch.Types.Selector
( Selector(..)
, fromText ) where
import Data.Text (Text)
import Data.String
import Data.Semigroup (Semigroup(..))
import qualified Data.Text as Text
newtype Selector = Selector { unSelector :: [Text] }
deriving (Show, Read, Eq, Ord)
instance IsString Selector where
fromString = fromText . fromString
instance Semigroup Selector where
Selector [] <> Selector ys = Selector ys
Selector xs <> Selector [] = Selector xs
Selector xs <> Selector ys =
Selector $ do
x <- xs
y <- ys
if Text.isInfixOf "&" y
then return $ Text.replace "&" x y
else return $ x <> " " <> y
instance Monoid Selector where
mempty = Selector []
mappend = (<>)
fromText :: Text -> Selector
fromText = Selector . filter (not . Text.null) . map Text.strip . Text.splitOn ","