Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unreachable alignment assertions #158

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/alignString.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import stringWidth from 'string-width';
import type {
ColumnUserConfig,
} from './types/api';

const alignLeft = (subject: string, width: number): string => {
return subject + ' '.repeat(width);
Expand All @@ -22,17 +25,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 (typeof subject !== 'string') {
throw new TypeError('Subject parameter value must be a string.');
}
Expand All @@ -47,14 +44,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 (typeof alignment !== 'string') {
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
4 changes: 2 additions & 2 deletions src/validateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type {

export default (schemaId: 'config.json'|'streamConfig.json', config: TableUserConfig): void => {
const validate = validators[schemaId] as ValidateFunction;
if (!validate(config)) {
const errors = validate.errors?.map((error: ErrorObject) => {
if (!validate(config) && validate.errors) {
const errors = validate.errors.map((error: ErrorObject) => {
return {
message: error.message,
params: error.params,
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