diff --git a/test/strings.js b/test/strings.js index 13a7a13a..102600bb 100644 --- a/test/strings.js +++ b/test/strings.js @@ -435,6 +435,8 @@ $(document).ready(function() { }); test('String: underscored', function(){ + equal(_('oneAtATime').underscored(), 'one_at_a_time'); + equal(_('oneAtATime AnotherWordAtATime').underscored(), 'one_at_a_time_another_word_at_a_time'); equal(_('the-underscored-string-method').underscored(), 'the_underscored_string_method'); equal(_('theUnderscoredStringMethod').underscored(), 'the_underscored_string_method'); equal(_('TheUnderscoredStringMethod').underscored(), 'the_underscored_string_method'); diff --git a/underscored.js b/underscored.js index b9d16283..a073faaf 100644 --- a/underscored.js +++ b/underscored.js @@ -1,5 +1,5 @@ var trim = require('./trim'); module.exports = function underscored(str) { - return trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase(); + return trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/([A-Z\d]+)([A-Z][a-z])/g,'$1_$2').replace(/[-\s]+/g, '_').toLowerCase(); };