Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added plural rules for numbers

  • Loading branch information...
commit 799a3a204aeea19d5addb27b9ea3261d772ecc56 1 parent 766738a
Pau Ramon Revilla masylum authored committed
21 lib/inflection.js
View
@@ -52,6 +52,19 @@ Language.prototype.irregular = function(singular, plural){
};
/**
+ * Add a pluralization `rule` for numbers
+ *
+ * @param {RegExp} rule
+ * @return {Language} for chaining
+ * @api public
+ */
+
+Language.prototype.pluralNumbers = function(rule){
+ this.rules.pluralNumbers = rule;
+ return this;
+};
+
+/**
* Add a pluralization `rule` with the given `substitution`.
*
* @param {RegExp} rule
@@ -100,7 +113,11 @@ Language.prototype.pluralize = function(word){
*/
Language.prototype.isPlural = function(word){
- return word == this.pluralize(this.singularize(word));
+ if (typeof word === 'number') {
+ return (this.rules.pluralNumbers || /.*/).test(word);
+ } else {
+ return word == this.pluralize(this.singularize(word));
+ }
};
/**
@@ -152,4 +169,4 @@ Language.prototype.inflect = function(word, type) {
}
return word;
-}
+}
6 lib/languages/en.js
View
@@ -17,6 +17,12 @@ var Language = require('../language');
var en = module.exports = new Language('en');
+
+/**
+ * Number pluraluzation rule
+ */
+en.pluralNumbers(/[^1]/);
+
/**
* Default pluralization rules.
*/
13 test/inflection.en.test.js
View
@@ -3,7 +3,7 @@
* Module dependencies.
*/
-var lingo = require('lingo')
+var lingo = require('./..')
, en = lingo.en;
module.exports = {
@@ -18,7 +18,14 @@ module.exports = {
en.uncountable('foobar');
assert.equal(true, en.isUncountable('foobar'));
},
-
+
+ 'test .pluralNumbers()': function(assert){
+ delete en.rules.pluralNumbers;
+ assert.equal(true, en.isPlural(1));
+ en.pluralNumbers(/[^1]/);
+ assert.equal(false, en.isPlural(1));
+ },
+
'test .pluralize()': function(assert){
assert.equal('ids', en.pluralize('id'));
assert.equal('friends', en.pluralize('friend'));
@@ -100,4 +107,4 @@ module.exports = {
assert.equal(false, en.isSingular('keys'));
assert.equal(false, en.isSingular('foxes'));
}
-};
+};
Please sign in to comment.
Something went wrong with that request. Please try again.