Permalink
Browse files

Merge branch 'integration'

  • Loading branch information...
2 parents 766738a + e5fa9ad commit 05875827c714ddbb314575eb462073cc54006a29 @tj committed Nov 28, 2010
Showing with 186 additions and 12 deletions.
  1. +25 −8 lib/inflection.js
  2. +7 −0 lib/languages/en.js
  3. +93 −0 lib/languages/es.js
  4. +2 −1 lib/lingo.js
  5. +10 −3 test/inflection.en.test.js
  6. +49 −0 test/inflection.es.test.js
View
33 lib/inflection.js
@@ -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
@@ -94,13 +107,17 @@ Language.prototype.pluralize = function(word){
/**
* Check if `word` is plural.
*
- * @param {String} word
+ * @param {String or Number} word
* @return {Boolean}
* @api public
*/
-Language.prototype.isPlural = function(word){
- return word == this.pluralize(this.singularize(word));
+Language.prototype.isPlural = function (word) {
+ if ('number' == typeof word) {
+ return (this.rules.pluralNumbers || /.*/).test(word);
+ } else {
+ return word == this.pluralize(this.singularize(word));
+ }
};
/**
@@ -111,20 +128,20 @@ Language.prototype.isPlural = function(word){
* @api public
*/
-Language.prototype.singularize = function(word){
+Language.prototype.singularize = function (word) {
return this.inflect(word, 'singular');
};
/**
* Check if `word` is singular.
*
- * @param {String} word
+ * @param {String or Number} word
* @return {Boolean}
* @api public
*/
-Language.prototype.isSingular = function(word){
- return word == this.singularize(this.pluralize(word));
+Language.prototype.isSingular = function (word) {
+ return !this.isPlural(word);
};
/**
@@ -152,4 +169,4 @@ Language.prototype.inflect = function(word, type) {
}
return word;
-}
+}
View
7 lib/languages/en.js
@@ -17,6 +17,13 @@ var Language = require('../language');
var en = module.exports = new Language('en');
+en.name = 'English';
+
+/**
+ * Number pluraluzation rule
+ */
+en.pluralNumbers(/[^1]/);
+
/**
* Default pluralization rules.
*/
View
93 lib/languages/es.js
@@ -0,0 +1,93 @@
+/*!
+ * Lingo - languages - Spanish
+ * Copyright(c) 2010 Pau Ramon <masylum@gmail.com>
+ * Based on Bermi's Python inflector http://github.com/bermi/Python-Inflector
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var Language = require('../language');
+
+/**
+ * English.
+ */
+
+var es = module.exports = new Language('es');
+
+es.name = 'Español';
+
+/**
+ * Number pluraluzation rule
+ */
+es.pluralNumbers(/[^1]/);
+
+/**
+ * Default pluralization rules.
+ */
+
+es.plural(/$/, "es")
+ .plural(/(ng|[wckgtp])$/i, "$1s")
+ .plural(/([íú])$/i, "$1es")
+ .plural(/z$/i, "ces")
+ .plural(/([éí])(s)$/i, "$1$2es")
+ .plural(/([aeiou])s$/i, "$1s")
+ .plural(/([aeiouáéó])$/i, "$1s")
+ .plural(/(^[bcdfghjklmnñpqrstvwxyz]*)([aeiou])([ns])$/i, "$1$2$3es")
+ .plural(/([áéíóú])s$/i, "$1ses")
+ .plural(/(^[bcdfghjklmnñpqrstvwxyz]*)an$/i, "$1anes")
+ .plural(/([á])([ns])$/i, "a$2es")
+ .plural(/([é])([ns])$/i, "e$2es")
+ .plural(/([í])([ns])$/i, "i$2es")
+ .plural(/([ó])([ns])$/i, "o$2es")
+ .plural(/([ú])([ns])$/i, "u$2es")
+ .plural(/([aeiou])x$/i, "$1x");
+
+/**
+ * Default singularization rules.
+ */
+
+es.singular(/es$/i, "")
+ .singular(/([ghñpv]e)s$/i, "$1")
+ .singular(/([bcdfghjklmnñprstvwxyz]{2,}e)s$/i, "$1")
+ .singular(/([^e])s$/i, "$1")
+ .singular(/(é)s$/i, "$1")
+ .singular(/(sis|tis|xis)+$/i, "$1")
+ .singular(/(ces)$/i, "z")
+ .singular(/oides$/i, "oide")
+ .singular(/([a])([ns])es$/i, "á$2")
+ .singular(/([e])([ns])es$/i, "é$2")
+ .singular(/([i])([ns])es$/i, "í$2")
+ .singular(/([o])([ns])es$/i, "ó$2")
+ .singular(/([u])([ns])es$/i, "ú$2")
+ .singular(/^([bcdfghjklmnñpqrstvwxyz]*)([aeiou])([ns])es$/i, "$1$2$3");
+
+/**
+ * Default irregular word mappings.
+ */
+
+es.irregular('país', 'países')
+ .irregular('champú', 'champús')
+ .irregular('jersey', 'jerséis')
+ .irregular('carácter', 'caracteres')
+ .irregular('espécimen', 'especímenes')
+ .irregular('menú', 'menús')
+ .irregular('régimen', 'regímenes')
+ .irregular('curriculum', 'currículos')
+ .irregular('ultimátum', 'ultimatos')
+ .irregular('memorándum', 'memorandos')
+ .irregular('referéndum', 'referendos')
+
+/**
+ * Default uncountables.
+ */
+
+es.uncountable('tijeras')
+ .uncountable('gafas')
+ .uncountable('agua')
+ .uncountable('vacaciones')
+ .uncountable('víveres')
+ .uncountable('déficit')
+
View
3 lib/lingo.js
@@ -38,6 +38,7 @@ require('./inflection');
*/
require('./languages/en');
+require('./languages/es');
/**
* Capitalize the first word of `str` or optionally `allWords`.
@@ -116,4 +117,4 @@ exports.join = function(arr, last){
str = arr.join(', ') + ' ' + last + ' ' + str;
}
return str;
-};
+};
View
13 test/inflection.en.test.js
@@ -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'));
}
-};
+};
View
49 test/inflection.es.test.js
@@ -0,0 +1,49 @@
+/**
+ * Module dependencies.
+ */
+
+var lingo = require('./..')
+ , es = lingo.es;
+
+module.exports = {
+ 'test .isUncountable()': function(assert){
+ assert.equal(true, es.isUncountable('agua'));
+ assert.equal(false, es.isUncountable('arbol'));
+ },
+
+ 'test .pluralNumbers()': function(assert){
+ delete es.rules.pluralNumbers;
+ assert.equal(true, es.isPlural(1));
+ es.pluralNumbers(/[^1]/);
+ assert.equal(false, es.isPlural(1));
+ },
+
+ 'test .pluralize()': function(assert){
+ assert.equal('perros', es.pluralize('perro'));
+ assert.equal('arboles', es.pluralize('arbol'));
+ assert.equal('cojones', es.pluralize('cojón'));
+ assert.equal('androides', es.pluralize('androide'));
+ },
+
+ 'test .singularize()': function(assert){
+ assert.equal('perro', es.singularize('perros'));
+ assert.equal('árbol', es.singularize('árboles'));
+ assert.equal('cojón', es.singularize('cojones'));
+ assert.equal('androide', es.singularize('androides'));
+ },
+
+ 'test .isPlural()': function(assert){
+ assert.equal(true, es.isPlural('perros'));
+ assert.equal(true, es.isPlural('árboles'));
+ assert.equal(true, es.isPlural('cojones'));
+ assert.equal(true, es.isPlural('androides'));
+ },
+
+ 'test .isSingular()': function(assert){
+ assert.equal(true, es.isSingular('perro'));
+ assert.equal(true, es.isSingular('árbol'));
+ assert.equal(true, es.isSingular('cojón'));
+ assert.equal(true, es.isSingular('androide'));
+ }
+};
+

0 comments on commit 0587582

Please sign in to comment.