git.iko.soy/typeable/inflections-hs
on master help sign in
Implement humanizeCustom to enable optional capitalization of first word
a902fa0Richard Cook
Text/Inflections.hs+2−1
@@ -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
@@ -118,7 +119,7 @@ import Data.Text (Text)
118import Text.Inflections.Camelize (camelize, camelizeCustom)
119import Text.Inflections.Camelize (camelize, camelizeCustom)
119import Text.Inflections.Dasherize (dasherize)
120import Text.Inflections.Dasherize (dasherize)
120import Text.Inflections.Data (Transliterations, defaultTransliterations)
121import Text.Inflections.Data (Transliterations, defaultTransliterations)
121−import Text.Inflections.Humanize (humanize)
122+import Text.Inflections.Humanize (humanize, humanizeCustom)
122import Text.Inflections.Ordinal (ordinal, ordinalize)
123import Text.Inflections.Ordinal (ordinal, ordinalize)
123import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
124import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
124import Text.Inflections.Parse.CamelCase (parseCamelCase)
125import Text.Inflections.Parse.CamelCase (parseCamelCase)
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"