|
30 | 30 | }
|
31 | 31 | };
|
32 | 32 |
|
| 33 | + function convertNumber(str) { |
| 34 | + if (str === '-Inf') { |
| 35 | + return -Infinity; |
| 36 | + } else if (str === '+Inf' || str === 'Inf') { |
| 37 | + return Infinity; |
| 38 | + } |
| 39 | + return parseInt(str, 10); |
| 40 | + } |
| 41 | + |
| 42 | + // Derived from: https://github.com/symfony/translation/blob/460390765eb7bb9338a4a323b8a4e815a47541ba/Interval.php |
| 43 | + var intervalRegexp = /^({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\-?\d+(\.\d+)?)\s*([\[\]])$/; |
| 44 | + var anyIntervalRegexp = /({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([\[\]])\s*(-Inf|\-?\d+(\.\d+)?)\s*,\s*(\+?Inf|\-?\d+(\.\d+)?)\s*([\[\]])/; |
| 45 | + |
33 | 46 | // Default options //
|
34 | 47 |
|
35 | 48 | var defaults = {
|
|
184 | 197 |
|
185 | 198 | // Get the explicit rules, If any
|
186 | 199 | var explicitRules = [];
|
187 |
| - var regex = /{\d+}\s(.+)|\[\d+,\d+\]\s(.+)|\[\d+,Inf\]\s(.+)/; |
188 | 200 |
|
189 | 201 | for (var i = 0; i < messageParts.length; i++) {
|
190 | 202 | messageParts[i] = messageParts[i].trim();
|
191 | 203 |
|
192 |
| - if (regex.test(messageParts[i])) { |
| 204 | + if (anyIntervalRegexp.test(messageParts[i])) { |
193 | 205 | var messageSpaceSplit = messageParts[i].split(/\s/);
|
194 | 206 | explicitRules.push(messageSpaceSplit.shift());
|
195 | 207 | messageParts[i] = messageSpaceSplit.join(' ');
|
|
297 | 309 | /**
|
298 | 310 | * Checks if the given `count` is within the interval defined by the {string} `interval`
|
299 | 311 | *
|
300 |
| - * @param count {int} The amount of items. |
301 |
| - * @param interval {string} The interval to be compared with the count. |
302 |
| - * @return {boolean} Returns true if count is within interval; false otherwise. |
| 312 | + * @param count {int} The amount of items. |
| 313 | + * @param interval {string} The interval to be compared with the count. |
| 314 | + * @return {boolean} Returns true if count is within interval; false otherwise. |
303 | 315 | */
|
304 | 316 | Lang.prototype._testInterval = function(count, interval) {
|
305 | 317 | /**
|
306 | 318 | * From the Symfony\Component\Translation\Interval Docs
|
307 | 319 | *
|
308 | 320 | * Tests if a given number belongs to a given math interval.
|
309 |
| - * An interval can represent a finite set of numbers: {1,2,3,4} |
310 |
| - * An interval can represent numbers between two numbers: [1, +Inf] ]-1,2[ |
| 321 | + * |
| 322 | + * An interval can represent a finite set of numbers: |
| 323 | + * |
| 324 | + * {1,2,3,4} |
| 325 | + * |
| 326 | + * An interval can represent numbers between two numbers: |
| 327 | + * |
| 328 | + * [1, +Inf] |
| 329 | + * ]-1,2[ |
| 330 | + * |
311 | 331 | * The left delimiter can be [ (inclusive) or ] (exclusive).
|
312 | 332 | * The right delimiter can be [ (exclusive) or ] (inclusive).
|
313 | 333 | * Beside numbers, you can use -Inf and +Inf for the infinite.
|
314 | 334 | */
|
315 | 335 |
|
| 336 | + if (typeof interval !== 'string') { |
| 337 | + throw 'Invalid interval: should be a string.'; |
| 338 | + } |
| 339 | + |
| 340 | + interval = interval.trim(); |
| 341 | + |
| 342 | + var matches = interval.match(intervalRegexp); |
| 343 | + if (!matches) { |
| 344 | + throw new 'Invalid interval: ' + interval; |
| 345 | + } |
| 346 | + |
| 347 | + if (matches[2]) { |
| 348 | + var items = matches[2].split(','); |
| 349 | + for (var i = 0; i < items.length; i++) { |
| 350 | + if (parseInt(items[i], 10) === count) { |
| 351 | + return true; |
| 352 | + } |
| 353 | + } |
| 354 | + } else { |
| 355 | + // Remove falsy values. |
| 356 | + matches = matches.filter(function(match) { |
| 357 | + return !!match; |
| 358 | + }); |
| 359 | + |
| 360 | + var leftDelimiter = matches[1]; |
| 361 | + var leftNumber = convertNumber(matches[2]); |
| 362 | + var rightNumber = convertNumber(matches[3]); |
| 363 | + var rightDelimiter = matches[4]; |
| 364 | + |
| 365 | + return (leftDelimiter === '[' ? count >= leftNumber : count > leftNumber) |
| 366 | + && (rightDelimiter === ']' ? count <= rightNumber : count < rightNumber); |
| 367 | + } |
| 368 | + |
316 | 369 | return false;
|
317 | 370 | };
|
318 | 371 |
|
|
0 commit comments