diff --git a/src/alignString.ts b/src/alignString.ts index aaa5421..100448a 100644 --- a/src/alignString.ts +++ b/src/alignString.ts @@ -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); @@ -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.'); } @@ -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); } diff --git a/src/validateConfig.ts b/src/validateConfig.ts index 9c4b637..52eeccf 100644 --- a/src/validateConfig.ts +++ b/src/validateConfig.ts @@ -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, diff --git a/test/alignString.ts b/test/alignString.ts index 47e01fc..769f120 100644 --- a/test/alignString.ts +++ b/test/alignString.ts @@ -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', () => { diff --git a/test/configSamples.ts b/test/configSamples.ts index 4b3f7ed..5c8bc65 100644 --- a/test/configSamples.ts +++ b/test/configSamples.ts @@ -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'}}}, @@ -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'}}, @@ -45,6 +47,7 @@ const configSamples: {invalid: unknown[], valid: TableUserConfig[], } = { {unknown: 1}, ], valid: [ + {}, { columns: { 0: { @@ -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: '-'}}, diff --git a/test/streamConfigSamples.ts b/test/streamConfigSamples.ts index b9223e3..2ab0fd7 100644 --- a/test/streamConfigSamples.ts +++ b/test/streamConfigSamples.ts @@ -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 @@ -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: {