@bemoje/table
v2.0.0
Published
Table formatting and string processing utilities.
Downloads
609
Maintainers
Readme
@mono/table
Table formatting and string processing utilities.
Exports
- TableFormatter: Formats a 2D array representing a table.
- formatAsStringTable: Formats an array of objects into a string table with customizable column formatters.
- getHeadersFromCsvFile: Extracts column headers from the first line of a CSV file.
- iterateTableArrayAsObjects: Generator that iterates through a 2D table array, yielding objects with header keys and row values.
- objectsToTable: Convert an array of objects to a table.
- parseCsvHeaderLine: Takes the first line of a CSV string and returns an array of column names.
Usage
Table Formatting
import { formatAsStringTable, TableFormatter } from '@mono/table'
const data = [
{ name: 'Alice', age: 30, active: true },
{ name: 'Bob', age: 25, active: false },
]
const result = formatAsStringTable(data, {
keys: ['name', 'age'],
formatters: [{ key: 'name', format: (val) => val.toUpperCase() }],
})Table Iteration & Data Conversion
import { objectsToTable, iterateTableArrayAsObjects } from '@mono/table'
// Convert array of objects to a 2D array representation
const tableArray = objectsToTable([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
])
// => [['id', 'name'], [1, 'Alice'], [2, 'Bob']]
// Iterate over table array representing rows as objects
const iter = iterateTableArrayAsObjects(tableArray)
for (const obj of iter) {
console.log(obj)
}CSV Headers
import { getHeadersFromCsvFile, parseCsvHeaderLine } from '@mono/table'
// Extract headers from a CSV line
const headers = parseCsvHeaderLine('id,name,age', ',')
// => ['id', 'name', 'age']
// Extract headers directly from a CSV file
const fileHeaders = await getHeadersFromCsvFile('data.csv', ',')