git.iko.soy/typeable/inflections-hs
on master help sign in
Implement toHumanized helper function
892e0d5Richard Cook
Text/Inflections.hs+27−7
@@ -111,6 +111,7 @@ module Text.Inflections
111 , toUnderscore
111 , toUnderscore
112 , toDashed
112 , toDashed
113 , toCamelCased
113 , toCamelCased
114+ , toHumanized
114 , betterThrow )
115 , betterThrow )
115where
116where
116
117
@@ -153,20 +154,39 @@ toDashed :: Text -> Either (ParseError Char Dec) Text
153toDashed = fmap dasherize . parseCamelCase []
154toDashed = fmap dasherize . parseCamelCase []
154
155
155-- | Transforms underscored_text to CamelCasedText. If first argument is
156-- | Transforms underscored_text to CamelCasedText. If first argument is
156−-- 'True' then FirstCharacter in result string will be in upper case. If
157+-- 'True' then the first character in the result string will be in upper case. If
157−-- 'False' then firstCharacter will be in lower case.
158+-- 'False' then the first character will be in lower case.
158--
159--
159−-- > toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
160+-- > toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
160--
161--
161-- >>> toCamelCased True "foo_bar_bazz"
162-- >>> toCamelCased True "foo_bar_bazz"
162-- "FooBarBazz"
163-- "FooBarBazz"
163-- >>> toCamelCased False "foo_bar_bazz"
164-- >>> toCamelCased False "foo_bar_bazz"
164-- "fooBarBazz"
165-- "fooBarBazz"
165toCamelCased
166toCamelCased
166− :: Bool -- ^ Capitalize the first character
167+ :: Bool -- ^ Capitalize the first character
167− -> Text -- ^ Input
168+ -> Text -- ^ Input
168− -> Either (ParseError Char Dec) Text -- ^ Ouput
169+ -> Either (ParseError Char Dec) Text -- ^ Output
169−toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
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 []
170
190
171-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
191-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
172-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
192-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
test/Text/InflectionsSpec.hs+8
@@ -27,6 +27,14 @@ spec = do
27 it "converts snake case to camel case with the first word capitalized" $
27 it "converts snake case to camel case with the first word capitalized" $
28 toCamelCased True "underscored_text" `shouldBe` Right "UnderscoredText"
28 toCamelCased True "underscored_text" `shouldBe` Right "UnderscoredText"
29
29
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+
30 describe "betterThrow" $ do
38 describe "betterThrow" $ do
31 context "when given a parse error" $
39 context "when given a parse error" $
32 it "throws the correct exception" $
40 it "throws the correct exception" $