3.0.4 • Published 1 year ago

csv-rw v3.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

CSV writer and parser

With dynamic typing! 🥳

npm-badge PRs Welcome

Usage

To install the package, run the following command:

npm i csv-rw

Example

import { CSV } from "csw-rw";

const csv = new CSV({ path: "path/to/file.csv", headers: ["name", "age"] });

// There's intellisense!
await csv.write({ name: "John", age: 21 });

await csv.write([
  { name: "Jane", age: 19 },
  { name: "Freddy", age: 45 }
]);

csv.read().then(console.log)

/* -> [
    { name: "John", age: 21 },
    { name: "Jane", age: 19 },
    { name: "Freddy", age: 45 }
] */

You must pass am object to the CSV class constructor, these are the properties on that object:

optiondescriptiontypedefault
pathPath to the CSV file, if the file doesn't exist it will be created.stringNone
headersThe headers of the CSV file.string[][]
deletePreviousIf the path to the CSV file already exists, whether to delete it and create a new one.stringfalse
delimiterThe delimiter that separates the value / headersstring,

Usage with types

By default, the value of each header is of type Value, which is string | number | boolean | null. The default Value type can be overridden, by preixing the headers value in the constructor.

s:... -> sting n:... -> number b:... -> boolean

And you can make a header value optional by suffixing the value with ? By default, if the value is optional and not passed while writing to the csv file, it will default to null

Example

import { CSV } from "csv-rw";

const csv = new CSV({
  path: "path/to/file.csv",
  headers: ["s:name", "n:age", "b:isAlive?"]
});

/**
 * Name - string
 * Age - number
 * isAlive - boolean | undefined
 */

csv.write({ name: "Jane", age: "Don't ask that to a lady!" });
/**
 *                         ↑
 * The typescript compiler will complain, as we are trying to
 * assign a string to a number
 */

License

MIT License (c) 2023 Saverio Scagnoli

3.0.4

1 year ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.1.1

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.3.2

2 years ago

0.2.2

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago