Skip to content

Commit

Permalink
feat: remove unreachable width assertions (#161)
Browse files Browse the repository at this point in the history
* Remove unreachable width assertions

* Remove unreachable width assertions

* Revert width assertion in stream config

Co-authored-by: Nam Hoang Le <nam.hoang.le@mgm-tp.com>
  • Loading branch information
nam-hle and nhle-mgmtp authored Apr 23, 2021
1 parent 2728d70 commit 07aca6a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 50 deletions.
4 changes: 0 additions & 4 deletions src/alignString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ export default (subject: string, containerWidth: number, alignment: ColumnUserCo
throw new TypeError('Subject parameter value must be a string.');
}

if (typeof containerWidth !== 'number') {
throw new TypeError('Container width parameter value must be a number.');
}

const subjectWidth = stringWidth(subject);

if (subjectWidth > containerWidth) {
Expand Down
4 changes: 0 additions & 4 deletions src/calculateCellHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ export default (value: string, columnWidth: number, useWrapWord = false): number
throw new TypeError('Value must be a string.');
}

if (!Number.isInteger(columnWidth) || columnWidth < 1) {
throw new TypeError('Column width must be an integer greater than 0.');
}

return wrapCell(value, columnWidth, useWrapWord).length;
};
4 changes: 0 additions & 4 deletions src/calculateRowHeightIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export default (rows: Row[], config: BaseConfig): number[] => {
const cellHeightIndex = new Array(tableWidth).fill(1);

cells.forEach((value, index1) => {
if (typeof config.columns[index1].width !== 'number') {
throw new TypeError('column[index].width must be a number.');
}

cellHeightIndex[index1] = calculateCellHeight(value, config.columns[index1].width, config.columns[index1].wrapWord);
});

Expand Down
4 changes: 3 additions & 1 deletion src/schemas/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
]
},
"width": {
"type": "number"
"type": "number",
"minimum": 1,
"multipleOf": 1.0
},
"wrapWord": {
"type": "boolean"
Expand Down
8 changes: 1 addition & 7 deletions test/alignString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ describe('alignString', () => {
}).to.throw(Error, 'Subject parameter value must be a string.');
});
});
context('container width parameter value is not a string', () => {
it('throws an error', () => {
expect(() => {
alignString('aa', 'aa' as never, 'left');
}).to.throw(Error, 'Container width parameter value must be a number.');
});
});

context('subject parameter value width is greater than the container width', () => {
it('throws an error', () => {
expect(() => {
Expand Down
14 changes: 0 additions & 14 deletions test/calculateCellHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ describe('calculateCellHeight', () => {
});
});
describe('context width', () => {
context('is not an integer', () => {
it('throws an error', () => {
expect(() => {
calculateCellHeight('foo', 1.5);
}).to.throw(Error, 'Column width must be an integer greater than 0.');
});
});
context('is 0', () => {
it('throws an error', () => {
expect(() => {
calculateCellHeight('foo', 0);
}).to.throw(Error, 'Column width must be an integer greater than 0.');
});
});
context('is lesser than the column width', () => {
it('has height 1', () => {
expect(calculateCellHeight('foo', 10)).to.equal(1);
Expand Down
16 changes: 0 additions & 16 deletions test/calculateRowHeightIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ import calculateRowHeightIndex from '../src/calculateRowHeightIndex';
import makeConfig from '../src/makeConfig';

describe('calculateRowHeightIndex', () => {
context('invalid column width', () => {
it('throws an TypeError', () => {
expect(() => {
return calculateRowHeightIndex([['a']], {
columns: {
0: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
width: '3',
},
},
});
}).to.be.throw(TypeError, 'column[index].width must be a number.');
});
});

context('single column', () => {
context('cell content width is lesser than column width', () => {
it('is equal to 1', () => {
Expand Down
6 changes: 6 additions & 0 deletions test/configSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
{columns: {1: {alignment: 1}}},
{columns: {1: {alignment: '1'}}},
{columns: {0: {alignment: 'middle'}}},
{columns: {1: {width: 0}}},
{columns: {1: {width: 1.5}}},
{columns: {1: {width: '5'}}},
{columns: {1: {wrapWord: 1}}},
{columns: {1: {wrapWord: 'true'}}},
Expand All @@ -39,6 +41,8 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
{columnDefault: {alignment: 1}},
{columnDefault: {alignment: '1'}},
{columnDefault: {alignment: 'middle'}},
{columnDefault: {width: 0}},
{columnDefault: {width: 1.5}},
{columnDefault: {width: '5'}},
{columnDefault: {wrapWord: 1}},
{columnDefault: {wrapWord: 'true'}},
Expand Down Expand Up @@ -145,13 +149,15 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = {
{border: {joinRight: '-'}},
{border: {joinJoin: '-'}},
{columns: {1: {alignment: 'left'}}},
{columns: {1: {width: 1}}},
{columns: {1: {width: 5}}},
{columns: {1: {wrapWord: true}}},
{columns: {1: {wrapWord: false}}},
{columns: {1: {truncate: 1}}},
{columns: {1: {paddingLeft: 1}}},
{columns: {1: {paddingRight: 1}}},
{columnDefault: {alignment: 'left'}},
{columnDefault: {width: 1}},
{columnDefault: {width: 5}},
{columnDefault: {wrapWord: true}},
{columnDefault: {wrapWord: false}},
Expand Down
34 changes: 34 additions & 0 deletions test/streamConfigSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ const streamConfigSamples: {invalid: unknown[], valid: StreamUserConfig[], } = {
columnDefault: {width: 20},
columns: {0: {wrapWord: 1}},
},
{
columnCount: 1,
columnDefault: {width: 2},
columns: {0: {width: 0}},
},
{
columnCount: 1,
columnDefault: {width: 2},
columns: {0: {width: 1.5}},
},
{
columnCount: 1,
columnDefault: {width: 0},
},
{
columnCount: 1,
columnDefault: {width: 1.5},
},

// eslint-disable-next-line no-warning-comments
// TODO: Fix the schema so that the following configs are truly invalid
Expand Down Expand Up @@ -95,10 +113,26 @@ const streamConfigSamples: {invalid: unknown[], valid: StreamUserConfig[], } = {
// {columnDefault: {paddingRight: 1}},
],
valid: [
// FIXME: The empty option pass the validator but be threw in runtime
{} as never,
{
columnCount: 1,
columnDefault: {width: 1},
},
{
columnCount: 3,
columnDefault: {width: 20},
},
{
columnCount: 1,
columnDefault: {width: 1},
columns: {0: {width: 1}},
},
{
columnCount: 1,
columnDefault: {width: 5},
columns: {0: {width: 5}},
},
{
columnCount: 3,
columnDefault: {width: 20},
Expand Down

0 comments on commit 07aca6a

Please sign in to comment.