Nodejs String Case Convert toSnakeCase()

Here you can find the source of toSnakeCase()

Method Source Code

/*@//  ww w. jav  a  2  s .  c  o m
  @file: src/tosnakecase.js
  @version: 1.0.0
  @website: http://webman.io/missing.js/tosnakecase.js
  @author: Mark Topper
  @author-website: http://webman.io
  @function: toSnakeCase
  @var:  String  the string to turn into snake case
  @description: convert a string into snake case, like from "helloWorld" to "hello_world"
@*/
String.prototype.toSnakeCase = function(){
   return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
};

Related

  1. toAlternatingCase()
    String.prototype.toAlternatingCase = function () {
      return this.toString().split('')
                            .map(l => l.match(/[a-z]/)  ? l.toUpperCase() : l.toLowerCase())
                            .join('');
    
  2. toAlternatingCase()
    String.prototype.toAlternatingCase = function() {
      var output = '';
      for (var i=0; i<this.length; i++) {
        if (this[i].toLowerCase() === this[i]) {
          output = output + this[i].toUpperCase();
        } else if (this[i].toUpperCase() === this[i]) {
          output = output + this[i].toLowerCase();
        } else {
          output = output + this[i];
    ...
    
  3. snakeCase()
    String.prototype.snakeCase = function() {
      return this.replace( /([A-Z])/g, function( $1 ) {return "_" + $1.toLowerCase();} );
    };
    
  4. snakeCaseDash()
    String.prototype.snakeCaseDash = function() {
      return this.replace( /([A-Z])/g, function( $1 ) {return "-" + $1.toLowerCase();} );
    };
    
  5. snake_case()
    String.prototype.snake_case = function(){
        return this
            .replace( /[A-Z]/g, function($1) { return '_' + $1 } )
            .toLowerCase();
    
  6. toCap()
    String.prototype.toCap = function () {
      var result = this.toUpperCase().charAt(0) + this.slice(1, this.length);
      return result;
    };
    
  7. toCap()
    String.prototype.toCap = function() {
      var string = this.toString();
      if(!string.length) return;
      var lastName = string.split(' ').pop();
      var firstName = string.split(' ')[0];
      lastName = lastName[0].toUpperCase() + lastName.slice(1);
      firstName = firstName[0].toUpperCase() + firstName.slice(1);
      string = firstName + " " + lastName;
      return string;
    ...
    
  8. toCap()
    'use strict'
    export default (obj) => String.prototype.toCap.apply(obj)
    String.prototype.toCap = function () {
     return this.toUpperCase().charAt(0) + this.slice(1,(this.length));