Some experiments and thoughts on all things from Mitchell Simoens

Pluralize text

2 min read

One of my gripes is when I see text that should be singular but shows plural when the value before it is one. It's 2015, surely we can detect when we should show singular there. Now admittedly, I've missed these opportunities but I do try to show the appropriate form of a word. So if you see 1 items wrong! Should be 1 item and 3 items. I mean all that has to be done is a simple ternary expression:

Done! So what does this have to do with Ext JS? Ext JS makes this a bit easier and has a function that you can call. That code there can be replaced with:

All the plural method does is adds the 's' onto the term you pass but also prepends the value you pass in. The plural method doesn't do too much magic in determining whether or not to add the 's' so for words that need to be changed more than just adding the 's' then you can pass the plural version as the third argument:

Ok, so this isn't really getting me much except a friendly API. Where I really love using this is in an XTemplate. Code like this:

can be replaced with this:

Super clean! In a grouped grid, I use plural in the groupHeaderTpl and love how clean it is. Just for completeness, you can still pass the plural version too:

Written by Mitchell Simoens who is a long time nerd developing software and building computers and gadgets. Anything expressed on this website are Mitchell Simoens's alone and do not represent his employer.
© Copyright 2023 Mitchell Simoens Site