UNPKG

voca

Version:

The ultimate JavaScript string library

35 lines (30 loc) 933 B
import './internal/is_nil.js'; import './is_string.js'; import { c as coerceToString } from './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 = 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); } export default swapCase;