Code Monkey home page Code Monkey logo

to-csv's Introduction

to-csv

Convert objects to CSV format

install

$ component install jwerle/to-csv

api

csv(array, opts)

Accepts an array of objects and converts them to CSV format

example

var data = [];

for (var i = 0; i < 10; ++i) {
	data.push({ 
		id: Math.random().toString(16).slice(2), 
		value: data.length % 2 
	});
}

console.log(csv(data));
/**
 	id,value
	22dd67dd,0
	8d18327,1
	adfcb2bb,0
	5424635b,1
	f8049525,0
	49493c6d,1
	af99d0f1,0
	d9f82d24,1
	e7f38d52,0
	47ef5c67,1
**/

License

MIT

to-csv's People

Contributors

albert-iv avatar jwerle avatar lafama avatar robertleeplummerjr avatar stiekel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

to-csv's Issues

Overriding delimiter leads to inconsistent headers

See example:

var CSV = requrie('to-csv');
CSV.DELIMITER = ';'.charCodeAt(0);

var result = CSV([{ a: 1, b: 2 }]);
console.log(result);

This will output:

a,b
1;2

Instead of expected

a;b
1;2

It seems for me that the issue is in this line:

return false !== opts.headers
        ? [].concat(headers.join(','), char(CSV.CHAR_NEWLINE)).concat(buf).filter(Boolean).join('')
        : buf.filter(Boolean).join('');

headers should be joined with CSV.DELIMITER instead of ','.

Bump npm Version

Could you bump the npm version by chance? I would like to use the newest version and using a git URL in my package.json is a pain to use in my environment.

Thanks!

Doesn't Encapsulate Commas

If you pass in strings that have commas the library doesn't encapsulate, thus breaking the spreadsheet.

Might think about adding options into the csv() call, allowing users to specify what character you want to enclose values with.

Expected

var data = [
  { dataString1 : '$5,000.00', dataString2 : "$6,000.00" }
]

console.log(csv(data));
// dataString1,dataString2
// "$5,000.00","$6,000.00"

Actual

var data = [
  { dataString1 : '$5,000.00', dataString2 : "$6,000.00" }
]

console.log(csv(data));
// dataString1,dataString2
// $5,000.00,$6,000.00

Headers read only from a[0] - throws Error TypeError: Cannot call method 'toString' of undefined

Assume the following object
[{
time: "2014-07-10",
temperature: 18.91578947368421,
humidity: 89.28157894736843,
date: "2014-07-10T23:59:18.553Z"},
{
time: "2014-07-11",
sensor_id: "DHT22",
status: "OK",
temperature: 17.358620689655176
}];

One of the properties ("humidity") present in a[0] is absent from an item that follows. In this case the script thows Error TypeError: Cannot call method 'toString' of undefined.

Suggest to leave an empty cell in this case.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.