methods.coffee | |
---|---|
inflections = require('../inflect').inflections | |
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to false then camelize produces lowerCamelCase. Examples
As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold: | camelize = (lower_case_and_underscored_word, first_letter_in_uppercase = true) ->
rest = lower_case_and_underscored_word.replace /_./g, (val) -> val[1..-1].toUpperCase()
if first_letter_in_uppercase
lower_case_and_underscored_word[0].toUpperCase() + rest[1..-1]
else
lower_case_and_underscored_word[0].toLowerCase() + rest[1..-1] |
Makes an underscored, lowercase form from the expression in the string. Examples
As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold: | underscore = (camel_cased_word) ->
word = camel_cased_word.toString()
word = word.replace(/([A-Z]+)([A-Z][a-z])/g,'$1_$2')
word = word.replace(/([a-z\d])([A-Z])/g,'$1_$2')
word = word.replace(/-/g, '_')
word = word.toLowerCase()
word |
Replaces underscores with dashes in the string. Examples | dasherize = (underscored_word) ->
underscored_word.replace(/_/g, '-') |
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 | titleize = (word) ->
humanize(underscore(word)).replace /\b('?[a-z])/g, (val) -> capitalize(val) |
Converts the first character to uppercase and the remainder to lowercase. Examples | capitalize = (word) ->
(word[0] || '').toUpperCase() + (word[1..-1] || '').toLowerCase() |
Returns the plural form of the word in the string. Examples | pluralize = (word) ->
result = word.toString()
if word.length == 0 || inflections().uncountables.indexOf(result.toLowerCase()) != -1
result
else
for plural in inflections().plurals
rule = plural[0]
replacement = plural[1]
if result.search(rule) != -1
result = result.replace(rule, replacement)
break
result |
The reverse of pluralize, returns the singular form of a word in a string. Examples | singularize = (word) ->
result = word.toString()
uncountable = false
for inflection in inflections().uncountables
if result.search(new RegExp("\\b#{inflection}$", 'i')) != -1
uncountable = true
break
if word.length == 0 || uncountable
result
else
for singular in inflections().singulars
rule = singular[0]
replacement = singular[1]
if result.search(rule) != -1
result = result.replace(rule, replacement)
break
result |
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 | humanize = (lower_case_and_underscored_word) ->
result = lower_case_and_underscored_word.toString()
for human in inflections().humans
rule = human[0]
replacement = human[1]
if result.search(rule) != -1
result = result.replace(rule, replacement)
break
capitalize(result.replace(/_id$/, '').replace(/_/g, ' ')) |
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th. Examples | ordinalize = (number) ->
number_int = parseInt(number, 10)
if [11..13].indexOf(number_int % 100) != -1
"#{number}th"
else
switch number_int % 10
when 1 then "#{number}st"
when 2 then "#{number}nd"
when 3 then "#{number}rd"
else "#{number}th" |
Replaces special characters in a string so that it may be used as part of a 'pretty' URL. Examples | parameterize = (string, sep = '-') ->
parameterized_string = string.toString() |
Turn unwanted chars into the separator | parameterized_string = parameterized_string.replace(/[^a-z0-9\-_]+/gi, sep)
if sep? |
No more than one of the separator in a row. | parameterized_string = parameterized_string.replace(new RegExp("#{sep}{2,}", 'g'), sep) |
Remove leading/trailing separator. | parameterized_string = parameterized_string.replace(new RegExp("^#{sep}|#{sep}$", 'gi'), '')
parameterized_string.toLowerCase()
exports.camelize = camelize
exports.underscore = underscore
exports.dasherize = dasherize
exports.titleize = titleize
exports.capitalize = capitalize
exports.pluralize = pluralize
exports.singularize = singularize
exports.humanize = humanize
exports.ordinalize = ordinalize
exports.parameterize = parameterize
|