voca
Version:
The ultimate JavaScript string library
37 lines (31 loc) • 967 B
JavaScript
;
require('./internal/is_nil.js');
require('./is_string.js');
var coerce_to_string = require('./internal/coerce_to_string.js');
/**
* Converts the uppercase alpha characters of `subject` to lowercase and lowercase
* characters to uppercase.
*
* @function swapCase
* @static
* @since 1.3.0
* @memberOf Case
* @param {string} [subject=''] The string to swap the case.
* @return {string} Returns the converted string.
* @example
* v.swapCase('League of Shadows');
* // => 'lEAGUE OF sHADOWS'
*
* v.swapCase('2 Bees');
* // => '2 bEES'
*/
function swapCase(subject) {
var subjectString = coerce_to_string.coerceToString(subject);
return subjectString.split('').reduce(swapAndConcat, '');
}
function swapAndConcat(swapped, character) {
var lowerCase = character.toLowerCase();
var upperCase = character.toUpperCase();
return swapped + (character === lowerCase ? upperCase : lowerCase);
}
module.exports = swapCase;