API for inflections
by
Roman Scherer
Usage:
(ns your-namespace
(:require inflections))
Overview
Rails-like inflections for Clojure.
Examples:
user> (use 'inflections)
nil
user> (pluralize "word")
"words"
user> (pluralize "virus")
"viri"
user> (singularize "apples")
"apple"
user> (singularize "octopi")
"octopus"
user> (underscore "puni-puni")
"puni_puni"
user> (ordinalize "52")
"52nd"
user> (capitalize "clojure")
"Clojure"
Public Variables and Functions
add-irregular
function
Usage: (add-irregular word)
Adds the given word to *irregular-words*.
camelize
function
Usage: (camelize word)
(camelize word mode)
By default, camelize converts strings to UpperCamelCase. If the
argument to camelize is set to :lower then camelize produces
lowerCamelCase. camelize will also convert "/" to "::" which is
useful for converting paths to namespaces.
Examples: (camelize "active_record") => "ActiveRecord"
(camelize "active_record") => "activeRecord"
(camelize "active_record/errors") => "ActiveRecord::Errors"
(camelize "active_record/errors" :lower) => "activeRecord::Errors"
capitalize
function
Usage: (capitalize word)
Returns a string with the first character of the word converted to
uppercase and the remaining to lowercase.
Examples: (capitalize "hello") => "Hello"
(capitalize "HELLO") => "Hello"
(capitalize "abc123") => "abc123"
dasherize
function
Usage: (dasherize word)
Replaces all underscores in the word with dashes.
Example: (dasherize "puni_puni") => "puni-puni"
delete-irregular
function
Usage: (delete-irregular word)
Deletes the given word from *irregular-words*.
demodulize
function
Usage: (demodulize word)
Removes the module part from the expression in the string.
Examples: (demodulize "inflections.MyRecord") => "MyRecord"
(demodulize "ActiveRecord::CoreExtensions::String::Inflections") => "Inflections"
(demodulize "Inflections") => "Inflections"
foreign-key
function
Usage: (foreign-key word)
(foreign-key word separator)
Creates a foreign key name from a class name. The default separator
"_" is placed between the name and "id".
Examples: (foreign-key "Message") => "message_id"
(foreign-key "Message" false) => "messageid"
(foreign-key "Admin::Post") => "post_id"
hyphenize
function
Usage: (hyphenize word)
Returns a string by threading word, which can be a string or a
symbol, through the functions str, underscore and dasherize.
Examples:
(camelcase->identifier 'Continent)
; => "continent"
(camelcase->identifier "CountryFlag")
; => "country-flag"
irregular!
function
Usage: (irregular! & singulars-and-plurals)
Define words that are irregular in singular and plural.
Examples: (irregular! "child" "children")
(irregular! "cow" "kine"
"louse" "lice")
irregular?
function
Usage: (irregular? word)
Returns true if the given word is irregular, else false.
Examples: (irregular? "child") => true
(irregular? "word") => false
normalize
function
Usage: (normalize arg)
Normalize a word.
ordinalize
function
Usage: (ordinalize number)
Turns a number into an ordinal string used to denote the position
in an ordered sequence such as 1st, 2nd, 3rd, 4th, etc.
Examples: (ordinalize "1") => "1st"
(ordinalize "23") => "23rd"
parameterize
function
Usage: (parameterize string & [separator])
Replaces special characters in a string so that it may be used as
part of a pretty URL.
Examples:
(parameterize "Donald E. Knuth")
; => "donald-e-knuth"
(parameterize "Donald E. Knuth" "_")
; => "donald_e_knuth"
plural!
function
Usage: (plural! & patterns-and-replacements)
Define rule(s) to map words from singular to plural.
Examples: (plural! #"$(?i)" "s")
(plural! #"(ax|test)is$(?i)" "$1es"
#"(octop|vir)us$(?i)" "$1i")
pluralize
function
Usage: (pluralize word)
Returns the plural of the given word.
Example: (pluralize "virus") => "virii"
reset-irregular-words!
function
Usage: (reset-irregular-words!)
Resets the irregular words.
reset-plural-rules!
function
Usage: (reset-plural-rules!)
Resets the rules used to map from singular to plural.
reset-singular-rules!
function
Usage: (reset-singular-rules!)
Resets the rules used to map from plural to singular.
reset-uncountable-words!
function
Usage: (reset-uncountable-words!)
Resets the uncountable words.
singular!
function
Usage: (singular! & patterns-and-replacements)
Define rule(s) to map words from singular to plural.
Examples: (singular! #"(n)ews$(?i)" "$1ews")
(singular! #"(m)ovies$(?i)" "$1ovie"
#"([m|l])ice$(?i)" "$1ouse")
singularize
function
Usage: (singularize word)
Returns the singular of the given word.
Example: (singularize "mice") => "mouse"
uncountable!
function
Usage: (uncountable! & words)
Adds the given word(s) to the list of uncountable words.
Examples: (uncountable! "air")
(uncountable! "alcohol" "blood" "butter")
uncountable?
function
Usage: (uncountable? word)
Returns true if the given word is uncountable, else false.
Examples: (uncountable? "alcohol") => true
(uncountable? "word") => false
underscore
function
Usage: (underscore word)
The reverse of camelize. Makes an underscored, lowercase form from
the expression in the string. Changes "::" to "/" to convert
namespaces to paths.
Examples: (underscore "ActiveRecord") => "active_record"
(underscore "ActiveRecord::Errors") => "active_record/errors"
inflections.irregular
add-irregular
function
Usage: (add-irregular word)
Adds the given word to *irregular-words*.
delete-irregular
function
Usage: (delete-irregular word)
Deletes the given word from *irregular-words*.
irregular!
function
Usage: (irregular! & singulars-and-plurals)
Define words that are irregular in singular and plural.
Examples: (irregular! "child" "children")
(irregular! "cow" "kine"
"louse" "lice")
irregular?
function
Usage: (irregular? word)
Returns true if the given word is irregular, else false.
Examples: (irregular? "child") => true
(irregular? "word") => false
reset-irregular-words!
function
Usage: (reset-irregular-words!)
Resets the irregular words.
inflections.plural
plural!
function
Usage: (plural! & patterns-and-replacements)
Define rule(s) to map words from singular to plural.
Examples: (plural! #"$(?i)" "s")
(plural! #"(ax|test)is$(?i)" "$1es"
#"(octop|vir)us$(?i)" "$1i")
pluralize
function
Usage: (pluralize word)
Returns the plural of the given word.
Example: (pluralize "virus") => "virii"
reset-plural-rules!
function
Usage: (reset-plural-rules!)
Resets the rules used to map from singular to plural.
inflections.rules
reset-rules!
function
Usage: (reset-rules! rules)
Resets the list of plural rules.
slurp-rules
function
Usage: (slurp-rules & patterns-and-replacements)
Returns a seq of rules, where the pattern and replacement must be
given in pairs of two elements.
inflections.singular
reset-singular-rules!
function
Usage: (reset-singular-rules!)
Resets the rules used to map from plural to singular.
singular!
function
Usage: (singular! & patterns-and-replacements)
Define rule(s) to map words from singular to plural.
Examples: (singular! #"(n)ews$(?i)" "$1ews")
(singular! #"(m)ovies$(?i)" "$1ovie"
#"([m|l])ice$(?i)" "$1ouse")
singularize
function
Usage: (singularize word)
Returns the singular of the given word.
Example: (singularize "mice") => "mouse"
inflections.transform
function
Usage: (camelize word)
(camelize word mode)
By default, camelize converts strings to UpperCamelCase. If the
argument to camelize is set to :lower then camelize produces
lowerCamelCase. camelize will also convert "/" to "::" which is
useful for converting paths to namespaces.
Examples: (camelize "active_record") => "ActiveRecord"
(camelize "active_record") => "activeRecord"
(camelize "active_record/errors") => "ActiveRecord::Errors"
(camelize "active_record/errors" :lower) => "activeRecord::Errors"
function
Usage: (capitalize word)
Returns a string with the first character of the word converted to
uppercase and the remaining to lowercase.
Examples: (capitalize "hello") => "Hello"
(capitalize "HELLO") => "Hello"
(capitalize "abc123") => "abc123"
function
Usage: (dasherize word)
Replaces all underscores in the word with dashes.
Example: (dasherize "puni_puni") => "puni-puni"
function
Usage: (demodulize word)
Removes the module part from the expression in the string.
Examples: (demodulize "inflections.MyRecord") => "MyRecord"
(demodulize "ActiveRecord::CoreExtensions::String::Inflections") => "Inflections"
(demodulize "Inflections") => "Inflections"
function
Usage: (foreign-key word)
(foreign-key word separator)
Creates a foreign key name from a class name. The default separator
"_" is placed between the name and "id".
Examples: (foreign-key "Message") => "message_id"
(foreign-key "Message" false) => "messageid"
(foreign-key "Admin::Post") => "post_id"
function
Usage: (hyphenize word)
Returns a string by threading word, which can be a string or a
symbol, through the functions str, underscore and dasherize.
Examples:
(camelcase->identifier 'Continent)
; => "continent"
(camelcase->identifier "CountryFlag")
; => "country-flag"
function
Usage: (normalize arg)
Normalize a word.
function
Usage: (ordinalize number)
Turns a number into an ordinal string used to denote the position
in an ordered sequence such as 1st, 2nd, 3rd, 4th, etc.
Examples: (ordinalize "1") => "1st"
(ordinalize "23") => "23rd"
function
Usage: (parameterize string & [separator])
Replaces special characters in a string so that it may be used as
part of a pretty URL.
Examples:
(parameterize "Donald E. Knuth")
; => "donald-e-knuth"
(parameterize "Donald E. Knuth" "_")
; => "donald_e_knuth"
function
Usage: (underscore word)
The reverse of camelize. Makes an underscored, lowercase form from
the expression in the string. Changes "::" to "/" to convert
namespaces to paths.
Examples: (underscore "ActiveRecord") => "active_record"
(underscore "ActiveRecord::Errors") => "active_record/errors"
inflections.uncountable
reset-uncountable-words!
function
Usage: (reset-uncountable-words!)
Resets the uncountable words.
uncountable!
function
Usage: (uncountable! & words)
Adds the given word(s) to the list of uncountable words.
Examples: (uncountable! "air")
(uncountable! "alcohol" "blood" "butter")
uncountable?
function
Usage: (uncountable? word)
Returns true if the given word is uncountable, else false.
Examples: (uncountable? "alcohol") => true
(uncountable? "word") => false