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

Commit

Permalink
feat(getMiddle): add getMiddle function (#189)
Browse files Browse the repository at this point in the history
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
d-ivashchuk authored and Kent C. Dodds committed Sep 6, 2018
1 parent f58b735 commit 7808a04
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@
"kentcdodds/ava"
]
}
}
}
12 changes: 12 additions & 0 deletions src/getMiddle.js
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)
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import makeObjectIterable from './makeObjectIterable'
import fibonacciSum from './fibonacciSum'
import lcm from './lcm'
import occurrences from './occurrences'
import getMiddle from './getMiddle'

export {
reverseArrayInPlace,
Expand Down Expand Up @@ -140,4 +141,5 @@ export {
fibonacciSum,
lcm,
occurrences,
getMiddle,
}
15 changes: 15 additions & 0 deletions test/getMiddle.test.js
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)
})

0 comments on commit 7808a04

Please sign in to comment.