Nodejs String to Underscore toUnderscore()

Here you can find the source of toUnderscore()

Method Source Code

/**//from   www .j a  v a  2s.  co  m
 * This function returns an underscored version of the string.
 *
 * @method
 * @public
 *
 * @returns An underscored version of this string.
 */
String.prototype.toUnderscore = function() {
  var str = this;
  var modified = '';

  for (var i = 0; i < str.length; i++) {

    // If this character is lowercase, append it to the string.
    if (isLower(str[i])) {
      modified += str[i];

    // If this character is uppercase, continue processing.
    } else {

      // If there is a previous character, continue processing.
      if (str.length > i-1) {

        // If the previous character is lowercase, then we need to append an
        // underscore.
        if (isLower(str[i-1])) {
          modified += '_' + str[i].toLowerCase();
        } else {
          modified += str[i].toLowerCase();
        }

      // If there's no previous character, then just lowercase this one.
      } else {
        modified += str[i].toLowerCase();
      }
    }
  }

  return modified;
};

Related

  1. toUnderscore()
    String.prototype.toUnderscore = function(){
      return this.charAt(0).toLowerCase() + this.substring(1).replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
    };
    console.log(process.argv[2].toUnderscore());
    
  2. toUnderscore()
    String.prototype.toUnderscore = function(){
      return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
    };
    
  3. toUnderscore()
    function getId(obj){
      return parseInt($(obj).attr('id').split("-").pop(), 10);
    String.prototype.toUnderscore = function(){
      return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
    };
    
  4. toUnderscore()
    String.prototype.toUnderscore = function() {
      return this.replace(/(^[A-Z])/, function(match) {
        return match.toLowerCase();
      }).replace(/([A-Z])/g, function(match){
        return "_" + match.toLowerCase();
      });
    };
    
  5. toUnderscore()
    String.prototype.toUnderscore = function() {
      var str = this.replace(/[A-Z]/g, function(s) {
        return "_" + s.toLowerCase();
      });
      while (str.charAt(0) === '_') {
        str = str.slice(1);
      return str;
    };
    ...
    
  6. underscore()
    String.prototype.underscore = function() {
      return this.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
    };
    
  7. underscore()
    String.prototype.underscore = function() {
      return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
    };
    
  8. underscore()
    String.prototype.underscore = function() {
      return this
        .replace(/::/g, '/')
        .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
        .replace(/([a-z\d])([A-Z])/g, '$1_$2')
        .replace(/-/g, '_')
        .toLowerCase();
    };
    
  9. underscore()
    String.prototype.underscore = function() {
      return this.trim().toLowerCase().replace(/[\s]+/g, '_');