-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
inflection.js
78 lines (71 loc) · 1.64 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
75
76
77
78
'use strict';
var util = require('handlebars-utils');
var helpers = module.exports;
/**
* Returns either the `singular` or `plural` inflection of a word based on
* the given `count`.
*
* ```handlebars
* {{inflect 0 "string" "strings"}}
* <!-- "strings" -->
* {{inflect 1 "string" "strings"}}
* <!-- "string" -->
* {{inflect 1 "string" "strings" true}}
* <!-- "1 string" -->
* {{inflect 2 "string" "strings"}}
* <!-- "strings" -->
* {{inflect 2 "string" "strings" true}}
* <!-- "2 strings" -->
* ```
* @param {Number} `count`
* @param {String} `singular` The singular form
* @param {String} `plural` The plural form
* @param {String} `includeCount`
* @return {String}
* @api public
*/
helpers.inflect = function(count, singular, plural, includeCount) {
var word = (count > 1 || count === 0) ? plural : singular;
if (includeCount === true) {
return String(count) + ' ' + word;
} else {
return word;
}
};
/**
* Returns an ordinalized number as a string.
*
* ```handlebars
* {{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 str = String(val);
var res = num % 100;
if (util.indexOf([11, 12, 13], res) >= 0) {
return str + 'th';
}
switch (num % 10) {
case 1:
return str + 'st';
case 2:
return str + 'nd';
case 3:
return str + 'rd';
default: {
return str + 'th';
}
}
};