inflections-0.1.0.7: Inflections library for Haskell

Portabilityportable
Stabilityunstable
Maintainerjustin@stackbuilders.com
Safe HaskellSafe-Inferred

Text.Inflections

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

camelizeSource

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.

camelizeCustomSource

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.

dasherizeSource

Arguments

:: [Word]

Input Words to separate with dashes

-> String

The dasherized String

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

humanizeSource

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.

underscoreSource

Arguments

:: [Word]

Input Words to separate with underscores

-> String

The underscored String

Turns a CamelCase string into an underscore_separated String.

titleizeSource

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 StringSource

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 -> StringSource

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 -> StringSource

Transliterate a String with a custom transliteration table.

transliterate :: String -> StringSource

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 -> StringSource

Returns a String after default approximations for changing Unicode characters to a valid ASCII range are applied.

ordinal :: Integer -> StringSource

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 -> StringSource

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 StringSource

A Map containing mappings from international characters to sequences approximating these characters within the ASCII range.