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

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

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

11

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

Nicolas Lenz's avatar
Nicolas Lenz committed
26
-- |Transforms a String into uppercase where the corresponding list is True. For False the String isn't changed.
27
28
29
30
31
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
32

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

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

Nicolas Lenz's avatar
Nicolas Lenz committed
41
-- |Letterspaces a String with the given number of blanks between the Chars.
42
43
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
44

45
-- |Transforms a character into its double-struck variant (if it is alphanumeric, else it is left unchanged).
46
47
48
49
50
51
52
53
54
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
55
56
57
    | 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
58
toDouble c = c
Nicolas Lenz's avatar
Nicolas Lenz committed
59
60
61
62

-- |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"