Skip to content

Commit

Permalink
Remove unreachable alignment assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
nhle-mgmtp committed Apr 22, 2021
1 parent 31bd12b commit 7d89d48
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
19 changes: 4 additions & 15 deletions src/alignString.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import isNumber from 'is-number-object';
import isString from 'is-string';
import stringWidth from 'string-width';
import type {
ColumnUserConfig,
} from './types/api';

const alignLeft = (subject: string, width: number): string => {
return subject + ' '.repeat(width);
Expand All @@ -24,17 +27,11 @@ const alignCenter = (subject: string, width: number): string => {
}
};

const alignments = [
'left',
'right',
'center',
];

/**
* Pads a string to the left and/or right to position the subject
* text in a desired alignment within a container.
*/
export default (subject: string, containerWidth: number, alignment: string): string => {
export default (subject: string, containerWidth: number, alignment: ColumnUserConfig['alignment']): string => {
if (!isString(subject)) {
throw new TypeError('Subject parameter value must be a string.');
}
Expand All @@ -49,14 +46,6 @@ export default (subject: string, containerWidth: number, alignment: string): str
throw new Error('Subject parameter value width cannot be greater than the container width.');
}

if (!isString(alignment)) {
throw new TypeError('Alignment parameter value must be a string.');
}

if (!alignments.includes(alignment)) {
throw new Error('Alignment parameter value must be a known alignment parameter value (left, right, center).');
}

if (subjectWidth === 0) {
return ' '.repeat(containerWidth);
}
Expand Down
15 changes: 1 addition & 14 deletions test/alignString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,7 @@ describe('alignString', () => {
}).to.throw(Error, 'Subject parameter value width cannot be greater than the container width.');
});
});
context('container alignment parameter value is not a string', () => {
it('throws an error', () => {
expect(() => {
alignString('', 1, 2 as never);
}).to.throw(Error, 'Alignment parameter value must be a string.');
});
});
context('container alignment parameter value is not a known alignment parameter value', () => {
it('throws an error', () => {
expect(() => {
alignString('', 1, 'foo');
}).to.throw(Error, 'Alignment parameter value must be a known alignment parameter value (left, right, center).');
});
});

context('subject parameter value', () => {
context('0 width', () => {
it('produces a string consisting of container width number of whitespace characters', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/configSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
{columns: {1: {unknown: 1}}},
{columns: {1: {alignment: 1}}},
{columns: {1: {alignment: '1'}}},
{columns: {0: {alignment: 'middle'}}},
{columns: {1: {width: '5'}}},
{columns: {1: {wrapWord: 1}}},
{columns: {1: {truncate: '1'}}},
Expand All @@ -36,6 +37,7 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
{columnDefault: {unknown: 1}},
{columnDefault: {alignment: 1}},
{columnDefault: {alignment: '1'}},
{columnDefault: {alignment: 'middle'}},
{columnDefault: {width: '5'}},
{columnDefault: {wrapWord: 1}},
{columnDefault: {truncate: '1'}},
Expand All @@ -45,6 +47,7 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
{unknown: 1},
],
valid: [
{},
{
columns: {
0: {
Expand Down Expand Up @@ -121,6 +124,9 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
},
},
},
{columns: {0: {alignment: 'left'}}},
{columns: {1: {alignment: 'right'}}},
{columns: {2: {alignment: 'center'}}},
{border: {topBody: '-'}},
{border: {topJoin: '-'}},
{border: {topLeft: '-'}},
Expand Down
24 changes: 24 additions & 0 deletions test/streamConfigSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const streamConfigSamples: {invalid: unknown[], valid: StreamUserConfig[], } = {
{columnDefault: {paddingLeft: '1'}},
{columnDefault: {paddingRight: '1'}},
{unknown: 1},
{
columnCount: 3,
columnDefault: {width: 20},
columns: {0: {alignment: 'middle'}},
},

// eslint-disable-next-line no-warning-comments
// TODO: Fix the schema so that the following configs are truly invalid
Expand Down Expand Up @@ -74,6 +79,25 @@ const streamConfigSamples: {invalid: unknown[], valid: StreamUserConfig[], } = {
// {columnDefault: {paddingRight: 1}},
],
valid: [
{
columnCount: 3,
columnDefault: {width: 20},
},
{
columnCount: 3,
columnDefault: {width: 20},
columns: {0: {alignment: 'left'}},
},
{
columnCount: 3,
columnDefault: {width: 20},
columns: {1: {alignment: 'right'}},
},
{
columnCount: 3,
columnDefault: {width: 20},
columns: {2: {alignment: 'center'}},
},
{
columnCount: 3,
columnDefault: {
Expand Down

0 comments on commit 7d89d48

Please sign in to comment.