Mock.hs 2.34 KB
Newer Older
1
2
3
{-# LANGUAGE OverloadedStrings #-}

module Mock (styles, mockAlternate, mockRandom, letterspace, toDouble) where
Nicolas Lenz's avatar
Nicolas Lenz committed
4

Nicolas Lenz's avatar
Nicolas Lenz committed
5
6
import qualified Data.Text as T
import Data.Text (Text)
Nicolas Lenz's avatar
Nicolas Lenz committed
7
8
import Data.Char
import Data.List
9
import Data.Hashable
Nicolas Lenz's avatar
Nicolas Lenz committed
10
11
import System.Random

12

Nicolas Lenz's avatar
Nicolas Lenz committed
13
-- |List of possible mock style names and their functions.
Nicolas Lenz's avatar
Nicolas Lenz committed
14
styles :: [(Text, Text -> Text)]
Nicolas Lenz's avatar
Nicolas Lenz committed
15
16
styles = [
    ("random", mockRandom),
17
18
19
20
    ("alternate", mockAlternate),
    ("space", letterspace 1),
    ("space2", letterspace 2),
    ("space3", letterspace 3),
21
    ("lines", T.intersperse '\n'),
22
23
    ("upper", T.toUpper),
    ("lower", T.toLower),
Nicolas Lenz's avatar
Nicolas Lenz committed
24
    ("double", T.map toDouble),
Nicolas Lenz's avatar
Nicolas Lenz committed
25
26
    ("cc", mockCC),
    ("b", mockB)]
Nicolas Lenz's avatar
Nicolas Lenz committed
27

Nicolas Lenz's avatar
Nicolas Lenz committed
28
-- |Transforms a String into uppercase where the corresponding list is True. For False the String isn't changed.
29
30
31
32
33
toUpperBy :: [Bool] -> T.Text -> T.Text
toUpperBy bs = T.pack . zipWith f bs . T.unpack where
    f :: Bool -> Char -> Char
    f True c = toUpper c
    f False c = c
Nicolas Lenz's avatar
Nicolas Lenz committed
34

Nicolas Lenz's avatar
Nicolas Lenz committed
35
-- |Transforms every other of the Chars of a String into uppercase. The other Chars aren't changed.
36
37
mockAlternate :: T.Text -> T.Text
mockAlternate = toUpperBy $ intersperse True $ repeat False
Nicolas Lenz's avatar
Nicolas Lenz committed
38

39
-- |Tansforms random (that is, random per input String) Chars of a String into uppercase.
40
41
mockRandom :: T.Text -> T.Text
mockRandom txt = toUpperBy (randoms $ mkStdGen (hash txt)) txt
42

Nicolas Lenz's avatar
Nicolas Lenz committed
43
-- |Letterspaces a String with the given number of blanks between the Chars.
44
45
letterspace :: Int -> T.Text -> T.Text
letterspace n = T.pack . intercalate (replicate n ' ') . map (\c -> [c]) . T.unpack
Nicolas Lenz's avatar
Nicolas Lenz committed
46

47
-- |Transforms a character into its double-struck variant (if it is alphanumeric, else it is left unchanged).
48
49
50
51
52
53
54
55
56
toDouble :: Char -> Char
toDouble 'C' = chr 8450
toDouble 'H' = chr 8461
toDouble 'N' = chr 8469
toDouble 'P' = chr 8473
toDouble 'Q' = chr 8474
toDouble 'R' = chr 8477
toDouble 'Z' = chr 8484
toDouble c
Nicolas Lenz's avatar
Nicolas Lenz committed
57
58
59
    | 48 <= ord c && ord c <= 57 =  chr $ ord c - 48 + 120792  -- Number
    | 65 <= ord c && ord c <= 90 =  chr $ ord c - 65 + 120120  -- Uppercase letter
    | 97 <= ord c && ord c <= 122 = chr $ ord c - 97 + 120146  -- Lowercase letter
60
toDouble c = c
Nicolas Lenz's avatar
Nicolas Lenz committed
61
62
63
64

-- |Replaces all occurences of lowercase "ck" and "k" in a string with "cc"s.
mockCC :: T.Text -> T.Text
mockCC = T.replace "k" "cc" . T.replace "ck" "cc"
Nicolas Lenz's avatar
Nicolas Lenz committed
65
66
67
68

-- |Repaclaces all occurences of "b" and "B" with B button emojis.
mockB :: Text -> Text
mockB = T.replace "b" "🅱️" . T.replace "B" "🅱️"