-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (91 loc) · 3.4 KB
/
index.js
File metadata and controls
108 lines (91 loc) · 3.4 KB
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
function createInflector() {
let plurals = [];
let singulars = [];
let irregulars = {};
let uncountables = [];
function plural(rule, replacement) {
plurals.unshift([new RegExp(rule, 'i'), replacement]);
}
function singular(rule, replacement) {
singulars.unshift([new RegExp(rule, 'i'), replacement]);
}
function irregular(singular, plural) {
irregulars[singular.toLowerCase()] = plural;
irregulars[plural.toLowerCase()] = singular;
}
function uncountable(words) {
uncountables = uncountables.concat(words.map(word => word.toLowerCase()));
}
function pluralize(word) {
if (uncountables.includes(word.toLowerCase())) return word;
if (irregulars[word.toLowerCase()]) return irregulars[word.toLowerCase()];
for (let [rule, replacement] of plurals) {
if (rule.test(word)) return word.replace(rule, replacement);
}
return word;
}
function singularize(word) {
if (uncountables.includes(word.toLowerCase())) return word;
if (irregulars[word.toLowerCase()]) return irregulars[word.toLowerCase()];
for (let [rule, replacement] of singulars) {
if (rule.test(word)) return word.replace(rule, replacement);
}
return word;
}
return {
plural,
singular,
irregular,
uncountable,
pluralize,
singularize,
};
}
const inflector = createInflector();
inflector.plural(/$/, "s");
inflector.plural(/s$/i, "s");
inflector.plural(/^(ax|test)is$/i, '$1es');
inflector.plural(/(octop|vir)us$/i, '$1i');
inflector.plural(/(alias|status)$/i, '$1es');
inflector.plural(/(bu)s$/i, '$1ses');
inflector.plural(/(buffal|tomat)o$/i, '$1oes');
inflector.plural(/([ti])um$/i, '$1a');
inflector.plural(/sis$/i, "ses");
inflector.plural(/(?:([^f])fe|([lr])f)$/i, '$1$2ves');
inflector.plural(/(hive)$/i, '$1s');
inflector.plural(/([^aeiouy]|qu)y$/i, '$1ies');
inflector.plural(/(x|ch|ss|sh)$/i, '$1es');
inflector.plural(/(matr|vert|ind)(?:ix|ex)$/i, '$1ices');
inflector.plural(/^(m|l)ouse$/i, '$1ice');
inflector.plural(/^(ox)$/i, '$1en');
inflector.plural(/(quiz)$/i, '$1zes');
inflector.singular(/s$/i, "");
inflector.singular(/(ss)$/i, '$1');
inflector.singular(/(n)ews$/i, '$1ews');
inflector.singular(/([ti])a$/i, '$1um');
inflector.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '$1sis');
inflector.singular(/([^f])ves$/i, '$1fe');
inflector.singular(/(hive)s$/i, '$1');
inflector.singular(/([lr])ves$/i, '$1f');
inflector.singular(/([^aeiouy]|qu)ies$/i, '$1y');
inflector.singular(/(s)eries$/i, '$1eries');
inflector.singular(/(x|ch|ss|sh)es$/i, '$1');
inflector.singular(/^(m|l)ice$/i, '$1ouse');
inflector.singular(/(bus)(es)?$/i, '$1');
inflector.singular(/(o)es$/i, '$1');
inflector.singular(/(shoe)s$/i, '$1');
inflector.singular(/(cris|test)(is|es)$/i, '$1is');
inflector.singular(/^(a)x[ie]s$/i, '$1xis');
inflector.singular(/(octop|vir)(us|i)$/i, '$1us');
inflector.singular(/(alias|status)(es)?$/i, '$1');
inflector.singular(/^(ox)en/i, '$1');
inflector.singular(/(vert|ind)ices$/i, '$1ex');
inflector.singular(/(quiz)zes$/i, '$1');
inflector.irregular("person", "people");
inflector.irregular("man", "men");
inflector.irregular("child", "children");
inflector.irregular("sex", "sexes");
inflector.irregular("move", "moves");
inflector.irregular("zombie", "zombies");
inflector.uncountable(['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'jeans', 'police']);
module.exports = inflector;