Skip to content

Commit

Permalink
fix: alignCenter (#115) (#116)
Browse files Browse the repository at this point in the history
The method for alignCenter is checking
to see if halfWidth (rather than width)
is even. This worked with the existing test
case because the container width given is 6
minus string width (2) is 4. Divide that in half
and you have an even number.

My test case makes the container width 8.
So available width is 6. Divide that in half
you get an odd number. So the odd logic triggers
padding 3 to the left and 4 to the right.

This commit fixes the minor defect.
  • Loading branch information
michaelgwelch committed Jul 30, 2020
1 parent 37fa97c commit fc2c35b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/alignString.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const alignCenter = (subject, width) => {

halfWidth = width / 2;

if (halfWidth % 2 === 0) {
if (width % 2 === 0) {
return ' '.repeat(halfWidth) + subject + ' '.repeat(halfWidth);
} else {
halfWidth = Math.floor(halfWidth);
Expand Down
2 changes: 1 addition & 1 deletion test/alignString.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('alignString', () => {
});
context('center', () => {
it('pads the string on both sides using a whitespace character', () => {
expect(alignString('aa', 6, 'center')).to.equal(' aa ');
expect(alignString('aa', 8, 'center')).to.equal(' aa ');
});
context('uneven number of available with', () => {
it('floors the available width; adds extra space to the end of the string', () => {
Expand Down

0 comments on commit fc2c35b

Please sign in to comment.