Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Software
Mock
Commits
2db683f4
Commit
2db683f4
authored
Nov 24, 2018
by
Nicolas Lenz
Browse files
Convert API to Text instead of String, v3.0.0
parent
ddbd211a
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/Main.hs
View file @
2db683f4
{-# LANGUAGE OverloadedStrings #-}
module
Main
where
import
Mock
(
styles
)
import
Data.List
import
qualified
Data.Text
as
T
import
qualified
Data.Text.IO
as
T
import
Data.Char
import
Data.Maybe
import
System.Environment
...
...
@@ -10,22 +13,24 @@ import System.Environment
-- |Main function.
main
::
IO
()
main
=
do
args
<-
getArgs
args'
<-
getArgs
let
args
=
map
T
.
pack
args'
-- Transform String arguments into Text
case
args
of
[]
->
putStrLn
help
[
"--help"
]
->
putStrLn
help
[]
->
T
.
putStrLn
help
[
"--help"
]
->
T
.
putStrLn
help
[
style
]
->
do
input
<-
getContents
-- Read from stdin
putStrLn
$
handle
style
[
input
]
(
style
:
text
)
->
putStrLn
$
handle
style
text
input
<-
T
.
getContents
-- Read from stdin
T
.
putStrLn
$
handle
style
[
input
]
(
style
:
str
)
->
T
.
putStrLn
$
handle
style
str
-- |Returns an IO action handling the given list of arguments.
handle
::
String
->
[
String
]
->
String
handle
style
=
fromMaybe
(
const
help
)
(
lookup
style
styles
)
.
dropWhileEnd
isSpace
.
intercalate
" "
handle
::
T
.
Text
->
[
T
.
Text
]
->
T
.
Text
handle
style
=
fromMaybe
(
const
help
)
(
lookup
style
styles
)
.
T
.
dropWhileEnd
isSpace
.
T
.
intercalate
" "
-- |Help string.
help
::
String
help
=
"Mock - a program to transform text.
\n\
\\n\
\
Usage: mock [STYLE] [TEXT]
\n\
\
Styles: "
++
(
intercalate
", "
$
map
fst
styles
)
help
::
T
.
Text
help
=
T
.
unlines
[
"Mock 3.0.0 - a program to transform text."
,
""
,
"Usage: mock [STYLE] [TEXT]"
,
"Styles: "
`
T
.
append
`
(
T
.
intercalate
", "
$
map
fst
styles
)]
mock.cabal
View file @
2db683f4
...
...
@@ -4,10 +4,10 @@ cabal-version: 2.2
--
-- see: https://github.com/sol/hpack
--
-- hash:
386f26c4a9d9d6902416cd8ff7c1215a43e9a87f681dba72300c5060f7419567
-- hash:
17611a7df00c9bd277a9091735ad4a0705598afdc3db7b64ac5852af2f642fa8
name: mock
version:
2
.0.0
version:
3
.0.0
synopsis: GrEAt HAskeLL PrOGRaM to trANsForm tEXT
description: Please see the README at <https://git.eisfunke.com/software/mock#readme>
category: String
...
...
package.yaml
View file @
2db683f4
name
:
mock
version
:
2
.0.0
version
:
3
.0.0
license
:
WTFPL
git
:
"
https://git.eisfunke.com/software/mock"
author
:
"
Nicolas
Lenz"
...
...
@@ -17,7 +17,6 @@ dependencies:
-
base >= 4.7 && <
5
-
random
-
hashable
-
time
-
text
library
:
...
...
src/Mock.hs
View file @
2db683f4
module
Mock
(
styles
,
mockAlternate
,
mockRandom
,
letterspace
,
toDS
)
where
{-# LANGUAGE OverloadedStrings #-}
module
Mock
(
styles
,
mockAlternate
,
mockRandom
,
letterspace
,
toDouble
)
where
import
Data.Char
import
Data.List
import
qualified
Data.Text
as
T
import
Data.Hashable
import
System.Random
-- |List of possible mock style names and their functions.
styles
::
[(
String
,
String
->
String
)]
styles
::
[(
T
.
Text
,
T
.
Text
->
T
.
Text
)]
styles
=
[
(
"random"
,
mockRandom
),
(
"alternate"
,
mockAlternate
),
(
"space"
,
letterspace
1
),
(
"space2"
,
letterspace
2
),
(
"space3"
,
letterspace
3
),
(
"lines"
,
intersperse
'
\n
'
),
(
"upper"
,
map
toUpper
),
(
"lower"
,
map
toLower
),
(
"double"
,
map
toD
S
)]
(
"lines"
,
T
.
intersperse
'
\n
'
),
(
"upper"
,
T
.
map
toUpper
),
(
"lower"
,
T
.
map
toLower
),
(
"double"
,
T
.
map
toD
ouble
)]
-- |Transforms a String into uppercase where the corresponding list is True. For False the String isn't changed.
toUpperBy
::
String
->
[
Bool
]
->
String
toUpperBy
(
c
:
cs
)
(
True
:
bs
)
=
toUpper
c
:
toUpperBy
cs
bs
toUpperBy
(
c
:
cs
)
(
False
:
bs
)
=
c
:
toUpperBy
cs
bs
toUpperBy
(
cs
)
[]
=
cs
toUpperBy
[]
_
=
[]
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
-- |Transforms every other of the Chars of a String into uppercase. The other Chars aren't changed.
mockAlternate
::
String
->
String
mockAlternate
str
=
toUpperBy
str
$
intersperse
True
$
repeat
False
mockAlternate
::
T
.
Text
->
T
.
Text
mockAlternate
=
toUpperBy
$
intersperse
True
$
repeat
False
-- |Tansforms random (that is, random per input String) Chars of a String into uppercase.
mockRandom
::
String
->
String
mockRandom
str
=
toUpperBy
str
(
randoms
$
mkStdGen
(
hash
str
))
mockRandom
::
T
.
Text
->
T
.
Text
mockRandom
txt
=
toUpperBy
(
randoms
$
mkStdGen
(
hash
txt
))
txt
-- |Letterspaces a String with the given number of blanks between the Chars.
letterspace
::
Int
->
String
->
String
letterspace
n
=
intercalate
(
replicate
n
' '
)
.
map
(
\
c
->
[
c
])
letterspace
::
Int
->
T
.
Text
->
T
.
Text
letterspace
n
=
T
.
pack
.
intercalate
(
replicate
n
' '
)
.
map
(
\
c
->
[
c
])
.
T
.
unpack
-- |Transforms a character into its double-struck variant (if it is alphanumeric, else it is left unchanged).
toD
S
::
Char
->
Char
toD
S
'C'
=
chr
8450
toD
S
'H'
=
chr
8461
toD
S
'N'
=
chr
8469
toD
S
'P'
=
chr
8473
toD
S
'Q'
=
chr
8474
toD
S
'R'
=
chr
8477
toD
S
'Z'
=
chr
8484
toD
S
c
toD
ouble
::
Char
->
Char
toD
ouble
'C'
=
chr
8450
toD
ouble
'H'
=
chr
8461
toD
ouble
'N'
=
chr
8469
toD
ouble
'P'
=
chr
8473
toD
ouble
'Q'
=
chr
8474
toD
ouble
'R'
=
chr
8477
toD
ouble
'Z'
=
chr
8484
toD
ouble
c
|
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
toD
S
c
=
c
toD
ouble
c
=
c
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment