{"id":16485323,"url":"https://github.com/morganconrad/jstocsv","last_synced_at":"2025-04-04T17:49:29.843Z","repository":{"id":42953092,"uuid":"232668571","full_name":"MorganConrad/JStoCSV","owner":"MorganConrad","description":"micro csv writer for javascipt objects","archived":false,"fork":false,"pushed_at":"2023-01-06T02:25:16.000Z","size":196,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T21:03:23.844Z","etag":null,"topics":["csv","csv-writer","export","javascript-library","writestream"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MorganConrad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-08T22:00:57.000Z","updated_at":"2021-05-10T23:11:36.000Z","dependencies_parsed_at":"2023-02-05T03:01:30.651Z","dependency_job_id":null,"html_url":"https://github.com/MorganConrad/JStoCSV","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorganConrad%2FJStoCSV","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorganConrad%2FJStoCSV/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorganConrad%2FJStoCSV/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MorganConrad%2FJStoCSV/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MorganConrad","download_url":"https://codeload.github.com/MorganConrad/JStoCSV/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226195,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","csv-writer","export","javascript-library","writestream"],"created_at":"2024-10-11T13:25:26.676Z","updated_at":"2025-04-04T17:49:29.819Z","avatar_url":"https://github.com/MorganConrad.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://secure.travis-ci.org/MorganConrad/JStoCSV.png)](http://travis-ci.org/MorganConrad/JStoCSV)\n[![License](http://img.shields.io/badge/license-MIT-A31F34.svg)](https://github.com/MorganConrad/JStoCSV)\n[![NPM Downloads](http://img.shields.io/npm/dm/jstocsv.svg)](https://www.npmjs.org/package/jstocsv)\n[![Known Vulnerabilities](https://snyk.io/test/github/morganconrad/JStoCSV/badge.svg)](https://snyk.io/test/github/morganconrad/JStoCSV)\n[![Coverage Status](https://coveralls.io/repos/github/MorganConrad/JStoCSV/badge.svg)](https://coveralls.io/github/MorganConrad/JStoCSV)\n\n# JStoCSV\nA small, versatile, dependency free module to convert an array of JavaScript objects to CSV.\n\n - Handles basic nesting, _e.g._ `field.nested.inAnother` or `myArray[4]`\n - Support alternative delimiters (_e.g._ TAB delimted) and end of lines.\n - Powerful user function option for \"tricky stuff\".\n\n## Usage\n\n```js\nconst labelledFields = [\n  { path: \"name.given\", label: \"First Name\" },  // example of nested data\n  { path: \"name.surname\", label: \"Last Name\" }\n  { path: \"birth_day\", label: \"Date of Birth\" },\n...\n];\n\ndata = [\n { name: { given: \"John\", surname: \"Doe\" }, birth_day: \"06 March 2003\" },\n  ...\n]\n\nconst jstocsv = new JStoCSV(labelledFields);\njstocsv.stream(data, someWriteableStream); // or\nlet result = jstocsv.generateString(data);\n\nOutput / Result\nFirst Name,Last Name,Date of Birth\nJohn,Doe, 06 March 2003\n...\n```\n\n## API\n\n### JStoCSV(fields, options)   constructor\n - fields is usually an array of {}\n   - path: a string, route to get the data, e.g. \"name\" or \"name.first\"\n   - label: label for header line (defaults to path)\n   - fn: optional user function to apply afterwards (usually null, see Notes at end)\n\n - fields may be an array of strings\n   - if so, they are used for the path (and the header label)\n   - fn option is unavailable\n\n_If fields is not provided, JStoCSV will use all keys from the first data value._\n\n - options\n   - delimiter     defaults to ',', you can use '\\t' etc.\n   - eol           defaults to '\\n'\n   - quoteEmpties  put quotes around all empty fields\n   - quoteAll      put quotes around all fields\n   - noHeaderLine  don't write out a header line\n\n### generateLines(data)\nCreates an array of Strings, one per line of the CSV file.\n  - data is an array of objects.\n\n### generateString(data, appendEOL)\nJoins the lines with the eol character.\n  - data is an array of objects.\n  - appendEOL  at end (default is false)\n\nBoth of those require that all the CSV file fit in memory.  If your data is huge, consider\n\n### stream(data, writeStream)\n - data is an array of objects.\n - writeStream   **note:** caller must end() or close() etc...\n\nFor the ultimate in control\n### reduce(data, reducer, acc)\n - data is an array of objects\n - reducer(acc, line) called for each line in the csv file\n - acc   the initial accumulator\n\nFor convenience, you can use (see tests)\n - _stringReducer();\n - _streamReducer()\n\n### Notes, Todos, and  Caveats\n\n#### Supplying a user function `fn` in `fields`\n\nThis module is short because it just does the basics for output.  If you need to \"do something very special\", supply a user function `fn` to `fields`.  For each value, it will be called with\n\n`fn(value, datum, index, array)`\n - **value** is the value within datum (as determined by path)\n - **datum** is the entire object in the data array\n - **index** is the 0-based index of the object within the data array\n - **array** is the full data array\n\nFor example, in the unit test \"userfn\", we generate an artificial column based on the difference between successive values in the field `.number`, via\n\n```js\nfn: function(val, datum, index, array) {\n  return index ? val-array[index-1].number : val;\n}\n```\n\n#### Caveats\n\n - The `_quoteValue()` method is pretty basic.  Pathological data may break it.\n - a new field, `.splitpath`, will be added to each of the fields you pass in.\n\n### Alternatives.  Most are larger.\n\n#### [json-csv](https://www.npmjs.com/package/json-csv)\n - works very similarly, even has a user function (caller \"filter\") but less versatile\n\n#### [simple-csv-writer](https://www.npmjs.com/package/simple-csv-writer-csv-writer)\n - works with arrays of **arrays** (not objects)\n\n#### [csv-writer](https://github.com/ryu1kn/csv-writer)\n - looks good but more complex.\n\n#### [fast-csv](https://www.npmjs.com/package/fast-csv)\n - looks powerful\n - the headers are confusing\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorganconrad%2Fjstocsv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorganconrad%2Fjstocsv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorganconrad%2Fjstocsv/lists"}