|
-
Jun 20th, 2011, 06:18 PM
#1
Thread Starter
Stack Overflow moderator
Easy Pluralization
It's always annoying to spend time working with ternary if statements and what not, or even (gasp) using a variable to pluralize things. Here's a snippet that makes it easier.
http://jsfiddle.net/minitech/PefYd/
You use it like so:
Code:
var str = "There {is|are} # apples. The #% apple looks nice to me.".plural(5);
Which gives:
Code:
"There are 5 apples. The 5th apple looks nice to me."
# is replaced with the passed integer. % becomes st, nd, rd, or th. {x} becomes x if the number is plural, nothing otherwise. {x|y} becomes x if the number is singular, y if it is plural. More than 2 results in 1 being turned into the first one, 2 to the second one, 3 to the third one, etc. and all out of range values being turned into the last one.
Enjoy! Suggestions are welcome.
Last edited by minitech; Jun 20th, 2011 at 06:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|