git.iko.soy/typeable/inflections-hs
on master help sign in
Merge pull request #48 from rcook/rcook-to-humanized

Implement humanizeCustom and toHumanized helper functions

commit32e69f85451738b8b319174e97a6b6823fdfd369 authorCristhian Motoche <CristhianMotoche@users.noreply.github.com> dateMon 24 Jul 2017, 07:45 (9 years ago) parentf103a07892e0d5
4 files changed+101−24
Text/Inflections.hs+29−8 Text/Inflections/Humanize.hs+24−3 test/Text/Inflections/HumanizeSpec.hs+40−13 test/Text/InflectionsSpec.hs+8
Text/Inflections.hs+29−8
@@ -96,6 +96,7 @@ module Text.Inflections
9696 , camelizeCustom
9797 , dasherize
9898 , humanize
99+ , humanizeCustom
99100 , underscore
100101 , titleize
101102 , Transliterations
@@ -110,6 +111,7 @@ module Text.Inflections
110111 , toUnderscore
111112 , toDashed
112113 , toCamelCased
114+ , toHumanized
113115 , betterThrow )
114116where
115117
@@ -118,7 +120,7 @@ import Data.Text (Text)
118120import Text.Inflections.Camelize (camelize, camelizeCustom)
119121import Text.Inflections.Dasherize (dasherize)
120122import Text.Inflections.Data (Transliterations, defaultTransliterations)
121−import Text.Inflections.Humanize (humanize)
123+import Text.Inflections.Humanize (humanize, humanizeCustom)
122124import Text.Inflections.Ordinal (ordinal, ordinalize)
123125import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
124126import Text.Inflections.Parse.CamelCase (parseCamelCase)
@@ -152,20 +154,39 @@ toDashed :: Text -> Either (ParseError Char Dec) Text
152154toDashed = fmap dasherize . parseCamelCase []
153155
154156-- | Transforms underscored_text to CamelCasedText. If first argument is
155−-- 'True' then FirstCharacter in result string will be in upper case. If
156−-- '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.
157159--
158−-- > toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
160+-- > toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
159161--
160162-- >>> toCamelCased True "foo_bar_bazz"
161163-- "FooBarBazz"
162164-- >>> toCamelCased False "foo_bar_bazz"
163165-- "fooBarBazz"
164166toCamelCased
165− :: Bool -- ^ Capitalize the first character
166− -> Text -- ^ Input
167− -> Either (ParseError Char Dec) Text -- ^ Ouput
168−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 []
169190
170191-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
171192-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
Text/Inflections/Humanize.hs+24−3
@@ -13,7 +13,8 @@
1313{-# LANGUAGE OverloadedStrings #-}
1414
1515module Text.Inflections.Humanize
16− ( humanize )
16+ ( humanize
17+ , humanizeCustom )
1718where
1819
1920import Data.Text (Text)
@@ -36,7 +37,27 @@ import Control.Applicative
3637humanize
3738 :: [SomeWord] -- ^ List of words, first of which will be capitalized
3839 -> 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' =
4061 case unSomeWord (T.replace "_" " ") <$> xs' of
4162 [] -> ""
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
1212#endif
1313
1414spec :: Spec
15−spec = describe "humazine" $ do
16− it "converts snake case to a human-readable string" $ do
17− employee <- SomeWord <$> mkWord "employee"
18− salary <- SomeWord <$> mkWord "salary"
19− humanize [employee,salary] `shouldBe` "Employee salary"
20− it "turns underscores into spaces" $ do
21− employee <- SomeWord <$> mkWord "employee"
22− has <- SomeWord <$> mkWord "has"
23− salary <- SomeWord <$> mkWord "salary"
24− humanize [employee, has, salary] `shouldBe` "Employee has salary"
25− it "capitalizes the first word of a sentence" $ do
26− underground <- SomeWord <$> mkWord "underground"
27− humanize [underground] `shouldBe` "Underground"
15+spec = do
16+ describe "humanize" $ do
17+ it "converts snake case to a human-readable string" $ do
18+ employee <- SomeWord <$> mkWord "employee"
19+ salary <- SomeWord <$> mkWord "salary"
20+ humanize [employee,salary] `shouldBe` "Employee salary"
21+ it "turns underscores into spaces" $ do
22+ employee <- SomeWord <$> mkWord "employee"
23+ has <- SomeWord <$> mkWord "has"
24+ salary <- SomeWord <$> mkWord "salary"
25+ humanize [employee, has, salary] `shouldBe` "Employee has salary"
26+ it "capitalizes the first word of a sentence" $ do
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"
54+ humanizeCustom True [underground] `shouldBe` "Underground"
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" $