inflections-0.1.0.10: Inflections library for Haskell

Copyright(c) Justin Leitgeb
LicenseMIT
Maintainerjustin@stackbuilders.com
Stabilityunstable
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Inflections

Contents

Description

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:

https://github.com/stackbuilders/inflections-hs

Synopsis

Documentation

camelize Source

Arguments

:: [Word]

Input Words to separate with underscores

-> String

The camelized String

Turns a an input Word List in into CamelCase. Returns the CamelCase String.

camelizeCustom Source

Arguments

:: 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.

dasherize Source

Arguments

:: [Word]

Input Words to separate with dashes

-> String

The dasherized String

Replaces underscores in a snake_cased string with dashes (hyphens).

humanize Source

Arguments

:: [Word]

List of Words, first of which will be capitalized

-> String

The humanized output

Capitalizes the first word and turns underscores into spaces. Like titleize, this is meant for creating pretty output.

underscore Source

Arguments

:: [Word]

Input Words to separate with underscores

-> String

The underscored String

Turns a CamelCase string into an underscore_separated String.

titleize Source

Arguments

:: [Word]

List of Words, first of which will be capitalized

-> String

The titleized 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.

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

toDashed :: String -> String Source

Transforms CamelCasedString to snake-cased-string-with-dashes. Throws exception if parsing failed.

toCamelCased :: Bool -> String -> String Source

Transforms underscored_text to CamelCasedText. If first argument is True then FirstCharacter in result string will be in upper case. If False then firstCharacter will be in lower case. Throws exception if parsing failed