CSV

Read and write CSV data in JavaScript.
IMPORTANT: This is an ESM only package.

Installation

Contents

Introduction

Writing and reading CSV data is a common task performed by software developers. CSV files have been used for exporting and importing data for a very long time. It is not used as much these days, but you will still find it cropping up in modern applications and is still one of the fallback methods of transferring data.

This library is used to take a list of records and write them into a CSV string. It can also take a CSV string and convert it into a list of records. Both writing and reading methods come with a number of options to allow you to handle the different CSV formatting.

In this example we are writing the list of records using the default formatting options.

In this example we are reading in CSV data to create a list of records using default formatting options.

CsvWriter

There is one static function for writing out a list records into a CSV string.

Write

Convert a list of records into CSV string.

Arguments

  • recordList - The list of records. This is an array of objects. Each object should contain the same set of properties, though, any that are missing will be written as an empty field or with NULL text (depending on which options you are using).
  • [options] - There are a number of different optional options available to help you format the CSV output.
    [newLine='\n'] - The character to use for each new line ('\r\n', '\r', '\n', etc).
    [separator=','] - The character to use for separating each field (';', ',', '\t', etc).
    [null=''] - The text to use when the field is null/undefined ('', 'NULL', 'N/A').
    [booleanTrue='TRUE'] - The text to use when the boolean field is true.
    [booleanFalse='FALSE'] - The text to use when the boolean field is false.
    [escapeCharacters=true] - Use back-slashes (e.g. \n, \t). If false then it uses the RFC 4180 double quote method.
    [quote='"'] - The character to use for quotes (single or double).
    [quoteAll=false] - Should all fields be placed inside "quotes".
    [quoteStrings=false] - Should all strings be placed inside "quotes", or only those that contain problem characters.
    [quoteEmptyStrings=false] - Should empty strings be placed inside "quotes".
    [quoteDates=false] - Should dates be put inside "quotes".
    [quoteHeaders=false] - Should header values be put inside "quotes".
    [dateFormat='ISO'] - The general format of the date and time.
    [header=true] - Should the header line be included.
    [headerList] - A list of header items that are used to control header information.
    headerList[].propertyName - The name of the record's field property.
    headerList[].csvName - The CSV name to add in the header line.
    [headerList[].defaultText] - If the field is missing then use this default text value.
    [headerList[].dateFormat] - The date format used by the header.
    [typeConvertCallback] - Function you can use to convert different records into text. typeCallback(record, header, value, options) = text.

Returns

Returns the formatted CSV text.

CsvReader

There is one static function for reading in CSV string and converting it into a list of records. By default it will try to workout what type each field is, but you can set them manually if required.

Read

Convert CSV text into a list of records.

Arguments

  • csv - The CSV text that will be converted into a list of records.
  • [options] - There are a number of different optional options available to help you control the reading process.
    [quote='"'] - The character to use for quotes (single or double).
    [separator=','] - The character to use for separating each field (';', ',', '\t', etc).
    [dateFormat='AUTO'] - How dates are generally formatted.
    [booleanTrue='AUTO'] - Converts fields with the given text as boolean TRUE fields.
    [booleanFalse='AUTO'] - Converts fields with the given text as boolean FALSE fields.
    [null=''] - Converts fields with the given text as null fields.
    [nullEmpty=true] - If a field is empty, should the property be set to "null" or just not added to the record.
    [skipEmptyLine=true] - If the line does not contain any fields then should it not be included.
    [escapeCharacters=true] - Using back-slashes (e.g. \n, \t). If false then it uses the RFC 4180 double quote method.
    [convertTypes=true] - Should the text fields be converted into types (numbers,boolean,dates), or keep everything as strings.
    [convertEscapeCharacters=true] - Are found escape characters converted or kept as they are.
    [header=true] - Is the first line the header or is it not included (if false, then headerList is required).
    [headerList] - A list of header items that are used to control header information.
    headerList[].propertyName - The name of the records property.
    headerList[].type - The name of the type of data. Can be either 'number', 'string', 'boolean' or 'date'.
    [headerList[].skip] - Should this field be added to each record, or left out.
    [headerList[].defaultValue] - If the field is empty then use this default value.
    [headerList[].dateFormat] - How the date is formatted (if type is 'date').
    [headerList[].booleanTrue] - How the boolean true is formatted (if type is 'boolean').
    [headerList[].booleanFalse] - How the boolean false is formatted (if type is 'boolean').
    [typeConvertCallback] - Function you can use to convert different CVS text into property values. typeConvertCallback(field, header, headerIndex options) = value.

