forked from shmuelie/Humanizer.Js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inflectorExtensions.ts
319 lines (277 loc) · 9.22 KB
/
inflectorExtensions.ts
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
interface String
{
pluralize(plurality: Humanizer.Plurality): string;
pluralize(isKnownToBeSingular?: boolean): string;
singularize(plurality: Humanizer.Plurality): string;
singularize(isKnownToBePlural?: boolean): string;
titleize(): string;
pascalize(): string;
camelize(): string;
underscore(): string;
dasherize(): string;
hyphenate(): string;
}
module Humanizer
{
"use strict";
/**
* Provides hint for Humanizer as to whether a word is singular, plural or with unknown plurality
* @enum
* @readonly
*/
export enum Plurality
{
/**
* The word is singular
*/
Singular,
/**
* The word is plural
*/
Plural,
/**
* I am unsure of the plurality
*/
CouldBeEither
}
class InflectorExtensionsRule
{
private regex: RegExp;
private replacement: string;
constructor(pattern: string, replacement: string)
{
this.regex = new RegExp(pattern, "i");
this.replacement = replacement;
}
apply(word: string): string
{
if (!this.regex.test(word))
{
return null;
}
return word.replace(this.regex, this.replacement);
}
}
var plurals: InflectorExtensionsRule[] = [];
var singulars: InflectorExtensionsRule[] = [];
var uncountables: string[] = [];
function addUncountable(word: string): void
{
uncountables.push(word);
}
function addSingular(rule: string, replacement: string): void
{
singulars.push(new InflectorExtensionsRule(rule, replacement));
}
function addPlural(rule: string, replacement: string): void
{
plurals.push(new InflectorExtensionsRule(rule, replacement));
}
function addIrregluar(singular: string, plural: string): void
{
addPlural("(" + singular.charAt(0) + ")" + singular.substr(1) + "$", "\\1" + plural.substr(1));
addSingular("(" + plural.charAt(0) + ")" + plural.substr(1) + "$", "\\1" + singular.substr(1));
}
addPlural("$", "s");
addPlural("s$", "s");
addPlural("(ax|test)is$", "\\1es");
addPlural("(octop|vir|alumn|fung)us$", "\\1i");
addPlural("(alias|status)$", "\\1es");
addPlural("(bu)s$", "\\1ses");
addPlural("(buffal|tomat|volcan)o$", "\\1oes");
addPlural("([ti])um$", "\\1a");
addPlural("sis$", "ses");
addPlural("(?:([^f])fe|([lr])f)$", "\\1\\2ves");
addPlural("(hive)$", "\\1s");
addPlural("([^aeiouy]|qu)y$", "\\1ies");
addPlural("(x|ch|ss|sh)$", "\\1es");
addPlural("(matr|vert|ind)ix|ex$", "\\1ices");
addPlural("([m|l])ouse$", "\\1ice");
addPlural("^(ox)$", "\\1en");
addPlural("(quiz)$", "\\1zes");
addPlural("(campus)$", "$1es");
addPlural("^is$", "are");
addSingular("s$", "");
addSingular("(n)ews$", "\\1ews");
addSingular("([ti])a$", "\\1um");
addSingular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "\\1\\2sis");
addSingular("(^analy)ses$", "\\1sis");
addSingular("([^f])ves$", "\\1fe");
addSingular("(hive)s$", "\\1");
addSingular("(tive)s$", "\\1");
addSingular("([lr])ves$", "\\1f");
addSingular("([^aeiouy]|qu)ies$", "\\1y");
addSingular("(s)eries$", "\\1eries");
addSingular("(m)ovies$", "\\1ovie");
addSingular("(x|ch|ss|sh)es$", "\\1");
addSingular("([m|l])ice$", "\\1ouse");
addSingular("(bus)es$", "\\1");
addSingular("(o)es$", "\\1");
addSingular("(shoe)s$", "\\1");
addSingular("(cris|ax|test)es$", "\\1is");
addSingular("(octop|vir|alumn|fung)i$", "\\1us");
addSingular("(alias|status)es$", "\\1");
addSingular("^(ox)en", "\\1");
addSingular("(vert|ind)ices$", "\\1ex");
addSingular("(matr)ices$", "\\1ix");
addSingular("(quiz)zes$", "\\1");
addSingular("(campus)es$", "$1");
addSingular("^are$", "is");
addIrregluar("person", "people");
addIrregluar("man", "men");
addIrregluar("child", "children");
addIrregluar("sex", "sexes");
addIrregluar("move", "moves");
addIrregluar("goose", "geese");
addIrregluar("alumna", "alumnae");
addUncountable("equipment");
addUncountable("information");
addUncountable("rice");
addUncountable("money");
addUncountable("species");
addUncountable("series");
addUncountable("fish");
addUncountable("sheep");
addUncountable("deer");
addUncountable("aircraft");
function applyRules(rules: Array<InflectorExtensionsRule>, word: string): string
{
if (word === null)
{
return null;
}
var result: string = word;
if (uncountables.indexOf(word.toLowerCase()) === -1)
{
for (var i: number = rules.length - 1; i >= 0; i--)
{
result = rules[i].apply(word);
if (result !== null)
{
break;
}
}
}
return result;
}
class Inflector
{
pluralize(plurality: Humanizer.Plurality): string
pluralize(isKnownToBeSingular: boolean): string
pluralize(x: any = true): string
{
var $this: string = <string>this;
if (x === Humanizer.Plurality.Plural)
{
return $this;
}
var result: string = applyRules(plurals, $this);
if (x === Humanizer.Plurality.Singular || x === true)
{
return result;
}
var asSingular: string = applyRules(singulars, $this);
var asSingularAsPlural: string = applyRules(plurals, asSingular);
if ((asSingular !== null) && (asSingular !== $this) && (asSingular + "s" !== $this) && (asSingularAsPlural !== $this) && (result !== $this))
{
return $this;
}
return result;
}
singularize(plurality: Humanizer.Plurality): string
singularize(isKnownToBePlural: boolean): string
singularize(x: any = true): string
{
var $this: string = <string>this;
if (x === Humanizer.Plurality.Singular)
{
return $this;
}
var result: string = applyRules(singulars, $this);
if (x === Humanizer.Plurality.Plural || x === true)
{
return result;
}
// the Plurality is unknown so we should check all possibilities
var asPlural: string = applyRules(plurals, $this);
var asPluralAsSingular: string = applyRules(singulars, asPlural);
if ((asPlural !== $this) && ($this + "s" !== asPlural) && (asPluralAsSingular === $this) && (result !== $this))
{
return $this;
}
return result || $this;
}
}
String.prototype.pluralize = Inflector.prototype.pluralize;
String.prototype.singularize = Inflector.prototype.singularize;
/**
* Humanizes the input with Title casing
*/
String.prototype.titleize = function (): string
{
/// <summary>
/// Humanizes the input with Title casing
/// </summary>
return this.humanize(Humanizer.LetterCasing.Title);
};
/**
* By default, pascalize converts strings to UpperCamelCase also removing underscores
*/
String.prototype.pascalize = function (): string
{
/// <summary>
/// By default, pascalize converts strings to UpperCamelCase also removing underscores
/// </summary>
var words: Array<string> = this.split("_");
var result: Array<string> = [];
var length: number = words.length;
for (var i: number = 0; i < length; i++)
{
var word: string = words[i];
result.push(word.charAt(0).toUpperCase());
result.push(word.substr(1));
}
return result.join("");
};
/**
* Same as Pascalize except that the first character is lower case
*/
String.prototype.camelize = function (): string
{
/// <summary>
/// Same as Pascalize except that the first character is lower case
/// </summary>
var word:string = this.pascalize();
return word.charAt(0).toLowerCase() + word.substr(1);
};
/**
* Separates the input words with underscore
*/
String.prototype.underscore = function (): string
{
/// <summary>
/// Separates the input words with underscore
/// </summary>
return this.replace(/([A-Z]+)([A-Z][a-z])/, "$1_$2").replace(/([a-z\d])([A-Z])/, "$1_$2").replace(/[-\s]/, "_").toLowerCase();
};
/**
* Replaces underscores with dashes in the string
*/
String.prototype.dasherize = function (): string
{
/// <summary>
/// Replaces underscores with dashes in the string
/// </summary>
return this.replace("_", "-");
};
/**
* Replaces underscores with hyphens in the string
*/
String.prototype.hyphenate = function (): string
{
/// <summary>
/// Replaces underscores with hyphens in the string
/// </summary>
return this.dasherize();
};
}