A basic, explicit CSV parsing and formatting library for use in node.js. CSV strings are parsed and formatted using a set of column definitions for converting to and from CSV.
Used for converting data to and from CSV.
Converts an array of objects to a CSV string.
-
rows
Array<Object> Array of objects to form rows from. -
columns
Array<Column> An array containing columns. -
options
Object Parsing options. (optional, default{}
)
toCSV([
{
name: 'Cat Chow',
price: 529
}
], [
{
header: 'Name',
key: 'name'
},
{
header: 'Price',
key: 'price',
converter: (value) => value / 100
}
], {
includeHeader: true
});
// "Name","Price"\n"Cat Chow",5.29
Returns String CSV string.
Converts a CSV string into objects.
-
csvStr
String CSV string. -
columns
Array<Column> An array containing columns. (optional, default[]
) -
options
Object Parsing options. (optional, default{}
)
fromCSV('"Name","Price"\n"Cat Chow",5.29', [
{
header: 'Name',
key: 'name'
},
{
header: 'Price',
key: 'price',
parser: (value) => Math.round(parseFloat(value) * 100)
}
], {
includeHeader: true
});
// { name: 'Cat Chow', price: 529 }
// using no column definitions
// there will be no type conversions if no parser is given, any returned values will be strings
fromCSV('"Cat Chow",5.29');
// { col1: 'Cat Chow', col2: '5.29' }
// using no column definitions (use header as keys)
fromCSV('"Name","Price"\n"Cat Chow",5.29', null, {
includeHeader: true
});
// { Name: 'Cat Chow', Price: '5.29' }
Returns Array<Object> Array of objects.
Gets the cell label.
cellLabel(0, 3); // A3
Returns String Cell label.
An object describing the format of a column.
Type: Object
-
header
String? The header to be used for this column. -
key
String? The object key for this column. -
required
Boolean? Indicates whether the value should be defined when getting value from CSV. Throws an error if the resulting value is null, undefined, or an empty string. -
converter
Converter? The function called to convert value to CSV. -
parser
Parser? The function called to parse value from CSV. Will not be called unless parseEmpty is set to true. -
parseEmpty
Boolean? Whether to parse empty values or not.
Function to convert value to raw CSV.
Type: Function
-
value
String? Value from object. -
details
ConverterDetails? Details of cell.
Returns (String | Number | null | undefined) Value of cell.
Converter details
Type: Object
-
obj
Object? Source object. -
key
String? The key of the value we want to take from the object for this column. -
row
Number? Row number. -
column
Number? Column number.
Function to parse value from raw CSV.
Type: Function
-
value
String? Value of cell. -
details
ParserDetails? Details of cell.
Returns any Parsed value from CSV string.
Parser details
Type: Object
MIT