/
Text.hs
167 lines (150 loc) · 3.96 KB
/
Text.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- Module : Gen.Text
-- Copyright : (c) 2013-2021 Brendan Hay
-- License : This Source Code Form is subject to the terms of
-- the Mozilla Public License, v. 2.0.
-- A copy of the MPL can be found in the LICENSE file or
-- you can obtain it at http://mozilla.org/MPL/2.0/.
-- Maintainer : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability : provisional
-- Portability : non-portable (GHC extensions)
module Gen.Text
( stripLens,
stripTilUpper,
stripPrefix,
stripSuffix,
renameOperation,
renameServiceFunction,
renameService,
renameBranch,
renameReserved,
lowerHead,
upperHead,
toPascalCase,
toCamelCase,
replaceHead,
)
where
import Control.Error
import Control.Monad
import Data.Bifunctor
import qualified Data.Char as Char
import qualified Data.HashSet as Set
import Data.Text (Text)
import qualified Data.Text as Text
stripLens :: Text -> Text
stripLens t
| "_" `Text.isPrefixOf` t = lowerHead (stripTilUpper t)
| otherwise = t
stripTilUpper :: Text -> Text
stripTilUpper = Text.dropWhile (not . Char.isUpper)
stripPrefix :: Text -> Text -> Text
stripPrefix p t = Text.strip . fromMaybe t $ p `Text.stripPrefix` t
stripSuffix :: Text -> Text -> Text
stripSuffix s t = Text.strip . fromMaybe t $ s `Text.stripSuffix` t
renameOperation :: Text -> Text
renameOperation = Text.dropWhileEnd (not . Char.isAlpha)
renameServiceFunction :: Text -> Text
renameServiceFunction _n = "defaultService"
renameService :: Text -> Text
renameService =
mappend "Amazon "
. flip mappend " SDK"
. stripPrefix "Amazon"
. stripPrefix "AWS"
. stripPrefix "Service"
. stripSuffix "SDK"
renameBranch :: Text -> (Text, Text)
renameBranch = first go . join (,)
where
go = Text.map (\c -> if Char.isAlphaNum c then c else '_') . upperHead
renameReserved :: Text -> Text
renameReserved x
| Text.isPrefixOf "new" x = x <> "'"
| Set.member x xs = x <> "'"
| otherwise = x
where
xs =
Set.fromList $
[ "Service",
"Status",
"ccall",
"as",
"case",
"class",
"data",
"default",
"delete",
"deriving",
"do",
"else",
"export",
"filter",
"forall",
"foreign",
"get",
"group",
"head",
"hiding",
"if",
"import",
"import",
"in",
"infix",
"infixl",
"infixr",
"instance",
"let",
"lex",
"map",
"module",
"newtype",
"object",
"of",
"pattern",
"primitive",
"primitive",
"pure",
"qualified",
"return",
"role",
"tail",
"then",
"type",
"where"
]
toPascalCase :: Text -> Text
toPascalCase = upperHead . toCamelCase
toCamelCase :: Text -> Text
toCamelCase = toCamelCase' . Text.split (not . Char.isAlphaNum) . Text.dropWhile (not . Char.isAlphaNum)
where
toCamelCase' :: [Text] -> Text
toCamelCase' xs =
case filter (not . Text.null) xs of
[] -> ""
y : ys -> mconcat (lowerHead y : map upperHead ys)
lowerHead :: Text -> Text
lowerHead text
| Text.all (\c -> Char.isUpper c || Char.isDigit c) text =
Text.toLower text
| otherwise =
fromMaybe (mapHead Char.toLower text)
. msum
$ map
(\acronym -> mappend (Text.toLower acronym) <$> Text.stripPrefix acronym text)
[ "KMS",
"DB",
"MFA"
]
upperHead :: Text -> Text
upperHead = mapHead Char.toUpper
mapHead :: (Char -> Char) -> Text -> Text
mapHead f text =
fromMaybe text $ do
(c, cs) <- Text.uncons text
pure (Text.cons (f c) cs)
replaceHead :: (Char -> Maybe Text) -> Text -> Text
replaceHead f text =
fromMaybe text $ do
(c, cs) <- Text.uncons text
replace <- f c
pure (replace <> cs)