1.0.18 • Published 9 months ago

@dborysov/md-table v1.0.18

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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: 'john.doe@example.com',
    middleName: 'SomeMiddleName',
    phoneNumber: '123-456-78-910',
  },
  {
    id: 2,
    name: 'Jane',
    middleName: 'SomeOtherMiddleName',
    lastName: 'Doe',
    email: 'jane.doe@example.com',
    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      | john.doe@example.com | 123-456-78-910 |
  | 2   | Jane | SomeOtherMiddleName | Doe      | jane.doe@example.com | 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       | john.doe@example.com | jane.doe@example.com |
  | 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>[]

1.0.18

9 months ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago