mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-21 09:16:41 +02:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
/*
|
|
Used to convert jest's Tagged Template literals to object arrays as required by vitest.
|
|
|
|
Example:
|
|
|
|
Jest code
|
|
```ts
|
|
it.each`
|
|
str | expected
|
|
${'1d'} | ${moment.duration(1, 'd')}
|
|
${'2w'} | ${moment.duration(2, 'w')}
|
|
`('should parse $str to $expected duration', ({ str, expected }) => {
|
|
expect(yourFunction(str)).toEqual(expected);
|
|
});
|
|
```
|
|
|
|
Vitest code
|
|
```ts
|
|
it.each(convert`
|
|
str | expected
|
|
${'1d'} | ${moment.duration(1, 'd')}
|
|
${'2w'} | ${moment.duration(2, 'w')}
|
|
`)('should parse $str to $expected duration', ({ str, expected }) => {
|
|
expect(yourFunction(str)).toEqual(expected);
|
|
});
|
|
```
|
|
*/
|
|
|
|
export const convert = (template: TemplateStringsArray, ...params: any[]) => {
|
|
const header = template[0]
|
|
.trim()
|
|
.split('|')
|
|
.map((s) => s.trim());
|
|
if (header.length === 0 || params.length % header.length !== 0) {
|
|
throw new Error('Table column count mismatch');
|
|
}
|
|
const chunkSize = header.length;
|
|
const out = [];
|
|
for (let i = 0; i < params.length; i += chunkSize) {
|
|
const chunk = params.slice(i, i + chunkSize);
|
|
out.push(Object.fromEntries(chunk.map((v, i) => [header[i], v])));
|
|
}
|
|
return out;
|
|
};
|