-- module Gargantext.Utils.Chronos
module Gargantext.Core.Utils.Prefix
, something
+ , alphanum
+ , choices
+ , randomString
) where
+import Data.Char (chr, ord)
import Data.Maybe
import Data.Monoid
+import Data.Text (Text, pack)
+import Prelude ((!!))
+import System.Random (initStdGen, uniformR)
-- import Gargantext.Utils.Chronos
import Gargantext.Core.Utils.Prefix
-
+import Gargantext.Prelude
something :: Monoid a => Maybe a -> a
something Nothing = mempty
something (Just a) = a
+
+alphanum :: [Char]
+alphanum = (chr <$> digits) <> (chr <$> lowercase) <> (chr <$> uppercase)
+ where
+ digits = [(ord '0')..(ord '9')]
+ lowercase = [(ord 'a')..(ord 'z')]
+ uppercase = [(ord 'A')..(ord 'Z')]
+
+choices :: Int -> [a] -> IO [a]
+choices 0 _ = pure []
+choices num lst = do
+ gen <- initStdGen
+ let (cIdx, _) = uniformR (0, length lst - 1) gen
+ c = lst !! cIdx
+ choices' <- choices (num - 1) lst
+ pure (c:choices')
+
+randomString :: Int -> IO Text
+randomString num = do
+ str <- choices num alphanum
+ pure $ pack str