git.iko.soy/typeable/inflections-hs
on master help sign in
Merge pull request #48 from rcook/rcook-to-humanized
32e69f8Cristhian Motoche
Text/Inflections.hs+29−8
@@ -96,6 +96,7 @@ module Text.Inflections
96 , camelizeCustom
96 , camelizeCustom
97 , dasherize
97 , dasherize
98 , humanize
98 , humanize
99+ , humanizeCustom
99 , underscore
100 , underscore
100 , titleize
101 , titleize
101 , Transliterations
102 , Transliterations
@@ -110,6 +111,7 @@ module Text.Inflections
110 , toUnderscore
111 , toUnderscore
111 , toDashed
112 , toDashed
112 , toCamelCased
113 , toCamelCased
114+ , toHumanized
113 , betterThrow )
115 , betterThrow )
114where
116where
115
117
@@ -118,7 +120,7 @@ import Data.Text (Text)
118import Text.Inflections.Camelize (camelize, camelizeCustom)
120import Text.Inflections.Camelize (camelize, camelizeCustom)
119import Text.Inflections.Dasherize (dasherize)
121import Text.Inflections.Dasherize (dasherize)
120import Text.Inflections.Data (Transliterations, defaultTransliterations)
122import Text.Inflections.Data (Transliterations, defaultTransliterations)
121−import Text.Inflections.Humanize (humanize)
123+import Text.Inflections.Humanize (humanize, humanizeCustom)
122import Text.Inflections.Ordinal (ordinal, ordinalize)
124import Text.Inflections.Ordinal (ordinal, ordinalize)
123import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
125import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
124import Text.Inflections.Parse.CamelCase (parseCamelCase)
126import Text.Inflections.Parse.CamelCase (parseCamelCase)
@@ -152,20 +154,39 @@ toDashed :: Text -> Either (ParseError Char Dec) Text
152toDashed = fmap dasherize . parseCamelCase []
154toDashed = fmap dasherize . parseCamelCase []
153
155
154-- | Transforms underscored_text to CamelCasedText. If first argument is
156-- | Transforms underscored_text to CamelCasedText. If first argument is
155−-- '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
156−-- 'False' then firstCharacter will be in lower case.
158+-- 'False' then the first character will be in lower case.
157--
159--
158−-- > toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
160+-- > toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
159--
161--
160-- >>> toCamelCased True "foo_bar_bazz"
162-- >>> toCamelCased True "foo_bar_bazz"
161-- "FooBarBazz"
163-- "FooBarBazz"
162-- >>> toCamelCased False "foo_bar_bazz"
164-- >>> toCamelCased False "foo_bar_bazz"
163-- "fooBarBazz"
165-- "fooBarBazz"
164toCamelCased
166toCamelCased
165− :: Bool -- ^ Capitalize the first character
167+ :: Bool -- ^ Capitalize the first character
166− -> Text -- ^ Input
168+ -> Text -- ^ Input
167− -> Either (ParseError Char Dec) Text -- ^ Ouput
169+ -> Either (ParseError Char Dec) Text -- ^ Output
168−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 []
169
190
170-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
191-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
171-- 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
Text/Inflections/Humanize.hs+24−3
@@ -13,7 +13,8 @@
13{-# LANGUAGE OverloadedStrings #-}
13{-# LANGUAGE OverloadedStrings #-}
14
14
15module Text.Inflections.Humanize
15module Text.Inflections.Humanize
16− ( humanize )
16+ ( humanize
17+ , humanizeCustom )
17where
18where
18
19
19import Data.Text (Text)
20import Data.Text (Text)
@@ -36,7 +37,27 @@ import Control.Applicative
36humanize
37humanize
37 :: [SomeWord] -- ^ List of words, first of which will be capitalized
38 :: [SomeWord] -- ^ List of words, first of which will be capitalized
38 -> Text -- ^ The humanized output
39 -> Text -- ^ The humanized output
39−humanize xs' =
40+humanize = humanizeCustom True
41+
42+
43+-- | Separate words with spaces, optionally capitalizing the first word. Like
44+-- 'Text.Inflections.Titleize.titleize', this is meant for creating pretty
45+-- output.
46+--
47+-- >>> foo <- SomeWord <$> mkWord "foo"
48+-- >>> bar <- SomeWord <$> mkAcronym "bar"
49+-- >>> bazz <- SomeWord <$> mkWord "bazz"
50+-- >>> humanizeCustom True [foo,bar,bazz]
51+-- "Foo bar bazz"
52+-- >>> humanizeCustom False [foo,bar,bazz]
53+-- "foo bar bazz"
54+--
55+-- /since 0.3.0.0/
56+humanizeCustom
57+ :: Bool -- ^ Whether to capitalize the first character in the output String
58+ -> [SomeWord] -- ^ List of words, first of which will be capitalized
59+ -> Text -- ^ The humanized output
60+humanizeCustom c xs' =
40 case unSomeWord (T.replace "_" " ") <$> xs' of
61 case unSomeWord (T.replace "_" " ") <$> xs' of
41 [] -> ""
62 [] -> ""
42− (x:xs) -> T.unwords $ T.toTitle x : (T.toLower <$> xs)
63+ (x:xs) -> T.unwords $ (if c then T.toTitle else T.toLower) x : (T.toLower <$> xs)
test/Text/Inflections/HumanizeSpec.hs+40−13
@@ -12,16 +12,43 @@ import Control.Applicative
12#endif
12#endif
13
13
14spec :: Spec
14spec :: Spec
15+spec = do
15−spec = describe "humazine" $ do
16+ describe "humanize" $ do
16− it "converts snake case to a human-readable string" $ do
17+ it "converts snake case to a human-readable string" $ do
17− employee <- SomeWord <$> mkWord "employee"
18+ employee <- SomeWord <$> mkWord "employee"
18− salary <- SomeWord <$> mkWord "salary"
19+ salary <- SomeWord <$> mkWord "salary"
19− humanize [employee,salary] `shouldBe` "Employee salary"
20+ humanize [employee,salary] `shouldBe` "Employee salary"
20− it "turns underscores into spaces" $ do
21+ it "turns underscores into spaces" $ do
21− employee <- SomeWord <$> mkWord "employee"
22+ employee <- SomeWord <$> mkWord "employee"
22− has <- SomeWord <$> mkWord "has"
23+ has <- SomeWord <$> mkWord "has"
23− salary <- SomeWord <$> mkWord "salary"
24+ salary <- SomeWord <$> mkWord "salary"
24− humanize [employee, has, salary] `shouldBe` "Employee has salary"
25+ humanize [employee, has, salary] `shouldBe` "Employee has salary"
25− it "capitalizes the first word of a sentence" $ do
26+ it "capitalizes the first word of a sentence" $ do
26− underground <- SomeWord <$> mkWord "underground"
27+ underground <- SomeWord <$> mkWord "underground"
28+ humanize [underground] `shouldBe` "Underground"
29+ describe "humanizeCustom False" $ do
30+ it "converts snake case to a human-readable string" $ do
31+ employee <- SomeWord <$> mkWord "employee"
32+ salary <- SomeWord <$> mkWord "salary"
33+ humanizeCustom False [employee,salary] `shouldBe` "employee salary"
34+ it "turns underscores into spaces" $ do
35+ employee <- SomeWord <$> mkWord "employee"
36+ has <- SomeWord <$> mkWord "has"
37+ salary <- SomeWord <$> mkWord "salary"
38+ humanizeCustom False [employee, has, salary] `shouldBe` "employee has salary"
39+ it "lower-cases the first word of a sentence" $ do
40+ underground <- SomeWord <$> mkWord "underground"
41+ humanizeCustom False [underground] `shouldBe` "underground"
42+ describe "humanizeCustom True" $ do
43+ it "converts snake case to a human-readable string" $ do
44+ employee <- SomeWord <$> mkWord "employee"
45+ salary <- SomeWord <$> mkWord "salary"
46+ humanizeCustom True [employee,salary] `shouldBe` "Employee salary"
47+ it "turns underscores into spaces" $ do
48+ employee <- SomeWord <$> mkWord "employee"
49+ has <- SomeWord <$> mkWord "has"
50+ salary <- SomeWord <$> mkWord "salary"
51+ humanizeCustom True [employee, has, salary] `shouldBe` "Employee has salary"
52+ it "capitalizes the first word of a sentence" $ do
53+ underground <- SomeWord <$> mkWord "underground"
27− humanize [underground] `shouldBe` "Underground"
54+ humanizeCustom True [underground] `shouldBe` "Underground"
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" $