Jump To …

string_extensions.coffee

inflect = require('../inflect')

enableStringExtensions = ->

Returns the plural form of the word in the string.

Examples

"post".pluralize()             # => "posts"
"octopus".pluralize()          # => "octopi"
"sheep".pluralize()            # => "sheep"
"words".pluralize()            # => "words"
"CamelOctopus".pluralize()     # => "CamelOctopi"
    String::pluralize = ->
        inflect.pluralize(this)

The reverse of pluralize, returns the singular form of a word in a string.

Examples

"posts".singularize()            # => "post"
"octopi".singularize()           # => "octopus"
"sheep".singularize()            # => "sheep"
"word".singularize()             # => "word"
"CamelOctopi".singularize()      # => "CamelOctopus"
    String::singularize = ->
        inflect.singularize(this)

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to false then camelize produces lowerCamelCase.

Examples

"active_record".camelize()              # => "ActiveRecord"
"active_record".camelize(false)         # => "activeRecord"

As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold:

"SSLError".underscore().camelize() # => "SslError"
    String::camelize = (first_letter_in_uppercase = true) ->
        inflect.camelize(this, first_letter_in_uppercase)

Converts the first character to uppercase and the remainder to lowercase.

Examples

'über'.capitalize() # => "Über"
    String::capitalize = ->
        inflect.capitalize(this)

Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize is meant for creating pretty output.

Examples

"man from the boondocks".titleize() # => "Man From The Boondocks"
"x-men: the last stand".titleize()  # => "X Men: The Last Stand"
    String::titleize = ->
        inflect.titleize(this)

Makes an underscored, lowercase form from the expression in the string.

Examples

"ActiveRecord".underscore()         # => "active_record"

As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold:

"SSLError".underscore().camelize() # => "SslError"
    String::underscore = ->
        inflect.underscore(this)

Replaces underscores with dashes in the string.

Examples

"puni_puni" # => "puni-puni"
    String::dasherize = ->
        inflect.dasherize(this)

Replaces special characters in a string so that it may be used as part of a 'pretty' URL.

Examples

"Donald E. Knuth".parameterize()         # => "donald-e-knuth"
"Donald E. Knuth".parameterize('_')      # => "donald_e_knuth"
    String::parameterize = (sep = '-') ->
        inflect.parameterize(this, sep)

Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", if any. Like titleize, this is meant for creating pretty output.

Examples

"employee_salary" # => "Employee salary"
"author_id"       # => "Author"
    String::humanize = ->
        inflect.humanize(this)

exports.enableStringExtensions = enableStringExtensions