The internal entries iterator. In some cases iterating over lines is simple. For example, if we create an instance from string or array we already have all the lines. However, if the input is a file, the entries iterator will be a function that reads and parses one line at a time.
The internal lines iterator. In some cases iterating over lines is simple. For example, if we create an instance from string or array we already have all the lines. However, if the input is a file, the lines iterator will be a function that reads one line at a time.
The entries iterator of the instance. Yields lines as objects.
The lines iterator of the instance. Yields lines as strings.
Sets the entries iterator of the instance. Useful while composing an instance from different sources. The "entries" might be JSON objects representing NDJSON lines or JS arrays of strings representing a line in CSV or TSV file.
The instance to allow chaining
Sets the entries iterator of the instance. Useful while composing an instance from different sources
The iterator to use
The instance to allow chaining
Converts the contents of the collection to array of "values". The values are json objects representing each line, excluding the header.
NOTE: Don't use this for big objects/files because the output array is built into memory and then returned. For big files iterate over the entries instead, which will yield the same objects:
for (const entry of collection.entries()) {
// entry is an object representing a row
}
Writes the collection to a file.
Absolute path to file.
Converts the contents of the collection to array of "values". The values are json objects representing each line. The result does not include the header in case of delimited formats.
NOTE: Don't use this for big objects/files because the output array is built into memory and then returned. For big files iterate over the entries instead, which will yield the same objects:
for (const entry of collection.entries()) {
// entry is an object representing a row
}
Converts the instance to NDJSON string.
NOTE: Don't use this for big objects/files because the output string is built into memory and then returned. For big files iterate over the entries instead and serialize one line at a time:
for (const entry of collection.entries()) {
const line = JSON.stringify(entry);
}
The new line character to use. Defaults to \r\n
.
Serializes the contents of the collection to a string. The result can be a CSV, TSV or other delimited format depending on the options. The default options will output CSV string. For TSV use:
instance.toString({ delimiter: "\t" });
Converts the contents of the collection to an array of strings. The result strings can be in CSV, TSV or other delimited format depending on the options. The default options will output CSV strings. For TSV use:
instance.toStringArray({ delimiter: "\t" });
If we happen to have the entire csv as array of objects, we can create an instance like so:
const csv = CSV.fromArray([{ a: "1", b: "2" }, { a: "3", b: "4" }]);
csv.lines(); // Lines iterator -> "1,2", "3,4"
csv.entries(); // JSON iterator -> { a: "1", b: "2" }, { a: "3", b: "4" }
In this case we already have entries and we only need to handle their
serialization in the lines
iterator.
An array of objects that can be serialized as JSON
Creates and returns an instance from directory path. This will walk
(recursively) through the directory and collect all the files having
a .csv
extension. The lines
and entries
iterators will yield
results from all those files combined. Example:
const csv = Delimited.fromDirectory("/path/to/directory/containing/csv/files");
csv.lines(); // Lines iterator
csv.entries(); // JSON iterator
Absolute path to directory
Creates and returns an instance from a file path. The lines
and
entries
iterators will read the file one line at a time. Example:
const csv = Delimited.fromFile("/path/to/file.csv");
csv.lines(); // Lines iterator
csv.entries(); // Entries iterator
For TSV or another custom delimited format provide a delimiter option like so:
const tsv = Delimited.fromFile("/path/to/file.tsv", { delimiter: "\t" });
Absolute path to CSV or TSV file
If we have the entire csv or tsv as a string, we can create an instance like so:
const csv = Delimited.fromString("a,b\n1,2\r\n3,4");
csv.lines(); // Lines iterator -> "1,2", "3,4"
csv.entries(); // JSON iterator -> { a: "1", b: "2" }, { a: "3", b: "4" }
The input string that can be parsed as CSV or TSV
If we have the entire collection as an array of string lines, we can create an instance like so:
const csv = Delimited.fromStringArray(["a,b", "1,2", "3,4"]);
csv.lines(); // Lines iterator -> "1,2", "3,4"
csv.entries(); // JSON iterator -> { a: "1", b: "2" }, { a: "3", b: "4" }
The fist line is used as header!
An array of strings that can be parsed as CSV or TSV
Generated using TypeDoc
This class represents a collection in delimited format.
from
and converted to other kinds of output using the instance methods starting withto
.entries
iterator will yield json objects for each line.