Skip to content

Instantly share code, notes, and snippets.

@jparker
Last active February 26, 2018 08:03
Show Gist options
  • Save jparker/6dd145e91a11547b0f67 to your computer and use it in GitHub Desktop.
Save jparker/6dd145e91a11547b0f67 to your computer and use it in GitHub Desktop.
coffeescript implementation of extending Number with an ordinalize() method
Number::ordinal = ->
return 'th' if 11 <= this % 100 <= 13
switch this % 10
when 1 then 'st'
when 2 then 'nd'
when 3 then 'rd'
else 'th'
Number::ordinalize = ->
this + this.ordinal()
1.ordinalize() # => '1st'
2.ordinalize() # => '2nd'
3.ordinalize() # => '3rd'
4.ordinalize() # => '4th'
11.ordinalize() # => '11th'
12.ordinalize() # => '12th'
13.ordinalize() # => '13th'
21.ordinalize() # => '21st'
22.ordinalize() # => '22nd'
23.ordinalize() # => '23rd'
100.ordinalize() # => '100th'
101.ordinalize() # => '101st'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment