https://github.com/cameronhunter/table
A tagged template for defining a table of data.
https://github.com/cameronhunter/table
Last synced: about 1 month ago
JSON representation
A tagged template for defining a table of data.
- Host: GitHub
- URL: https://github.com/cameronhunter/table
- Owner: cameronhunter
- License: mit
- Created: 2021-11-05T21:43:58.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-11-08T19:10:27.000Z (over 4 years ago)
- Last Synced: 2025-09-29T05:20:48.810Z (9 months ago)
- Language: TypeScript
- Size: 144 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Table  [](https://npmjs.com/package/@cameronhunter/table)
Create an array of objects from a table of data defined in a template string.
## Runtime
The default import will convert the table into an array of objects at runtime.
```ts
import table from '@cameronhunter/table';
type Data = {
cell: number;
presentation: 'slideshow' | 'static';
preloading: boolean;
};
const data: Data[] = table`
cell | presentation | preloading
${2} | ${'slideshow'} | ${true}
${3} | ${'static'} | ${true}
${4} | ${'slideshow'} | ${false}
${5} | ${'static'} | ${false}
`;
```
## Babel Macro
The babel macro will convert the table into an array of objects at build time.
The usage is the same as the runtime function, just a different import.
```ts
import table from '@cameronhunter/table/macro';
type Data = {
cell: number;
presentation: 'slideshow' | 'static';
preloading: boolean;
};
const data: Data[] = table`
cell | presentation | preloading
${2} | ${'slideshow'} | ${true}
${3} | ${'static'} | ${true}
${4} | ${'slideshow'} | ${false}
${5} | ${'static'} | ${false}
`;
```
↓ ↓ ↓ ↓ ↓ ↓
```ts
type Data = {
cell: number;
presentation: 'slideshow' | 'static';
preloading: boolean;
};
const data: Data[] = [
{
cell: 2,
presentation: 'slideshow',
preloading: true
},
{
cell: 3,
presentation: 'static',
preloading: true
},
{
cell: 4,
presentation: 'slideshow',
preloading: false
},
{
cell: 5,
presentation: 'static',
preloading: false
}
];
```