Nodejs String to Underscore toUnderscore()

Here you can find the source of toUnderscore()

Method Source Code

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();});
};

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()
    String.prototype.toUnderscore = function() {
      return this.replace(/(^[A-Z])/, function(match) {
        return match.toLowerCase();
      }).replace(/([A-Z])/g, function(match){
        return "_" + match.toLowerCase();
      });
    };
    
  4. 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;
    };
    ...
    
  5. toUnderscore()
    String.prototype.toUnderscore = function() {
      var str = this;
      var modified = '';
      for (var i = 0; i < str.length; i++) {
        if (isLower(str[i])) {
          modified += str[i];
        } else {
          if (str.length > i-1) {
            if (isLower(str[i-1])) {
    ...
    
  6. underscore()
    String.prototype.underscore = function() {
      return this.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
    };