Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //dependencies: jQuery and string.js (http://stringjs.com/)
- //usage: {{pluralize num 'word'}}
- Handlebars.registerHelper('pluralize', function(number, str) {
- //doesn't handle weird words
- var lastLetter = str[str.length - 1]
- , strObj = S(str)
- , vowels = ['a', 'e','i','o','u']
- , root = str
- , suffix = 's'
- ;
- if (+number !== 1) {
- //possible weird cases
- if (strObj.endsWith('man')) {
- return strObj.chompRight('man').s + 'men';
- }
- if (str === 'person') {
- return 'people';
- }
- //rule-based cases
- switch (lastLetter) {
- case 'o':
- case 'x':
- case 'z': //intentional fallthrough
- suffix = 'es';
- break;
- case 'e':
- if (strObj.endsWith('fe')) {
- root = strObj.chompRight('fe').s;
- suffix = 'ves';
- }
- break;
- case 'f':
- if (strObj.endsWith('arf') || strObj.endsWith('lf') || strObj.endsWith('af')) {
- root = strObj.chompRight('f').s;
- suffix = 'ves';
- }
- break;
- case 'h':
- if (strObj.endsWith('sh') || strObj.endsWith('ch')) {
- suffix = 'es';
- }
- break;
- case 's':
- if (!strObj.endsWith('ies')) {
- suffix = 'es';
- }
- break;
- case 'y':
- //ends in [consonant]y
- if ($.inArray(str[str.length - 2], vowels) === -1 || strObj.endsWith('quy')) {
- root = strObj.chompRight('y').s;
- suffix = 'ies';
- }
- break;
- }
- return root + suffix;
- }
- return str;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement