npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

template-literal-table

v4.0.2

Published

Tagged template literal mapping markdown style tables into objects

Downloads

5,092

Readme

Template literal - Table

Tagged template literal mapping markdown style tables into objects.

Inspired by the Jest test.each syntax.

Installation

$ npm install --save template-literal-table

Exports

The following functions are exported

| name | description | | --------- | --------------------------------------------------------------------------------------------------------------------- | | table | Table parser, skips divider rows (cell only containing two or more - characters) and rows consisting of empty cells | | empty | Table parser, skips divider rows, preserves rows consisting of empty cells | | create | Table parser creator, creates a new partser function with custom row filter functions | | mapper | Table parser creator, creates a new partser function which maps specific property values |

Usage

The template literal syntax allows for a lot of flexibility, as any type of value can be formatted without losing the actual value by providing the ${value} syntax. For fixed strings, you don't have to use the placeholder syntax if you don't care about its type (e.g. you cast it "manually" afterwards or its type is string anyway, which is what will be provided as).

const { table } = require('template-literal-table');

const records = table`
	foo  | bar  | baz 
	-----|------|-----
	${1} | ${2} | ${4}
	${2} | ${4} | ${8}
	4    | 8    | 16  
`;

//  records = [
//    { foo: 1, bar: 2, baz: 4},
//    { foo: 2, bar: 4, baz: 8},
//    { foo: '4', bar: '8', baz: '16'},
//  ]

API

table

The table function is the recommended use, as it parses table structures as most likely intended

import { table } from('template-literal-table');

const records = table`
	foo  | bar   | baz
	-----|-------|-----
	     |
	one
	     | two
	     |       | three
	${1} | ${2}  | ${4}
	${2} | ${4}  | ${8}
	4    | 8     | 16
`;

//  records = [
//    { foo: 'one', bar: undefined, baz: undefined},
//    { foo: undefined, bar: 'two', baz: undefined},
//    { foo: undefined, bar: undefined, baz: 'three'},
//    { foo: 1, bar: 2, baz: 4},
//    { foo: 2, bar: 4, baz: 8},
//    { foo: '4', bar: '8', baz: '16'},
//  ]

empty

Like the table function, with the difference that empty lines will be preserved, do note that any newline in the table structure will become a new "row", regardless of the intention. This means that a trailing newline (as show below) will also result in a record with undefined values.

import { empty } from('template-literal-table');

const records = empty`
	foo  | bar   | baz
	-----|-------|-----
	     |
	one
	     | two
	     |       | three
	${1} | ${2}  | ${4}
	${2} | ${4}  | ${8}
	4    | 8     | 16
`;

//  records = [
//    { foo: undefined, bar: undefined, baz: undefined},
//    { foo: 'one', bar: undefined, baz: undefined},
//    { foo: undefined, bar: 'two', baz: undefined},
//    { foo: undefined, bar: undefined, baz: 'three'},
//    { foo: 1, bar: 2, baz: 4},
//    { foo: 2, bar: 4, baz: 8},
//    { foo: '4', bar: '8', baz: '16'},
//    { foo: undefined, bar: undefined, baz: undefined},
//  ]

create

The table and empty function should cover most scenarios, though sometimes one needs different filters to be applied to the records passed in. For this purpose the create function exists, it allows any number of filters to be specified, which will be applied before the records are created from the values, meaning the filter functions will receive all values as argument.

The filter function signature is (...cells: Array<unknown>) => boolean

import { create } from 'template-literal-table';

// if the fourth column contains a value that is not a divider we want it to be present
const fourth = create((...values: Array<unknown>) => Boolean(values[3]) && !/^--+$/.test(String(values[3])) );
const records = fourth`
 | one | two | three | four |
 | --- | --- | ----- | ---- |
 | 1   | 2   | 3     | 4    |
 | 1   | 2   | 3     |
 | 1   | 2   |       | 4    |
 | 1   |     | 3     | 4    |
 | 2   | 3   | 4     |
`;

//  records = [
//    { one: '1', two: '2', three: '3', four: '4' },
//    { one: '1', two: '2', three: undefined, four: '4' },
//    { one: '1', two: undefined, three: '3', four: '4' },
//    { one: undefined, two: '2', three: '3', four: '4' },
//  ]

mapper

