@dborysov/md-table
v1.0.18
Published
Parse md tables
Downloads
33
Readme
md-table
Demo
Install
yarn add @dborysov/md-table
# or
npm i -S @dborysov/md-table
Motivation
Of course, you can use plain old (java|type)script to create an array of objects:
interface User {
id: number;
name: string;
phoneNumber: string;
lastName: string;
email: string;
}
const users: User[] = [
{
id: 1,
name: 'John',
lastName: 'Doe',
email: '[email protected]',
middleName: 'SomeMiddleName',
phoneNumber: '123-456-78-910',
},
{
id: 2,
name: 'Jane',
middleName: 'SomeOtherMiddleName',
lastName: 'Doe',
email: '[email protected]',
phoneNumber: '56-47-78-910',
},
];
But it can be done in a more readable way:
import { parseTable } from '@dborysov/md-table';
const users = parseTable(`
| id | name | middleName | lastName | email | phoneNumber |
| --- | ---- | ------------------- | -------- | -------------------- | -------------- |
| 1 | John | SomeMiddleName | Doe | [email protected] | 123-456-78-910 |
| 2 | Jane | SomeOtherMiddleName | Doe | [email protected] | 56-47-78-910 |
`);
If the table is getting wider, you can transpose it. No delimiter is needed in that case:
import { parseTable } from '@dborysov/md-table';
const users = parseTable(
`
| id | 1 | 2 |
| name | John | Jane |
| middleName | SomeMiddleName | SomeOtherMiddleName |
| lastName | Doe | Doe |
| email | [email protected] | [email protected] |
| phoneNumber | 123-456-78-910 | 56-47-78-910 |
`,
{ transpose: true },
);
Yeah, but typings?..
I love typescript for so many things, for example for working with string literals. In the previous examples the type of users
is Record<"id" | "name" | "middleName" | "lastName" | "email" | "phoneNumber", string>[]