Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 8735163

Browse files
jefferson-silvaKent C. Dodds
authored andcommitted
feat(swapCase): add a swapCase function (#88)
1 parent 6898a39 commit 8735163

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import searchAndReplace from './search-and-replace'
2828
import sqrt from './sqrt'
2929
import toPower from './to-power'
3030
import mod from './mod'
31+
import swapCase from './swap-case'
3132

3233
export {
3334
initArray,
@@ -59,4 +60,5 @@ export {
5960
sqrt,
6061
toPower,
6162
mod,
63+
swapCase,
6264
}

src/swap-case.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default swapCase
2+
3+
/**
4+
* Original source: http://stackoverflow.com/a/40192407/4204587
5+
*
6+
* This method will swap cases for a given string.
7+
*
8+
* @param {String} s - string for case swapping
9+
* @return {String} - string with all cases swapped
10+
*/
11+
function swapCase(s) {
12+
return s.split('').map(function swapper(c) {
13+
return c === c.toUpperCase() ?
14+
c.toLowerCase() :
15+
c.toUpperCase()
16+
}).join('')
17+
}

test/swap-case.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import test from 'ava'
2+
import {swapCase} from '../src'
3+
4+
test('Swap cases from string ', t => {
5+
const original = 'That\'s one small step for a DEVELOPER, one giant leap for a Community.'
6+
const expected = 'tHAT\'S ONE SMALL STEP FOR A developer, ONE GIANT LEAP FOR A cOMMUNITY.'
7+
const actual = swapCase(original)
8+
t.is(actual, expected)
9+
})

0 commit comments

Comments
 (0)