git.iko.soy/typeable/inflections-hs
on master help sign in
Implement toHumanized helper function
commit892e0d5f6dbf4961e0132a6d9419901f663ea678 authorRichard Cook <rcook@rcook.org> dateSat 22 Jul 2017, 09:49 (9 years ago) parenta902fa0
2 files changed+35−7
Text/Inflections.hs+27−7 test/Text/InflectionsSpec.hs+8
Text/Inflections.hs+27−7
@@ -111,6 +111,7 @@ module Text.Inflections
111111 , toUnderscore
112112 , toDashed
113113 , toCamelCased
114+ , toHumanized
114115 , betterThrow )
115116where
116117
@@ -153,20 +154,39 @@ toDashed :: Text -> Either (ParseError Char Dec) Text
153154toDashed = fmap dasherize . parseCamelCase []
154155
155156-- | Transforms underscored_text to CamelCasedText. If first argument is
156−-- 'True' then FirstCharacter in result string will be in upper case. If
157−-- 'False' then firstCharacter will be in lower case.
157+-- 'True' then the first character in the result string will be in upper case. If
158+-- 'False' then the first character will be in lower case.
158159--
159−-- > toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
160+-- > toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
160161--
161162-- >>> toCamelCased True "foo_bar_bazz"
162163-- "FooBarBazz"
163164-- >>> toCamelCased False "foo_bar_bazz"
164165-- "fooBarBazz"
165166toCamelCased
166− :: Bool -- ^ Capitalize the first character
167− -> Text -- ^ Input
168− -> Either (ParseError Char Dec) Text -- ^ Ouput
169−toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
167+ :: Bool -- ^ Capitalize the first character
168+ -> Text -- ^ Input
169+ -> Either (ParseError Char Dec) Text -- ^ Output
170+toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
171+
172+-- | Transforms underscored_text to space-separated human-readable text.
173+-- If first argument is 'True' then the first character in the result
174+-- string will be in upper case. If 'False' then the first character will be
175+-- in lower case.
176+--
177+-- > toHumanized c = fmap (humanizeCustom c) . parseSnakeCase []
178+--
179+-- >>> toHumanized True "foo_bar_bazz"
180+-- "Foo bar bazz"
181+-- >>> toHumanized False "foo_bar_bazz"
182+-- "foo bar bazz"
183+--
184+-- /since 0.3.0.0/
185+toHumanized
186+ :: Bool -- ^ Capitalize the first character
187+ -> Text -- ^ Input
188+ -> Either (ParseError Char Dec) Text -- ^ Output
189+toHumanized c = fmap (humanizeCustom c) . parseSnakeCase []
170190
171191-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
172192-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
test/Text/InflectionsSpec.hs+8
@@ -27,6 +27,14 @@ spec = do
2727 it "converts snake case to camel case with the first word capitalized" $
2828 toCamelCased True "underscored_text" `shouldBe` Right "UnderscoredText"
2929
30+ describe "toHumanized" $ do
31+ context "when the first argument is False" $
32+ it "converts snake case to human-readable form with lower-case initial letter" $
33+ toHumanized False "underscored_text" `shouldBe` Right "underscored text"
34+ context "when the first argument is True" $
35+ it "converts snake case to human-readable form with the first word capitalized" $
36+ toHumanized True "underscored_text" `shouldBe` Right "Underscored text"
37+
3038 describe "betterThrow" $ do
3139 context "when given a parse error" $
3240 it "throws the correct exception" $