Skip to content

Commit

Permalink
fix(tables): make number of dashes to match header length
Browse files Browse the repository at this point in the history
  • Loading branch information
syroegkin committed Dec 24, 2022
1 parent 5748eea commit 73b6182
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/lib/markdown/mdtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,18 @@ export class MDtable {
const result: string[] = [];
const columns = this._th.length;

if (this._th.length) {
if (columns) {
const th = `| ${this._th.map(
(d: MDstring | string) => (typeof d === 'string' ? d : d.get()),
).join(' | ')} |`;
result.push(th);

const thead = `| ${[...Array(columns).keys()].map(() => '---').join(' | ')} |`;
const thead = `| ${
this._th.map(
(el: string | MDstring) => '-'.repeat(el.length > 3 ? el.length : 3)
,
).join(' | ')
} |`;
result.push(thead);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/markdown/mdtable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('MDtable', () => {
.td('td1')
.td('td2')
.td(MDstring.string('td3').strikethrough());
const expected = '| h1 | h2 | **h3** |\n| --- | --- | --- |\n'
const expected = '| h1 | h2 | **h3** |\n| --- | --- | ------ |\n'
+ '| td1 | td2 | ~~td3~~ |';
const result = table.get();
expect(result).to.be.equal(expected);
Expand Down
22 changes: 11 additions & 11 deletions tests/transformers/pathParameters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from 'chai';
import { transformParameters } from '../../src/transformers/pathParameters';

const tableFixture = [
const tableFixture: string[] = [
'##### Parameters',
'| Name | Located in | Description | Required | Schema |',
'| ---- | ---------- | ----------- | -------- | ---- |',
'| ---- | ---------- | ----------- | -------- | ------ |',
];

describe('Path parameters transformer', () => {
Expand All @@ -27,13 +27,13 @@ describe('Path parameters transformer', () => {
}, {},
];

const results = [].concat(tableFixture, [
const results = [...tableFixture, ...[
'| name | formData | name | No | string |',
'| year | formData | year | Yes | string |',
'| | formData | | No | string |',
'| | | | No | |',
]);
const res = transformParameters(fixture).split('\n');
]];
const res = (transformParameters(fixture as any) as string).split('\n');

it('Should create parameters header', () => {
expect(res[0]).to.be.equal(results[0]);
Expand All @@ -59,10 +59,10 @@ describe('Path parameters transformer', () => {
description: 'name',
type: 'string',
}];
const results = [].concat(tableFixture, [
const results = [...tableFixture, ...[
'| name | formData | name | No | string |',
]);
const res = transformParameters(undefined, fixture).split('\n');
]];
const res = (transformParameters(undefined as any, fixture) as string).split('\n');

it('Should create parameters header', () => {
expect(res[0]).to.be.equal(results[0]);
Expand Down Expand Up @@ -93,11 +93,11 @@ describe('Path parameters transformer', () => {
description: 'name',
type: 'string',
}];
const results = [].concat(tableFixture, [
const results = [...tableFixture, ...[
'| path name | formData | name | No | string |',
'| method name | formData | name | No | string |',
]);
const res = transformParameters(methodFixture, pathFixture).split('\n');
]];
const res = (transformParameters(methodFixture, pathFixture) as string).split('\n');
it('Should create parameters header', () => {
expect(res[0]).to.be.equal(results[0]);
});
Expand Down
8 changes: 5 additions & 3 deletions tests/transformers/securityDefinitions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('Security definitions', () => {
const result = '### Security\n'
+ '**auth** \n\n'
+ `| ${type} | *${typeResolver[type]}* |\n`
+ '| --- | --- |\n\n\n';
expect(result).to.be.equal(res);
+ `| ${'-'.repeat(type.length)} | ${'-'.repeat(2 + typeResolver[type].length)} |\n\n\n`;
expect(res).to.be.equal(result);
});
});
it('Should resolve names', () => {
Expand Down Expand Up @@ -50,7 +50,9 @@ describe('Security definitions', () => {
'unknown-key': 'Uknown key',
},
};
const result = transformSecurityDefinitions(fixture as OpenAPIV2.SecurityDefinitionsObject);
const result = transformSecurityDefinitions(
fixture as OpenAPIV2.SecurityDefinitionsObject,
) as string;
expect(result.match(/undefined/ig)).to.be.null;
expect(result.match(/x-special-key/ig)).to.be.not.null;
});
Expand Down

0 comments on commit 73b6182

Please sign in to comment.