1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var util = require('./util');
|
17 |
|
18 | var Inflections = function () {
|
19 | this.plurals = [];
|
20 | this.singulars = [];
|
21 | this.uncountables = [];
|
22 | this.humans = [];
|
23 | require('./defaults')(this);
|
24 | return this;
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 | Inflections.prototype.plural = function (rule, replacement) {
|
30 | if (typeof rule == 'string') {
|
31 | this.uncountables = util.array.del(this.uncountables, rule);
|
32 | }
|
33 | this.uncountables = util.array.del(this.uncountables, replacement);
|
34 | this.plurals.unshift([rule, replacement]);
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 | Inflections.prototype.singular = function (rule, replacement) {
|
40 | if (typeof rule == 'string') {
|
41 | this.uncountables = util.array.del(this.uncountables, rule);
|
42 | }
|
43 | this.uncountables = util.array.del(this.uncountables, replacement);
|
44 | this.singulars.unshift([rule, replacement]);
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | Inflections.prototype.irregular = function (singular, plural, fullMatchRequired) {
|
53 | this.uncountables = util.array.del(this.uncountables, singular);
|
54 | this.uncountables = util.array.del(this.uncountables, plural);
|
55 | var prefix = "";
|
56 | if (fullMatchRequired) {
|
57 | prefix = "^";
|
58 | }
|
59 | if (singular[0].toUpperCase() == plural[0].toUpperCase()) {
|
60 | this.plural(new RegExp("(" + prefix + singular[0] + ")" + singular.slice(1) + "$", "i"), '$1' + plural.slice(1));
|
61 | this.plural(new RegExp("(" + prefix + plural[0] + ")" + plural.slice(1) + "$", "i"), '$1' + plural.slice(1));
|
62 | this.singular(new RegExp("(" + prefix + plural[0] + ")" + plural.slice(1) + "$", "i"), '$1' + singular.slice(1));
|
63 | } else {
|
64 | this.plural(new RegExp(prefix + (singular[0].toUpperCase()) + singular.slice(1) + "$"), plural[0].toUpperCase() + plural.slice(1));
|
65 | this.plural(new RegExp(prefix + (singular[0].toLowerCase()) + singular.slice(1) + "$"), plural[0].toLowerCase() + plural.slice(1));
|
66 | this.plural(new RegExp(prefix + (plural[0].toUpperCase()) + plural.slice(1) + "$"), plural[0].toUpperCase() + plural.slice(1));
|
67 | this.plural(new RegExp(prefix + (plural[0].toLowerCase()) + plural.slice(1) + "$"), plural[0].toLowerCase() + plural.slice(1));
|
68 | this.singular(new RegExp(prefix + (plural[0].toUpperCase()) + plural.slice(1) + "$"), singular[0].toUpperCase() + singular.slice(1));
|
69 | this.singular(new RegExp(prefix + (plural[0].toLowerCase()) + plural.slice(1) + "$"), singular[0].toLowerCase() + singular.slice(1));
|
70 | }
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | Inflections.prototype.human = function (rule, replacement) {
|
80 | this.humans.unshift([rule, replacement]);
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | Inflections.prototype.uncountable = function (words) {
|
88 | this.uncountables = this.uncountables.concat(words);
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | Inflections.prototype.clear = function (scope) {
|
98 | if (scope == null) scope = 'all';
|
99 | switch (scope) {
|
100 | case 'all':
|
101 | this.plurals = [];
|
102 | this.singulars = [];
|
103 | this.uncountables = [];
|
104 | this.humans = [];
|
105 | default:
|
106 | this[scope] = [];
|
107 | }
|
108 | }
|
109 |
|
110 |
|
111 | Inflections.prototype.default = function () {
|
112 | this.plurals = [];
|
113 | this.singulars = [];
|
114 | this.uncountables = [];
|
115 | this.humans = [];
|
116 | require('./defaults')(this);
|
117 | return this;
|
118 | };
|
119 |
|
120 | module.exports = new Inflections();
|