It can be convenient to simplify a table and then mapping the values into a specific type or format. For this purpose the mapper function exists, it receives a mapper object defining all the properties with a mapping function as value. The mappings will be applied after the records are created from the values, meaning the mapping functions will receive only the value of records that were preserved during filtering.

import { mapper } from 'template-literal-table';

// define the mapping
const mapping = {
	// we can skip the string conversion, as all values are strings by default
	number: (value) => value.length ? Number(value): undefined,
	boolean: (value) => value === 'yes',
	array: (value) => value.split(/\s*,\s*/),
};
const mapped = mapper();
const records = mapped`
 string | number | boolean | array  
 ------ | ------ | ------- | -------
 foo    | 1      | yes     | a, b   
 bar    |        | no      | b, c, d
 baz    | 7      |         |        
 `;

//  records = [
//    { string: 'foo', number: 1, boolean: true, array: ['a', 'b'] },
//    { string: 'bar', number: undefined, boolean: false, array: ['b', 'c', 'd'] },
//    { string: 'baz', number: 7, boolean: false, array: [] },
//  ]

Filters

Filters provided to the create function are applied after the "cell" values have been normalized and before the values are turned into the record objects.

This means that every cells' value has been trimmed of whitespace and the raw values are restored or merged if there's more than one. This process consists of the following steps:

  • remove trailing and leading space from the cell
  • restore the original value
  • if there's more than one value in a cell, merge it (this will always result in a string value)

In the table below some samples are shown:

 input         | typeof    | output             
---------------|-----------|--------------------
 1             | string    | (string) `'1'`     
 ${1}          | number    | (number) `1`       
---------------|-----------|--------------------
 true          | string    | (string) `'true'`  
 ${true}       | boolean   | (boolean) `true`   
 false         | string    | (string) `'false'` 
 ${false}      | boolean   | (boolean) `false`  
---------------|-----------|--------------------
 1 2           | string    | (string) `'1 2'`   
 ${1} 2        | string    | (string) `'1 2'`   
 1 ${2}        | string    | (string) `'1 2'`   
 ${1} ${2}     | string    | (string) `'1 2'`   

Styles

Tables in Markdown style come in different style, currently we support the following styles. Keep in mind that the cells of the header divider row have to be two characters minimum (e.g. --, :-, -:), this is to allow for cells to contain only - (which you could interpret as an explicit form of N/A)

The most basic format, only the bare essentials

key1|key2|key3
value1|value2|value3

Slightly improved readability using some more spacing around column separators

key1 | key2 | key3
value1 | value2 | value3

As that doesn't really provide more readability, this format is preferred

key1   | key2   | key3
-------|--------|--------
value1 | value2 | value3

A common format provides a more table-like look and feel

| key1   | key2   | key3   |
|--------|--------|--------|
| value1 | value2 | value3 |

Some like to have more clear columns

| key1   | key2   | key3   |
| ------ | ------ | ------ |
| value1 | value2 | value3 |

Although ignored by the table-tag, alignment indicators are supported in the header divider

key1   | key2   | key3
------:|:------:|:------
value1 | value2 | value3

Can be used with spacing around the column separators

key1   | key2   | key3
-----: | :----: | :-----
value1 | value2 | value3

Also works in combination with borders

| key1   | key2   | key3   |
|-------:|:------:|:-------|
| value1 | value2 | value3 |

And with both borders and spacing around column separators

| key1   | key2   | key3   |
| -----: | :----: | :----- |
| value1 | value2 | value3 |

Tips

Prettier

This tip was suggested by @LucasSegersFabro If you want prettier to format your tables automatically, you can trick it into thinking it is looking at the Jest each syntax (which unfortunatly a hardcoded pattern within prettier).

// for formatting purposes
const it = {
  each: table,
};

const test = suite('Common processor tests');

const Table = it.each`
  your   | table | here
  -------|-------|------
  thanks | to    | @LucasSegersFabro
`;

Escaping the pipe character (|)

Besides the general character escaping rules in JavaScript/TypeScript strings, sometimes you really want a pipe symbol without resorting to a value (${'|'}), this can be done by adding two backslashes before a pipe character: \\|, it is actively enforced not to be interpreted as column separator.

License

MIT License Copyright (c) 2018-2021 Rogier Spieker

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.