-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathswap_case.js
More file actions
29 lines (27 loc) · 862 Bytes
/
swap_case.js
File metadata and controls
29 lines (27 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import coerceToString from 'helper/string/coerce_to_string';
/**
* 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'
*/
export default function swapCase(subject) {
const subjectString = coerceToString(subject);
return subjectString.split('').reduce(swapAndConcat, '');
}
function swapAndConcat(swapped, character) {
const lowerCase = character.toLowerCase();
const upperCase = character.toUpperCase();
return swapped + (character === lowerCase ? upperCase : lowerCase);
}