Permalink
Browse files

Add underscore and tableize methods

  • Loading branch information...
1 parent 20b61aa commit 478d5177d0ecbedc4fb328363a5060a48053aa94 @arbarlow arbarlow committed Apr 29, 2012
Showing with 54 additions and 0 deletions.
  1. +22 −0 lib/inflection.js
  2. +20 −0 lib/lingo.js
  3. +7 −0 test/inflection.en.test.js
  4. +5 −0 test/lingo.test.js
View
@@ -144,6 +144,28 @@ Language.prototype.isSingular = function (word) {
return !this.isPlural(word);
};
+
+/**
+ * Tableize the given `str`.
+ *
+ * Examples:
+ *
+ * lingo.tableize('UserAccount');
+ * // => "user_accounts"
+ *
+ * lingo.tableize('User');
+ * // => "users"
+ *
+ * @param {String} str
+ * @return {String}
+ * @api public
+ */
+
+exports.tableize = function(str){
+ var underscored = exports.underscore(word);
+ return Language.pluralize(underscored);
+};
+
/**
* Perform `type` inflection rules on the given `word`.
*
View
@@ -92,6 +92,26 @@ exports.camelcase = function(str, uppercaseFirst){
}).join('');
};
+/**
+ * Underscore the given `str`.
+ *
+ * Examples:
+ *
+ * lingo.underscore('UserAccount');
+ * // => "user_account"
+ *
+ * lingo.underscore('User');
+ * // => "user"
+ *
+ * @param {String} str
+ * @return {String}
+ * @api public
+ */
+
+exports.underscore = function(str){
+ return str.replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
+};
+
/**
* Join an array with the given `last` string
* which defaults to "and".
@@ -113,5 +113,12 @@ module.exports = {
assert.equal(true, en.isSingular('dog'));
assert.equal(false, en.isSingular('keys'));
assert.equal(false, en.isSingular('foxes'));
+ },
+
+ 'test .tabelize()': function(assert){
+ assert.equal('user_accounts', en.tabelize('UserAccount'));
+ assert.equal('user', en.tabelize('User'));
+ assert.equal('monkeys', en.tabelize('Monkey'));
+ assert.equal('animals', en.tabelize('Animal'));
}
};
View
@@ -23,6 +23,11 @@ module.exports = {
assert.equal('UserRole', lingo.camelcase('user role', true));
},
+ 'test .underscore()': function(assert){
+ assert.equal('user', lingo.underscore('User'));
+ assert.equal('user_account', lingo.underscore('UserAccount'))
+ },
+
'test .join()': function(assert){
assert.equal('foo', lingo.join(['foo']));
assert.equal('foo and bar', lingo.join(['foo', 'bar']));

0 comments on commit 478d517

Please sign in to comment.