-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathi18nextIntervalPluralPostProcessor.js
220 lines (173 loc) · 4.76 KB
/
i18nextIntervalPluralPostProcessor.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.i18nextIntervalPluralPostProcessor = factory());
}(this, (function () { 'use strict';
var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}
function AsyncGenerator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}
this._invoke = send;
if (typeof gen.return !== "function") {
this.return = undefined;
}
}
if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}
AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};
AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};
AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};
return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
function intervalMatches(interval, count) {
if (interval.indexOf('-') > -1) {
var p = interval.split('-');
if (p[1] === 'inf') {
var from = parseInt(p[0], 10);
return count >= from;
} else {
var _from = parseInt(p[0], 10);
var to = parseInt(p[1], 10);
return count >= _from && count <= to;
}
} else {
var match = parseInt(interval, 10);
return match === count;
}
}
var index = {
name: 'interval',
type: 'postProcessor',
options: {
intervalSeparator: ';',
intervalRegex: /\((\S*)\).*?\[((.|\n)*)\]/,
intervalSuffix: '_interval'
},
setOptions: function setOptions(options) {
this.options = _extends({}, this.options, options);
},
process: function process(value, key, options, translator) {
var _this = this;
var p = value.split(this.options.intervalSeparator);
var found = void 0;
p.forEach(function (iv) {
if (found) return;
var match = _this.options.intervalRegex.exec(iv);
if (match && intervalMatches(match[1], options.count || 0)) {
found = match[2];
}
});
// not found fallback to classical plural
if (!found) {
var newOptions = _extends({}, options);
if (typeof newOptions.postProcess === 'string') {
delete newOptions.postProcess;
} else {
var index = newOptions.postProcess.indexOf('interval'); // <-- Not supported in <IE9
if (index !== -1) newOptions.postProcess.splice(index, 1);
}
var newKeys = void 0;
if (typeof key === 'string') {
newKeys = key.replace(this.options.intervalSuffix, '');
} else if (key.length > -1) {
newKeys = key.map(function (k) {
return k.replace(_this.options.intervalSuffix, '');
});
}
if (newKeys) found = translator.translate(newKeys, newOptions);
}
return found || value;
}
};
return index;
})));