All files / to-case to-invert-case.ts

100% Statements 19/19
100% Branches 5/5
100% Functions 1/1
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 1x 1x 1x 1x  
/**
 * Invert each character in a `string` from upper to lower and vice versa.
 *
 * @param {String} string
 * @return {String}
 */
 
function toInvertCase(string) {
  var chars = string.split("");
  for (var i = 0, char; (char = chars[i]); i++) {
    if (!/[a-z]/i.test(char)) continue;
    var upper = char.toUpperCase();
    var lower = char.toLowerCase();
    chars[i] = char == upper ? lower : upper;
  }
  return chars.join("");
}
 
export default toInvertCase;