]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Core/Utils.hs
[VERSION] +1 to 0.0.6.1
[gargantext.git] / src / Gargantext / Core / Utils.hs
1 {-|
2 Module : Gargantext.Utils
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
12 -}
13
14
15 module Gargantext.Core.Utils (
16 -- module Gargantext.Utils.Chronos
17 module Gargantext.Core.Utils.Prefix
18 , something
19 , alphanum
20 , choices
21 , randomString
22 ) where
23
24 import Data.Char (chr, ord)
25 import Data.Maybe
26 import Data.Monoid
27 import Data.Text (Text, pack)
28 import Prelude ((!!))
29 import System.Random (initStdGen, uniformR)
30
31 -- import Gargantext.Utils.Chronos
32 import Gargantext.Core.Utils.Prefix
33 import Gargantext.Prelude
34
35
36 something :: Monoid a => Maybe a -> a
37 something Nothing = mempty
38 something (Just a) = a
39
40 alphanum :: [Char]
41 alphanum = (chr <$> digits) <> (chr <$> lowercase) <> (chr <$> uppercase)
42 where
43 digits = [(ord '0')..(ord '9')]
44 lowercase = [(ord 'a')..(ord 'z')]
45 uppercase = [(ord 'A')..(ord 'Z')]
46
47 choices :: Int -> [a] -> IO [a]
48 choices 0 _ = pure []
49 choices num lst = do
50 gen <- initStdGen
51 let (cIdx, _) = uniformR (0, length lst - 1) gen
52 c = lst !! cIdx
53 choices' <- choices (num - 1) lst
54 pure (c:choices')
55
56 randomString :: Int -> IO Text
57 randomString num = do
58 str <- choices num alphanum
59 pure $ pack str