Nodejs String Case Convert toAlternatingCase()

Here you can find the source of toAlternatingCase()

Method Source Code

// altERnaTIng cAsE <=> ALTerNAtiNG CaSe
// Define to_alternating_case(char*) such that each lowercase letter becomes
// uppercase and each uppercase letter becomes lowercase.
// For example://www .j a va2  s.co  m
// char source[] = "hello world";
// char *upperCase = to_alternating_case(source);
// (void)puts(upperCase); // outputs: HELLO WORLD
// char source[] = "HELLO WORLD";
// char *upperCase = to_alternating_case(source);
// (void)puts(upperCase); // outputs: hello world
// char source[] = "HeLLo WoRLD";
// char *upperCase = to_alternating_case(source);
// (void)puts(upperCase); // outputs: hEllO wOrld

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];
    }
  }
  return output;
}
console.log('hello world'.toAlternatingCase(), '|', 'HELLO WORLD');
console.log('HELLO WORLD'.toAlternatingCase(), '|', 'hello world');
console.log('12345'.toAlternatingCase(), '|', '12345');
console.log('1a2b3c4d5e'.toAlternatingCase(), '|', '1A2B3C4D5E');

Related

  1. toAlternatingCase()
    String.prototype.toAlternatingCase = function() {
      return [...this].map(
        (c) => (c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()),
      ).join``;
    };
    
  2. toAlternatingCase()
    String.prototype.toAlternatingCase = function () {
      var _str = ""; 
      for(var _c = 0; _c < this.length; _c++){
        _str += ((this[_c] === this[_c].toUpperCase())? this[_c].toLowerCase() : this[_c].toUpperCase());
      return _str;
    
  3. toAlternatingCase()
    String.prototype.toAlternatingCase = function () {
      var result = [];
      var letters = this.split("");
      letters.forEach(function(char) {
        if ((char.charCodeAt(0) >= 97) && (char.charCodeAt(0) <= 122)) {
          result.push(char.toUpperCase());
        } else if ((char.charCodeAt(0) >= 65) && (char.charCodeAt(0) <= 90)){
          result.push(char.toLowerCase());
        } else {
    ...
    
  4. toAlternatingCase()
    String.prototype.toAlternatingCase = function () {
      var newStr = "";
      for (var i = 0; i < this.length; i++){
        if (this.charAt(i).toUpperCase() === this.charAt(i)){
           newStr += this.charAt(i).toLowerCase();
        } else {
           newStr += this.charAt(i).toUpperCase();  
      return newStr;
    
  5. toAlternatingCase()
    String.prototype.toAlternatingCase = function () {
      return this.toString().split('')
                            .map(l => l.match(/[a-z]/)  ? l.toUpperCase() : l.toLowerCase())
                            .join('');
    
  6. snakeCase()
    String.prototype.snakeCase = function() {
      return this.replace( /([A-Z])/g, function( $1 ) {return "_" + $1.toLowerCase();} );
    };
    
  7. snakeCaseDash()
    String.prototype.snakeCaseDash = function() {
      return this.replace( /([A-Z])/g, function( $1 ) {return "-" + $1.toLowerCase();} );
    };
    
  8. snake_case()
    String.prototype.snake_case = function(){
        return this
            .replace( /[A-Z]/g, function($1) { return '_' + $1 } )
            .toLowerCase();
    
  9. toSnakeCase()
    String.prototype.toSnakeCase = function(){
      return this.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
    };