Skip to content

Commit

Permalink
Consistency schema and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nam Hoang Le committed Apr 26, 2021
1 parent 8148dde commit f06e203
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 81 deletions.
4 changes: 0 additions & 4 deletions src/calculateColumnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import type {
* Produces an array of values that describe the largest value length (width) in every column.
*/
export default (rows: Row[]): number[] => {
if (!rows[0]) {
throw new Error('Dataset must have at least one row.');
}

const columnWidths = new Array(rows[0].length).fill(0);

rows.forEach((row) => {
Expand Down
6 changes: 1 addition & 5 deletions src/makeStreamConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ export default (userConfig: StreamUserConfig): StreamConfig => {

const config = cloneDeep(userConfig);

if (!config.columnDefault || !config.columnDefault.width) {
if (config.columnDefault.width === undefined) {
throw new Error('Must provide config.columnDefault.width when creating a stream.');
}

if (!config.columnCount) {
throw new Error('Must provide config.columnCount.');
}

return {
...config,
border: makeBorder(config.border),
Expand Down
9 changes: 6 additions & 3 deletions src/schemas/shared.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
]
},
"width": {
"type": "number",
"minimum": 1,
"multipleOf": 1.0
"$ref": "#/definitions/naturalNumber"
},
"wrapWord": {
"type": "boolean"
Expand Down Expand Up @@ -105,6 +103,11 @@
},
"border": {
"type": "string"
},
"naturalNumber": {
"type": "number",
"minimum": 1,
"multipleOf": 1.0
}
}
}
7 changes: 4 additions & 3 deletions src/schemas/streamConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"$ref": "shared.json#/definitions/column"
},
"columnCount": {
"type": "number"
"$ref": "shared.json#/definitions/naturalNumber"
},
"drawVerticalLine": {
"typeof": "function"
"typeof": "function"
}
},
"additionalProperties": false
"required": ["columnDefault", "columnCount"],
"additionalProperties": false
}
5 changes: 0 additions & 5 deletions test/calculateColumnWidths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import chalk from 'chalk';
import calculateColumnWidths from '../src/calculateColumnWidths';

describe('calculateColumnWidths', () => {
it('throws an error when attempting to calculate maximum column value index for an empty data set', () => {
expect(() => {
calculateColumnWidths([]);
}).to.throw(Error, 'Dataset must have at least one row.');
});
it('calculates the maximum column value index', () => {
const columnWidths = calculateColumnWidths([
[
Expand Down
14 changes: 3 additions & 11 deletions test/createStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@ import getBorderCharacters from '../src/getBorderCharacters';

describe('createStream', () => {
context('"config.columnDefault.width" property is not provided', () => {
it('throws an error', () => {
expect(() => {
createStream({} as never);
}).to.throw(Error, 'Must provide config.columnDefault.width when creating a stream.');
});
});
context('"config.columnCount" property is not provided', () => {
it('throws an error', () => {
expect(() => {
createStream({
columnDefault: {
width: 10,
},
columnCount: 1,
columnDefault: {},
} as never);
}).to.throw(Error, 'Must provide config.columnCount.');
}).to.throw(Error, 'Must provide config.columnDefault.width when creating a stream.');
});
});
context('Table data cell count does not match the columnCount.', () => {
Expand Down
20 changes: 1 addition & 19 deletions test/makeStreamConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ describe('makeStreamConfig', () => {
});

context('columnDefault', () => {
context('is not provided', () => {
it('throws an error', () => {
expect(() => {
return makeStreamConfig({columnCount: 2} as never);
}).to.be.throw('Must provide config.columnDefault.width when creating a stream.');
});
});

context('is provided but not contains width', () => {
context('not contains width', () => {
it('throws an error', () => {
expect(() => {
return makeStreamConfig({columnCount: 2,
Expand All @@ -41,16 +33,6 @@ describe('makeStreamConfig', () => {
});
});

context('columnCount', () => {
context('is not provided', () => {
it('throws an error', () => {
expect(() => {
return makeStreamConfig({columnDefault: {width: 5}} as never);
}).to.be.throw('Must provide config.columnCount.');
});
});
});

context('column', () => {
context('alignment', () => {
context('is not provided', () => {
Expand Down
59 changes: 28 additions & 31 deletions test/streamConfigSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {

const streamConfigSamples: {invalid: unknown[], valid: StreamUserConfig[], } = {
invalid: [
{},
{border: 1},
{border: {unknown: '-'}},
{border: {topBody: 1}},
Expand Down Expand Up @@ -97,39 +98,35 @@ const streamConfigSamples: {invalid: unknown[], valid: StreamUserConfig[], } = {
columnDefault: {width: 1.5},
},

// eslint-disable-next-line no-warning-comments
// TODO: Fix the schema so that the following configs are truly invalid
// {border: {topBody: '-'}},
// {border: {topJoin: '-'}},
// {border: {topLeft: '-'}},
// {border: {topRight: '-'}},
// {border: {bottomBody: '-'}},
// {border: {bottomJoin: '-'}},
// {border: {bottomLeft: '-'}},
// {border: {bottomRight: '-'}},
// {border: {bodyLeft: '-'}},
// {border: {bodyRight: '-'}},
// {border: {bodyJoin: '-'}},
// {border: {joinBody: '-'}},
// {border: {joinLeft: '-'}},
// {border: {joinRight: '-'}},
// {border: {joinJoin: '-'}},
// {columns: {1: {alignment: 'left'}}},
// {columns: {1: {width: 5}}},
// {columns: {1: {wrapWord: true}}},
// {columns: {1: {truncate: 1}}},
// {columns: {1: {paddingLeft: 1}}},
// {columns: {1: {paddingRight: 1}}},
// {columnDefault: {alignment: 'left'}},
// {columnDefault: {width: 5}},
// {columnDefault: {wrapWord: true}},
// {columnDefault: {truncate: 1}},
// {columnDefault: {paddingLeft: 1}},
// {columnDefault: {paddingRight: 1}},
{border: {topBody: '-'}},
{border: {topJoin: '-'}},
{border: {topLeft: '-'}},
{border: {topRight: '-'}},
{border: {bottomBody: '-'}},
{border: {bottomJoin: '-'}},
{border: {bottomLeft: '-'}},
{border: {bottomRight: '-'}},
{border: {bodyLeft: '-'}},
{border: {bodyRight: '-'}},
{border: {bodyJoin: '-'}},
{border: {joinBody: '-'}},
{border: {joinLeft: '-'}},
{border: {joinRight: '-'}},
{border: {joinJoin: '-'}},
{columns: {1: {alignment: 'left'}}},
{columns: {1: {width: 5}}},
{columns: {1: {wrapWord: true}}},
{columns: {1: {truncate: 1}}},
{columns: {1: {paddingLeft: 1}}},
{columns: {1: {paddingRight: 1}}},
{columnDefault: {alignment: 'left'}},
{columnDefault: {width: 5}},
{columnDefault: {wrapWord: true}},
{columnDefault: {truncate: 1}},
{columnDefault: {paddingLeft: 1}},
{columnDefault: {paddingRight: 1}},
],
valid: [
// FIXME: The empty option pass the validator but be threw in runtime
{} as never,
{
columnCount: 1,
columnDefault: {width: 1},
Expand Down

0 comments on commit f06e203

Please sign in to comment.