This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(getMiddle): add getMiddle function (#189)
My first pull request! Had to use --no-verify because some error didn't let me commit the changes though. Thanks again for the opportunity!
- Loading branch information
1 parent
f58b735
commit 7808a04
Showing
4 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,4 +101,4 @@ | |
"kentcdodds/ava" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default getMiddle | ||
|
||
/** | ||
* This method will return the character that stays in the middle of the string. | ||
* | ||
* @param {String} string - string to get the middle character from | ||
* @return {String} - middle character of the string | ||
**/ | ||
|
||
function getMiddle(string) { | ||
return string.substr(Math.ceil(string.length / 2 - 1), string.length % 2 === 0 ? 2 : 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import test from 'ava' | ||
import { getMiddle } from '../src' | ||
|
||
test('Gets middle character of given string with a length of uneven number of characters ', t => { | ||
const string = 'rumpelstiltskin' | ||
const expected = 't' | ||
const actual = getMiddle(string) | ||
t.deepEqual(actual, expected) | ||
}) | ||
test('Gets two middle characters of given string with a length of even number of characters ', t => { | ||
const string = 'pull' | ||
const expected = 'ul' | ||
const actual = getMiddle(string) | ||
t.deepEqual(actual, expected) | ||
}) |