Returns

Returns an array of objects. Each object is a record, a line within the CSV data, contain all the header fields, converted from strings into the related data types. If nothing (undefined) is returned then something went wrong.

Options

newLine

At the end of each CSV line there needs to be an "end of line" character. Normally this would be either \n (new line), \r (carriage return), or \r\n (both together).

separator

Each field is separated with a special character. This is normally ,, ; or \t (tab), but you can use any character you need. If one of the strings of text also contains the same separator character then it will be placed inside quotes.

null

Some fields in a record will either not exist (undefined) or will be set to null. This option will set what is written with these types of fields.

boolean TRUE FALSE

Boolean fields are written using the boolean text values. By default these are either TRUE or FALSE. These can be changed to any other required values. For example you could set them to 1 and 0, or Yes and No.

When reading, if these options are set to AUTO then it will try to workout how the booleans are formatted.

escapeCharacters

There are some characters inside text data that are going to create problems. For example, if a field contains new line characters, then they would output an end of line character, spliting the record in two. There are two method for dealing with this.

Escape Character

This replaces problem characters with a \ backslash character and a code character. For example, the new line character 0x0A become \n (a backslash + n character).


RFC 4180 Double Quote

This surrounds the whole text field with quotation marks. If there is another quotation inside the field text then it is replaced with two quotation characters.

quote

There are two different quotation characters we can use. Either single quote ', or double quote ".

quoteAll, quoteStrings, quoteEmptyStrings, quoteDates, quoteHeaders

When writing you can choose to surround all the fields and headers with quotes.

Or, if you want, you can just quote all the string fields.

If the string field is empty (not null or undefined) then instead of writing nothing, you can write two quotation marks.

All the dates can be put inside quotes.

You can have all the header names placed inside quotes too.

dateFormat

Date fields can be formatted in different ways. When writing the default format is ISO. You can set the formatting pattern. You can use the following formatting characters.

When reading, if the date format is set to AUTO then it will try to work out the format of the date.

nullEmpty

When reading in field data, if the data is empty, should the record's field property not be set, or should it be set to null.

skipEmptyLine

If all the fields in a line are empty, should a record be created containing null field properties, or should the record, which doesn't contain any data, be skipped.

convertTypes

When reading in, should all the values be converted into different types. For example, should the text value TRUE be converted into a boolean true property. If no conversion is performed then all the fields are set to string values.

convertEscapeCharacters

When reading in text fields that contain escape characters, should they be converted. For example, when reading in \n (a backslash + n character), should it convert them in to a new line character (0x0A).

typeConvertCallback

There maybe times when the default field type convert is not good enough. You can replace the whole type conversion function with your own. Take a look at the example below. You can look out for the fields in the header you required, convert the value manually yourself, and then convert all the rest using the `CsvWrite.defaultTypeConvertCallback` function.

This same type of thing can be performed when reading in data too.

headerList

By default the headers are workout automatically and you therefore do not need to create this information yourself. However, there maybe times when you need to have more control over what is happening.

Property
Description
skip
When reading in data, you can set to skip those headers you are not interested in, and they will not end up as field properties in the records.
propertyName
The name of the field property inside the record.
csvName
When writing out CSV data, this is the header name used for the field property. Most of the time the property name and the CSV name will be the same, but you can change this as you need.
defaultText
When the field is empty then instead of setting the field to null you can use a default value.
dateFormat
This is the format of the date for when writing or reading in date data.
booleanTrue/booleanFalse
This is the text to look for when processing boolean fields.
type
This states the field type. It can be either number, string, boolean or date.