@@ -1,6 +1,6 @@
2-- Module : Text.Inflections
2-- Module : Text.Inflections
3−-- Description : Rails-like inflections library for common String transformations.
3+-- Description : Rails-like inflections library for common Text transformations.
4-- Copyright : (c) Justin Leitgeb
4-- Copyright : (c) Justin Leitgeb
5-- License : MIT
5-- License : MIT
@@ -8,7 +8,7 @@
8-- Stability : unstable
8-- Stability : unstable
9-- Portability : portable
9-- Portability : portable
11−-- This module provides methods for common String transformations, similar
11+-- This module provides methods for common 'Text' transformations, similar
12-- to the Inflections library found in Rails:
12-- to the Inflections library found in Rails:
14-- <http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html>
14-- <http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html>
@@ -17,7 +17,7 @@
17-- implementations in Rails' ActiveSupport, the philosophy of this library
17-- implementations in Rails' ActiveSupport, the philosophy of this library
18-- is fundamentally different. Where Rails tries to be as permissive as
18-- is fundamentally different. Where Rails tries to be as permissive as
19-- possible, and return a String when given any input, this library tries to
19-- possible, and return a String when given any input, this library tries to
20−-- output strings that make sense according to the function that is called.
20+-- output 'Text' that makes sense according to the function that is called.
22-- When you look closely at many of the functions in Rails' inflections
22-- When you look closely at many of the functions in Rails' inflections
23-- library, you will notice that many of them are partial. That is, they
23-- library, you will notice that many of them are partial. That is, they
@@ -33,7 +33,7 @@
33-- >>> "foo bar".underscore
33-- >>> "foo bar".underscore
34-- "foo bar"
34-- "foo bar"
36−-- The output isn't underscored - it contains a space! It turns out that
36+-- The output isn't underscored — it contains a space! It turns out that
37-- some of the functions from Inflections in ActiveSupport are /partial/.
37-- some of the functions from Inflections in ActiveSupport are /partial/.
38-- I.e., the outputs are really only specified for a certain range of the
38-- I.e., the outputs are really only specified for a certain range of the
39-- inputs allowed by the String type.
39-- inputs allowed by the String type.
@@ -42,7 +42,7 @@
42-- results by separating the parsing of strings into tokens from the
42-- results by separating the parsing of strings into tokens from the
43-- application of transformations. Let's see an example.
43-- application of transformations. Let's see an example.
45−-- First, we tokenize an underscored String using 'parseSnakeCase':
45+-- First, we tokenize an underscored 'Text' using 'parseSnakeCase':
47-- >>> parseSnakeCase [] "foo_bar"
47-- >>> parseSnakeCase [] "foo_bar"
48-- Right [Word "foo",Word "bar"]
48-- Right [Word "foo",Word "bar"]
@@ -79,14 +79,24 @@
79{-# LANGUAGE CPP #-}
79{-# LANGUAGE CPP #-}
81module Text.Inflections
81module Text.Inflections
82+ ( -- * Types and helpers
90+ , InflectionException (..)
82− ( -- * Parsing
91+ -- * Parsing
83− parseSnakeCase
92+ , parseSnakeCase
84 , parseCamelCase
93 , parseCamelCase
85 -- * Rendering
94 -- * Rendering
86 , camelize
95 , camelize
87 , camelizeCustom
96 , camelizeCustom
88 , dasherize
97 , dasherize
89 , humanize
98 , humanize
90 , underscore
100 , underscore
91 , titleize
101 , titleize
92 , Transliterations
102 , Transliterations
@@ -97,28 +107,21 @@ module Text.Inflections
97 , transliterateCustom
107 , transliterateCustom
98 , ordinalize
108 , ordinalize
99 , ordinal
109 , ordinal
100− -- * Types and helpers
108− , InflectionException (..)
109 -- * Often used combinators
110 -- * Often used combinators
110 , toUnderscore
111 , toUnderscore
111 , toDashed
112 , toDashed
112 , toCamelCased
113 , toCamelCased
113 , betterThrow )
115 , betterThrow )
116import Control.Monad.Catch (MonadThrow (..))
118import Control.Monad.Catch (MonadThrow (..))
117import Data.Text (Text)
119import Data.Text (Text)
120+import Data.Void (Void)
118import Text.Inflections.Camelize (camelize, camelizeCustom)
121import Text.Inflections.Camelize (camelize, camelizeCustom)
119import Text.Inflections.Dasherize (dasherize)
122import Text.Inflections.Dasherize (dasherize)
120import Text.Inflections.Data (Transliterations, defaultTransliterations)
123import Text.Inflections.Data (Transliterations, defaultTransliterations)
121−import Text.Inflections.Humanize (humanize)
124+import Text.Inflections.Humanize (humanize, humanizeCustom)
122import Text.Inflections.Ordinal (ordinal, ordinalize)
125import Text.Inflections.Ordinal (ordinal, ordinalize)
123import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
126import Text.Inflections.Parameterize (parameterize, parameterizeCustom)
124import Text.Inflections.Parse.CamelCase (parseCamelCase)
127import Text.Inflections.Parse.CamelCase (parseCamelCase)
@@ -139,7 +142,7 @@ import Prelude hiding (Word)
140-- >>> toUnderscore "FooBarBazz"
143-- >>> toUnderscore "FooBarBazz"
141-- "foo_bar_bazz"
144-- "foo_bar_bazz"
142−toUnderscore :: Text -> Either (ParseError Char Dec) Text
145+toUnderscore :: Text -> Either (ParseErrorBundle Text Void) Text
143toUnderscore = fmap underscore . parseCamelCase []
146toUnderscore = fmap underscore . parseCamelCase []
145-- | Transforms CamelCasedString to snake-cased-string-with-dashes.
148-- | Transforms CamelCasedString to snake-cased-string-with-dashes.
@@ -148,26 +151,45 @@ toUnderscore = fmap underscore . parseCamelCase []
149-- >>> toDashed "FooBarBazz"
152-- >>> toDashed "FooBarBazz"
150-- "foo-bar-bazz"
153-- "foo-bar-bazz"
151−toDashed :: Text -> Either (ParseError Char Dec) Text
154+toDashed :: Text -> Either (ParseErrorBundle Text Void) Text
152toDashed = fmap dasherize . parseCamelCase []
155toDashed = fmap dasherize . parseCamelCase []
154-- | Transforms underscored_text to CamelCasedText. If first argument is
157-- | Transforms underscored_text to CamelCasedText. If first argument is
155−-- 'True' then FirstCharacter in result string will be in upper case. If
158+-- 'True' then the first character in the result string will be in upper case. If
156−-- 'False' then firstCharacter will be in lower case.
159+-- 'False' then the first character will be in lower case.
158−-- > toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
161+-- > toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
160-- >>> toCamelCased True "foo_bar_bazz"
163-- >>> toCamelCased True "foo_bar_bazz"
161-- "FooBarBazz"
164-- "FooBarBazz"
162-- >>> toCamelCased False "foo_bar_bazz"
165-- >>> toCamelCased False "foo_bar_bazz"
163-- "fooBarBazz"
166-- "fooBarBazz"
164toCamelCased
167toCamelCased
165− :: Bool -- ^ Capitalize the first character
168+ :: Bool -- ^ Capitalize the first character
166− -> Text -- ^ Input
169+ -> Text -- ^ Input
167− -> Either (ParseError Char Dec) Text -- ^ Ouput
170+ -> Either (ParseErrorBundle Text Void) Text -- ^ Output
168−toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
171+toCamelCased c = fmap (camelizeCustom c) . parseSnakeCase []
173+-- | Transforms underscored_text to space-separated human-readable text.
174+-- If first argument is 'True' then the first character in the result
175+-- string will be in upper case. If 'False' then the first character will be
178+-- > toHumanized c = fmap (humanizeCustom c) . parseSnakeCase []
180+-- >>> toHumanized True "foo_bar_bazz"
182+-- >>> toHumanized False "foo_bar_bazz"
187+ :: Bool -- ^ Capitalize the first character
189+ -> Either (ParseErrorBundle Text Void) Text -- ^ Output
190+toHumanized c = fmap (humanizeCustom c) . parseSnakeCase []
170−-- | Lift something of type @'Either' ('ParseError' 'Char' 'Dec') a@ to
192+-- | Lift something of type @'Either' ('ParseError' 'Char' 'Void') a@ to
171-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
193-- an instance of 'MonadThrow'. Useful when you want to shortcut on parsing
172-- failures and you're in an instance of 'MonadThrow'.
194-- failures and you're in an instance of 'MonadThrow'.
@@ -175,6 +197,6 @@ toCamelCased t = fmap (camelizeCustom t) . parseSnakeCase []
176-- /since 0.3.0.0/
198-- /since 0.3.0.0/
178−betterThrow :: MonadThrow m => Either (ParseError Char Dec) a -> m a
200+betterThrow :: MonadThrow m => Either (ParseErrorBundle Text Void) a -> m a
179betterThrow (Left err) = throwM (InflectionParsingFailed err)
201betterThrow (Left err) = throwM (InflectionParsingFailed err)
180betterThrow (Right x) = return x
202betterThrow (Right x) = return x