24
loading...
This website collects cookies to deliver better user experience
lil-csv
.1465 B: lil-csv.js.gz
1313 B: lil-csv.js.br
1315 B: lil-csv.modern.js.gz
1201 B: lil-csv.modern.js.br
1480 B: lil-csv.module.js.gz
1327 B: lil-csv.module.js.br
1524 B: lil-csv.umd.js.gz
1359 B: lil-csv.umd.js.br
name,address,course
John Smith,"123 John St, CARLTON",Advanced Calculus
Any Newman,"4a/3a Church Ave, CROYDON",Advanced Calculus
import { parse } from "lil-csv";
const objects = parse(fileContents);
console.log(objects);
/*
[
{
"name": "John Smith",
"address": "123 John St, CARLTON",
"course": "Advanced Calculus"
},
{
"name": "Any Newman",
"address": "4a/3a Church Ave, CROYDON",
"course": "Advanced Calculus"
}
]
*/
import { generate } from "lil-csv";
const string = generate(objects);
console.log(string);
/*
name,address,course
John Smith,"123 John St, CARLTON",Advanced Calculus
Any Newman,"4a/3a Church Ave, CROYDON",Advanced Calculus
*/
assert.deepEqual(objects, parse(generate(objects)));
const arrays = parse(fileContents, { header: false });
console.log(arrays);
/*
[
["name","address","course"],
["John Smith","123 John St, CARLTON","Advanced Calculus"],
["Any Newman","4a/3a Church Ave, CROYDON","Advanced Calculus"]
]
*/
const string = generate(arrays, { header: false });
console.log(string);
/*
name,address,course
John Smith,"123 John St, CARLTON",Advanced Calculus
Any Newman,"4a/3a Church Ave, CROYDON",Advanced Calculus
*/
firstName,lastName,dob,price,completed
John,Smith,1999-01-15,123.00,Y
Alice,Dwarf,1991-11-24,123.00,N
const people = parse(fileContents, {
header: {
"*": String,
dob: v => v ? new Date(v) : null,
price: v => isNaN(v) ? null : Number(v),
completed: v => String(v).toUpperCase() === "Y",
}
});
console.log(people);
/*
[
{
"firstName": "John",
"lastName": "Smith",
"dob": "1999-01-15T00:00:00.000Z",
"price": 123.00,
"completed": true
},
{
"firstName": "Alice",
"lastName": "Dwarf",
"dob": "1991-11-24T00:00:00.000Z",
"price": 123.00,
"completed": false
}
]
*/
Date
to calendar dates like "YYYY-MM-DD", and add custom formatting to numbers like "123.00" instead of the default "123":const string = generate(people, {
header: {
"*": String,
dob: v => v ? new Date(v).toISOString().substr(0, 10) : "",
price: v => isNaN(v) ? "" : Number(v).toFixed(2),
completed: v => v ? "Y" : "N",
}
});
console.log(string);
/*
firstName,lastName,dob,price,completed
John,Smith,1999-01-15,123.55,Y
Alice,Dwarf,1991-11-24,123.55,N
*/
lil-csv
is little but powerful. It can can handle that too.First name,Last name,Date of birth,Price in dollars,Completed
John,Smith,1999-01-15,123.00,Y
Alice,Dwarf,1991-11-24,123.00,N
const people = parse(fileContents, {
header: {
"First name": "firstName",
"Last name": "lastName",
"Date of birth": {
newName: "dob",
parse: v => v ? new Date(v) : null,
},
"Price in dollars": {
newName: "price",
parse: v => isNaN(v) ? null : Number(v),
},
Completed: {
newName: "completed",
parse: v => String(v).toUpperCase() === "Y",
},
}
});
console.log(people);
/*
[
{
"firstName": "John",
"lastName": "Smith",
"dob": "1999-01-15T00:00:00.000Z",
"price": 123.00,
"completed": true
},
{
"firstName": "Alice",
"lastName": "Dwarf",
"dob": "1991-11-24T00:00:00.000Z",
"price": 123.00,
"completed": false
}
]
*/
const string = generate(people, {
header: {
firstName: "First name",
lastName: "Last name",
dob: {
newName: "Date of birth",
stringify: v => v ? new Date(v).toISOString().substr(0, 10) : "",
},
price: {
newName: "Price in dollars",
stringify: v => isNaN(v) ? "" : Number(v).toFixed(2),
},
completed: {
newName: "Completed",
stringify: v => v ? "Y" : "N",
},
}
});
console.log(string);
/*
First name,Last name,Date of birth,Price in dollars,Completed
John,Smith,1999-01-15,123.00,Y
Alice,Dwarf,1991-11-24,123.00,N
*/
lil-csv
. You can parse CSV rows directly to deep objects like:{
order_id: 51234,
recipient: {
firstName: "John",
lastName: "Smith",
dob: "1999-01-15T00:00:00.000Z",
address: {
street: "123 John St, CARLTON",
country: "AU",
}
}
}
ID,First name,Last name,Date of birth,Address,Country
51234,John,Smith,1999-01-15,"123 John St, CARLTON",AU
const orders = parse(fileContents, {
header: {
ID: {
parse: Number,
newName: "order_id",
},
"First name": "recipient.firstName",
"Last name": "recipient.lastName",
"Date of birth": {
newName: "recipient.dob",
parse: v => v ? new Date(v) : null,
},
Address: "recipient.address.street",
Country: "recipient.address.country",
}
});
console.log(orders);
const string = generate(orders, {
header: {
order_id: "ID",
"recipient.firstName": "First name",
"recipient.lastName": "Last name",
"recipient.dob": {
newName: "Date of birth",
stringify: v => v ? new Date(v).toISOString().substr(0, 10) : "",
},
"recipient.address.street": "Address",
"recipient.address.country": "Country",
}
});
console.log(string);
/*
ID,First name,Last name,Date of birth,Address,Country
51234,John,Smith,1999-01-15,"123 John St, CARLTON",AU
*/
lil-csv
).lil-csv
then feel free open an issue here: https://github.com/flash-oss/lil-csv/issues