forked from tj/lingo
/
inflection.js
172 lines (144 loc) · 3.23 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*!
* Lingo - inflection
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var Language = require('./language');
/**
* Check if a `word` is uncountable.
*
* @param {String} word
* @return {Boolean}
* @api public
*/
Language.prototype.isUncountable = function(word){
return !!this.rules.uncountable[word];
};
/**
* Add an uncountable `word`.
*
* @param {String} word
* @return {Language} for chaining
* @api public
*/
Language.prototype.uncountable = function(word){
this.rules.uncountable[word] = word;
return this;
};
/**
* Add an irreglar `singular` / `plural` map.
*
* @param {String} singular
* @param {String} plural
* @return {Language} for chaining
* @api public
*/
Language.prototype.irregular = function(singular, plural){
this.rules.irregular.plural[singular] = plural;
this.rules.irregular.singular[plural] = singular;
return this;
};
/**
* Add a pluralization `rule` for numbers
*
* @param {RegExp} rule
* @return {Language} for chaining
* @api public
*/
Language.prototype.pluralNumbers = function(rule){
this.rules.pluralNumbers = rule;
return this;
};
/**
* Add a pluralization `rule` with the given `substitution`.
*
* @param {RegExp} rule
* @param {String} substitution
* @return {Language} for chaining
* @api public
*/
Language.prototype.plural = function(rule, substitution){
this.rules.plural.unshift([rule, substitution]);
return this;
};
/**
* Add a singularization `rule` with the given `substitution`.
*
* @param {RegExp} rule
* @param {String} substitution
* @return {Language} for chaining
* @api public
*/
Language.prototype.singular = function(rule, substitution){
this.rules.singular.unshift([rule, substitution]);
return this;
};
/**
* Pluralize the given `word`.
*
* @param {String} word
* @return {String}
* @api public
*/
Language.prototype.pluralize = function(word){
return this.inflect(word, 'plural');
};
/**
* Check if `word` is plural.
*
* @param {String or Number} word
* @return {Boolean}
* @api public
*/
Language.prototype.isPlural = function (word) {
if ('number' == typeof word) {
return (this.rules.pluralNumbers || /.*/).test(word);
} else {
return word == this.pluralize(this.singularize(word));
}
};
/**
* Singularize the given `word`.
*
* @param {String} word
* @return {String}
* @api public
*/
Language.prototype.singularize = function (word) {
return this.inflect(word, 'singular');
};
/**
* Check if `word` is singular.
*
* @param {String or Number} word
* @return {Boolean}
* @api public
*/
Language.prototype.isSingular = function (word) {
return !this.isPlural(word);
};
/**
* Perform `type` inflection rules on the given `word`.
*
* @param {String} word
* @param {String} type
* @return {String}
* @api private
*/
Language.prototype.inflect = function(word, type) {
if (this.isUncountable(word)) return word;
var irregular = this.rules.irregular[type][word];
if (irregular) return irregular;
for (var i = 0, len = this.rules[type].length; i < len; ++i) {
var rule = this.rules[type][i]
, regexp = rule[0]
, sub = rule[1];
if (regexp.test(word)) {
return word.replace(regexp, sub);
}
}
return word;
}