@@ -2,22 +2,39 @@ module Data.UUID
22 ( UUID()
33 , GENUUID()
44 , genUUID
5+ , parseUUID
56 ) where
67
78import Prelude
89import Control.Monad.Eff
10+import Data.Maybe
911
12+-- | Represents a UUID String.
13+-- | Most UUID instances defer to String
1014newtype UUID = UUID String
1115
16+-- | The effect of generating a new UUID.
1217foreign import data GENUUID :: !
1318
1419foreign import getUUIDImpl :: forall e. Eff (uuid :: GENUUID | e) String
1520
16−-- | Generates a v4 UUID with default options.
21+-- | Generates a v4 UUID
1722genUUID :: forall e. Eff (uuid :: GENUUID | e) UUID
18−genUUID = do
19− uuidStr <- getUUIDImpl
20− pure $ UUID uuidStr
23+genUUID = getUUIDImpl >>= pure <<< UUID
24+
25+foreign import validateV4UUID :: String -> Boolean
26+
27+-- | Validates a String as a v4 UUID
28+parseUUID :: forall e. String -> Maybe UUID
29+parseUUID str = case validateV4UUID str of
30+ true -> Just $ UUID str
31+ _ -> Nothing
2132
2233instance showUUID :: Show UUID where
2334 show (UUID uuid) = uuid
35+
36+instance eqUUID :: Eq UUID where
37+ eq (UUID uA) (UUID uB) = uA == uB
38+
39+instance ordUUID :: Ord UUID where
40+ compare (UUID uA) (UUID uB) = compare uA uB