Copyright | (c) Justin Leitgeb |
---|---|
License | MIT |
Maintainer | justin@stackbuilders.com |
Stability | unstable |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
This module provides methods for common String transformations, similar to the Inflections library found in Rails:
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html
While many of the functions in this library are the same as in implementations in Rails' ActiveSupport, the philosophy of this library is fundamentally different. Where Rails tries to be as permissive as possible, and return a String when given any input, this library tries to output strings that make sense according to the function that is called.
When you look closely at many of the functions in Rails' inflections
library, you will notice that many of them are partial. That is, they only
have well-defined output for some of the possible inputs to the function allowed
by the type system. As an example, let's take the underscore
function. In
Rails, it works like this:
>>>
"fooBar".underscore
"foo_bar"
Looks ok so far. However, it's also easy to produce less expected results:
>>>
"foo bar".underscore
"foo bar"
The output isn't underscored - it contains a space! It turns out that some of the functions from Inflections in ActiveSupport are partial. Ie., the outputs are really only specified for a certain range of the inputs allowed by the String type.
In the Haskell inflections library, we aim to deliver more predictable results by separating the parsing of strings into tokens from the application of transformations. Let's see an example.
First, we tokenize an underscored String using parseSnakeCase
:
>>>
parseSnakeCase [] "foo_bar"
Right [Word "foo",Word "bar"]
We can chain together the tokenization of the input String and the
transformation to CamelCase by using LiftM
:
>>>
import Control.Monad (liftM)
>>>
liftM camelize $ parseSnakeCase "foo_bar"
By separating out the tokenization from the application of inflections, we also end up with useful libraries for validating input which can be used independently:
>>>
parseSnakeCase [] "fooBar"
Left "(unknown)" (line 1, column 4): unexpected 'B' expecting lowercase letter, "_" or end of input
This library is still a work-in-progress, and contributions are welcome for missing pieces and to fix bugs. Please see the Github page to contribute with code or bug reports:
- camelize :: [Word] -> String
- camelizeCustom :: Bool -> [Word] -> String
- dasherize :: [Word] -> String
- humanize :: [Word] -> String
- underscore :: [Word] -> String
- titleize :: [Word] -> String
- defaultMap :: Map Char String
- parameterize :: String -> String
- parameterizeCustom :: Transliterations -> String -> String
- transliterate :: String -> String
- transliterateCustom :: String -> Transliterations -> String -> String
- ordinal :: Integer -> String
- ordinalize :: Integer -> String
- parseSnakeCase :: [String] -> String -> Either ParseError [Word]
- parseCamelCase :: [String] -> String -> Either ParseError [Word]
- type Transliterations = Map Char String
- toUnderscore :: String -> String
- toDashed :: String -> String
- toCamelCased :: Bool -> String -> String
Documentation
Turns a an input Word List in into CamelCase. Returns the CamelCase String.
:: Bool | Whether to capitalize the first character in the output String |
-> [Word] | The input Words |
-> String | The camelized String |
Turns an input Word List into a CamelCase String.
Replaces underscores in a snake_cased string with dashes (hyphens).
Capitalizes the first word and turns underscores into spaces. Like titleize, this is meant for creating pretty output.
Turns a CamelCase string into an underscore_separated String.
Capitalizes all the Words in the input List
.
defaultMap :: Map Char String Source
These default transliterations stolen from the Ruby i18n library - see https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/transliterator.rb#L41:L69.
parameterize :: String -> String Source
Replaces special characters in a string so that it may be used as part of a
pretty
URL. Uses the default transliterations in this library
parameterizeCustom :: Transliterations -> String -> String Source
Transliterate a String with a custom transliteration table.
transliterate :: String -> String Source
Returns a String after default approximations for changing Unicode characters to a valid ASCII range are applied. If you want to supplement the default approximations with your own, you should use the transliterateCustom function instead of transliterate.
transliterateCustom :: String -> Transliterations -> String -> String Source
Returns a String after default approximations for changing Unicode characters to a valid ASCII range are applied.
ordinal :: Integer -> String Source
Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
ordinalize :: Integer -> String Source
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
parseSnakeCase :: [String] -> String -> Either ParseError [Word] Source
parseCamelCase :: [String] -> String -> Either ParseError [Word] Source
type Transliterations = Map Char String Source
A Map
containing mappings from international characters to
sequences approximating these characters within the ASCII range.
Often used combinators
toUnderscore :: String -> String Source
Transforms CamelCasedString to snake_cased_string_with_underscores. Throws exception if parsing failed