forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflection.js
74 lines (64 loc) · 1.28 KB
/
inflection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
var utils = require('./utils');
var indexOf = require('./utils/indexOf');
/**
* Expose `helpers`
*/
var helpers = module.exports;
/**
* @name .inflect
* @param {type} `count`
* @param {type} `singular`
* @param {type} `plural`
* @param {type} `include`
* @return {String}
* @api public
*/
helpers.inflect = function(count, singular, plural, include) {
var word = count > 1 || count === 0
? plural
: singular;
if (utils.isUndefined(include) || include === false) {
return word;
} else {
return ''
+ count
+ ' '
+ word;
}
};
/**
* Returns an ordinalized number (as a string).
*
* ```js
* {{ordinalize 1}}
* //=> '1st'
* {{ordinalize 21}}
* //=> '21st'
* {{ordinalize 29}}
* //=> '29th'
* {{ordinalize 22}}
* //=> '22nd'
* ```
*
* @param {String} `val` The value to ordinalize.
* @return {String} The ordinalized number
* @api public
*/
helpers.ordinalize = function(val) {
var num = Math.abs(Math.round(val));
var res;
if (res = num % 100, indexOf.call([11, 12, 13], res) >= 0) {
return '' + val + 'th';
}
switch (num % 10) {
case 1:
return '' + val + 'st';
case 2:
return '' + val + 'nd';
case 3:
return '' + val + 'rd';
default:
return '' + val + 'th';
}
};