-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluralizer.js
589 lines (583 loc) · 21.1 KB
/
pluralizer.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
pluralizer.js v1.1 - Add-on tool for JavaScript that returns plural form of English words
Created by Ron Royston, https://rack.pub
https://github.com/rhroyston/pluralizer-js
License: MIT
*/
//Revealing Module Pattern (Public & Private) w Public Namespace 'pluralizer'
var pluralizer = (function() {
var pub = {};
var r = 'pluralizer.js error';
var expectedArrayOfArrays = {name:r, message:"Invalid argument. Expected array of arrays"};
var expectedString = {name:r, message:"Invalid argument. Expected string"};
//creates Array.isArray() if it's not natively available
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
//creates .endsWith() if it's not natively available
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
var irregular = [['child','children'],
['die','dice'],
['foot','feet'],
['goose','geese'],
['louse','lice'],
['man','men'],
['mouse','mice'],
['ox','oxen'],
['person','people'],
['that','those'],
['this','these'],
['tooth','teeth'],
['woman','women']];
var xExceptions = [['axis','axes'], ['ox','oxen']];
var fExceptions = [['belief','beliefs'],
['chef','chefs'],
['chief','chiefs'],
['dwarf','dwarfs'],
['grief','griefs'],
['gulf','gulfs'],
['handkerchief','handkerchiefs'],
['kerchief','kerchiefs'],
['mischief','mischiefs'],
['muff','muffs'],
['oaf','oafs'],
['proof','proofs'],
['roof','roofs'],
['safe','safes'],
['turf','turfs']];
var feExceptions = [[' safe','safes']];
var oExceptions = [['albino','albinos'],
['armadillo','armadillos'],
['auto','autos'],
['cameo','cameos'],
['cello','cellos'],
['combo','combos'],
['duo','duos'],
['ego','egos'],
['folio','folios'],
['halo','halos'],
['inferno','infernos'],
['lasso','lassos'],
['memento','mementos'],
['memo','memos'],
['piano','pianos'],
['photo','photos'],
['portfolio','portfolios'],
['pro','pros'],
['silo','silos'],
['solo','solos'],
['stereo','stereos'],
['studio','studios'],
['taco','tacos'],
['tattoo','tattoos'],
['tuxedo','tuxedos'],
['typo','typos'],
['veto','vetoes'],
['video','videos'],
['yo','yos'],
['zoo','zoos']];
var usExceptions = [['abacus','abacuses'],
['crocus','crocuses'],
['genus','genera'],
['octopus','octopuses'],
['rhombus','rhombuses'],
['walrus','walruses']];
var umExceptions = [['album','albums'], ['stadium','stadiums']];
var aExceptions = [['agenda','agendas'],
['alfalfa','alfalfas'],
['aurora','auroras'],
['banana','bananas'],
['barracuda','barracudas'],
['cornea','corneas'],
['nova','novas'],
['phobia','phobias']];
var onExceptions = [['balloon','balloons'], ['carton','cartons']];
var exExceptions = [['annex','annexes'],
['complex','complexes'],
['duplex','duplexes'],
['hex','hexes'],
['index','indices']];
var unchanging = ['advice',
'aircraft',
'bison',
'corn',
'deer',
'equipment',
'evidence',
'fish',
'gold',
'information',
'jewelry',
'kin',
'legislation',
'luck',
'luggage',
'moose',
'music',
'offspring',
'sheep',
'silver',
'swine',
'trousers',
'trout',
'wheat'];
var onlyPlurals = ['barracks',
'bellows',
'cattle',
'congratulations',
'deer',
'dregs',
'eyeglasses',
'gallows',
'headquarters',
'mathematics',
'means',
'measles',
'mumps',
'news',
'oats',
'pants',
'pliers',
'pajamas',
'scissors',
'series',
'shears',
'shorts',
'species',
'tongs',
'tweezers',
'vespers'];
var doc = document;
pub.help = "pluralizer.js returns 2 public methods - read and format. pluralizer.read expects an array of arrays, each with quantity and item name, e.g. pluralizer.read([[2,'orange'],[3,'peach'],[5,'cherry']]) returns string '2 oranges, 3 peaches, and 5 cherries.'. pluralizer.format expects an array with quantity and item name, e.g., pluralizer.format([3,'couch']) returns array '[3, 'couches']'"
pub.read = function (arr) {
if(isArrayOfArrays(arr)){
var count = arr.length;
var str = '';
var temp = [];
switch (count) {
//if arr has 1 item is 1 apple (no and no commas)
case 1:
temp[0] = pluralizer.format(arr[0]);
str = temp[0][0] + ' ' + temp[0][1];
break;
//if arr has 2 items it's 1 apple and 2 oranges (no commas but an and)
case 2:
temp[0] = pluralizer.format(arr[0]);
temp[1] = pluralizer.format(arr[1]);
str = temp[0][0] + ' ' + temp[0][1] + ' and ' + temp[1][0] + ' ' + temp[1][1];
break;
//if arr has 3 items or more it's 1 apple, 2 oranges, and 3 cherries (the last item has an 'and ' put before it)
default:
// for each item in array output format it and concatentate it to a string
var arrayLength = arr.length;
for (var i = 0; i < arrayLength; i++) {
temp = pluralizer.format(arr[i]);
//if this is 2nd last item append with ', and '
if (i === arrayLength - 2){
str += temp[0] + ' ' + temp[1] + ', and ';
}
//if this is last item append with '.'
else if (i === arrayLength - 1){
str += temp[0] + ' ' + temp[1] + '.';
}
else {
str += temp[0] + ' ' + temp[1] + ', ';
}
}
}
return str;
} else {
try {
throw new Error('pluralizer.read had an issue processing argument ');
}
catch (e) {
console.log(e.name + ': ' + e.message);
}
}
};
pub.run = function(str){
if(typeof str === "string"){
//Word ends in s, x, ch, z, or sh
if (str.endsWith('s') || str.endsWith('x') || str.endsWith('ch') || str.endsWith('sh') || str.endsWith('z')){
//look for exceptions first xExceptions
for (var i = 0; i < xExceptions.length; i++) {
if(str === xExceptions[i][0]){
return xExceptions[i][1];
}
}
//str = str.substring(0, str.length - 1);
str = str + 'es';
return str;
}
// Ending in 'y'
else if (str.endsWith('y')){
var s = str.substring(0, str.length - 1);
// preceded by a vowel
if (s.endsWith('a') || s.endsWith('e') || s.endsWith('i') || s.endsWith('o') || s.endsWith('u')){
str = str + 's';
return str;
} else {
//drop the y and add ies
str = s + 'ies';
return str;
}
}
//Ends with 'ff' or 'ffe'
else if (str.endsWith('ff') || str.endsWith('ffe')){
str = str + 's';
return str;
}
//Ends with 'f' (but not 'ff')
else if (str.endsWith('f')){
//look for exceptions first fExceptions
for (var i = 0; i < fExceptions.length; i++) {
if(str === fExceptions[i][0]){
return fExceptions[i][1];
}
}
//Change the 'f' to 'ves'
var s = str.substring(0, str.length - 1);
str = s + 'ves';
return str;
}
//Ends with 'fe' (but not ffe')
else if (str.endsWith('fe')){
//look for exceptions first feExceptions
for (var i = 0; i < feExceptions.length; i++) {
if(str === feExceptions[i][0]){
return feExceptions[i][1];
}
}
//Change the 'fe' to 'ves'
var s = str.substring(0, str.length - 2);
str = s + 'ves';
return str;
}
//Ends with 'o'
else if (str.endsWith('o')){
//look for exceptions first oExceptions
for (var i = 0; i < oExceptions.length; i++) {
if(str === oExceptions[i][0]){
return oExceptions[i][1];
}
}
//Add 'es'
str = s + 'es';
return str;
}
//Ends with 'is'
else if (str.endsWith('is')){
//Change final 'is' to 'es'
var s = str.substring(0, str.length - 2);
str = s + 'es';
return str;
}
//Ends with 'us'
else if (str.endsWith('us')){
//look for exceptions first oExceptions
for (var i = 0; i < usExceptions.length; i++) {
if(str === usExceptions[i][0]){
return usExceptions[i][1];
}
}
//Change final 'us' to 'i'
var s = str.substring(0, str.length - 2);
str = s + 'i';
return str;
}
//Ends with 'um'
else if (str.endsWith('um')){
//look for exceptions first oExceptions
for (var i = 0; i < umExceptions.length; i++) {
if(str === umExceptions[i][0]){
return umExceptions[i][1];
}
}
//Change final 'um' to 'a'
var s = str.substring(0, str.length - 2);
str = s + 'a';
return str;
}
//Ends with 'a' but not 'ia'
else if (str.endsWith('a')){
//not ending is 'ia'
if (str.endsWith('ia')){
str = str + 's';
return str;
}
//look for exceptions first aExceptions
for (var i = 0; i < aExceptions.length; i++) {
if(str === aExceptions[i][0]){
return aExceptions[i][1];
}
}
//Change final 'a' to 'ae'
var s = str.substring(0, str.length - 2);
str = s + 'a';
return str;
}
//Ends with 'on' Change final 'on' to 'a'
else if (str.endsWith('on')){
//look for exceptions first onExceptions
for (var i = 0; i < onExceptions.length; i++) {
if(str === onExceptions[i][0]){
return onExceptions[i][1];
}
}
//Change final 'um' to 'a'
var s = str.substring(0, str.length - 2);
str = s + 'a';
return str;
}
//Ends with 'ex'
else if (str.endsWith('ex')){
//look for exceptions first onExceptions
for (var i = 0; i < exExceptions.length; i++) {
if(str === exExceptions[i][0]){
return exExceptions[i][1];
}
}
//Change final 'ex' to 'ices'
var s = str.substring(0, str.length - 2);
str = s + 'ices';
return str;
}
else {
//check unchanging
for (var i = 0; i < unchanging.length; i++) {
if(str === unchanging[i]){
return str;
}
}
//check onlyPlurals
for (var i = 0; i < onlyPlurals.length; i++) {
if(str === onlyPlurals[i]){
return str;
}
}
//check irregular
for (var i = 0; i < irregular.length; i++) {
if(str === irregular[i][0]){
return irregular[i][1];
}
}
str = str + 's';
return str;
}
} else {
try {
throw new Error('pluralizer.run had an issue processing argument ');
}
catch (e) {
console.log(e.name + ': ' + e.message);
}
}
};
pub.format = function (arr) {
//if qty is greater than 1 we need to add s, es, or ies
var qty = arr[0];
var str = arr[1];
if (qty > 1){
//Word ends in s, x, ch, z, or sh
if (str.endsWith('s') || str.endsWith('x') || str.endsWith('ch') || str.endsWith('sh') || str.endsWith('z')){
//look for exceptions first xExceptions
for (var i = 0; i < xExceptions.length; i++) {
if(str === xExceptions[i][0]){
return [qty,xExceptions[i][1]];
}
}
//str = str.substring(0, str.length - 1);
str = str + 'es';
return [qty,str];
}
// Ending in 'y'
else if (str.endsWith('y')){
var s = str.substring(0, str.length - 1);
// preceded by a vowel
if (s.endsWith('a') || s.endsWith('e') || s.endsWith('i') || s.endsWith('o') || s.endsWith('u')){
str = str + 's';
return [qty,str];
} else {
//drop the y and add ies
str = s + 'ies';
return [qty,str];
}
}
//Ends with 'ff' or 'ffe'
else if (str.endsWith('ff') || str.endsWith('ffe')){
str = str + 's';
return [qty,str];
}
//Ends with 'f' (but not 'ff')
else if (str.endsWith('f')){
//look for exceptions first fExceptions
for (var i = 0; i < fExceptions.length; i++) {
if(str === fExceptions[i][0]){
return [qty,fExceptions[i][1]];
}
}
//Change the 'f' to 'ves'
var s = str.substring(0, str.length - 1);
str = s + 'ves';
return [qty,str];
}
//Ends with 'fe' (but not ffe')
else if (str.endsWith('fe')){
//look for exceptions first feExceptions
for (var i = 0; i < feExceptions.length; i++) {
if(str === feExceptions[i][0]){
return [qty,feExceptions[i][1]];
}
}
//Change the 'fe' to 'ves'
var s = str.substring(0, str.length - 2);
str = s + 'ves';
return [qty,str];
}
//Ends with 'o'
else if (str.endsWith('o')){
//look for exceptions first oExceptions
for (var i = 0; i < oExceptions.length; i++) {
if(str === oExceptions[i][0]){
return [qty,oExceptions[i][1]];
}
}
//Add 'es'
str = s + 'es';
return [qty,str];
}
//Ends with 'is'
else if (str.endsWith('is')){
//Change final 'is' to 'es'
var s = str.substring(0, str.length - 2);
str = s + 'es';
return [qty,str];
}
//Ends with 'us'
else if (str.endsWith('us')){
//look for exceptions first oExceptions
for (var i = 0; i < usExceptions.length; i++) {
if(str === usExceptions[i][0]){
return [qty,usExceptions[i][1]];
}
}
//Change final 'us' to 'i'
var s = str.substring(0, str.length - 2);
str = s + 'i';
return [qty,str];
}
//Ends with 'um'
else if (str.endsWith('um')){
//look for exceptions first oExceptions
for (var i = 0; i < umExceptions.length; i++) {
if(str === umExceptions[i][0]){
return [qty,umExceptions[i][1]];
}
}
//Change final 'um' to 'a'
var s = str.substring(0, str.length - 2);
str = s + 'a';
return [qty,str];
}
//Ends with 'a' but not 'ia'
else if (str.endsWith('a')){
//not ending is 'ia'
if (str.endsWith('ia')){
str = str + 's';
return [qty,str];
}
//look for exceptions first aExceptions
for (var i = 0; i < aExceptions.length; i++) {
if(str === aExceptions[i][0]){
return [qty,aExceptions[i][1]];
}
}
//Change final 'a' to 'ae'
var s = str.substring(0, str.length - 2);
str = s + 'a';
return [qty,str];
}
//Ends with 'on' Change final 'on' to 'a'
else if (str.endsWith('on')){
//look for exceptions first onExceptions
for (var i = 0; i < onExceptions.length; i++) {
if(str === onExceptions[i][0]){
return [qty,onExceptions[i][1]];
}
}
//Change final 'um' to 'a'
var s = str.substring(0, str.length - 2);
str = s + 'a';
return [qty,str];
}
//Ends with 'ex'
else if (str.endsWith('ex')){
//look for exceptions first onExceptions
for (var i = 0; i < exExceptions.length; i++) {
if(str === exExceptions[i][0]){
return [qty,exExceptions[i][1]];
}
}
//Change final 'ex' to 'ices'
var s = str.substring(0, str.length - 2);
str = s + 'ices';
return [qty,str];
}
else {
//check unchanging
for (var i = 0; i < unchanging.length; i++) {
if(str === unchanging[i]){
return [qty,str];
}
}
//check onlyPlurals
for (var i = 0; i < onlyPlurals.length; i++) {
if(str === onlyPlurals[i]){
return [qty,str];
}
}
//check irregular
for (var i = 0; i < irregular.length; i++) {
if(str === irregular[i][0]){
return [qty,irregular[i][1]];
}
}
str = str + 's';
return [qty,str];
}
} else {
return [qty,str];
}
}
function isArrayOfArrays(arr){
if(Array.isArray(arr)){
var result = true;
for (var i = 0; i < arr.length; i++) {
if(!Array.isArray(arr[i])){
result = false;
}
}
if(result){
return true;
} else {
return false;
}
} else {
return false;
}
}
//API
return pub;
}());