{"id":20763085,"url":"https://github.com/nickpisacane/csvbuilder","last_synced_at":"2025-04-10T03:56:13.069Z","repository":{"id":31690121,"uuid":"35255735","full_name":"nickpisacane/CsvBuilder","owner":"nickpisacane","description":"Easily encode complex JSON objects to CSV with CsvBuilder's schema-like API","archived":false,"fork":false,"pushed_at":"2018-08-23T15:19:43.000Z","size":62,"stargazers_count":129,"open_issues_count":2,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T03:56:08.246Z","etag":null,"topics":["csv","csv-converter","csv-export","csv-stream","stream"],"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/nickpisacane.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":"2015-05-08T03:04:39.000Z","updated_at":"2024-11-30T08:28:01.000Z","dependencies_parsed_at":"2022-09-06T13:11:55.216Z","dependency_job_id":null,"html_url":"https://github.com/nickpisacane/CsvBuilder","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickpisacane%2FCsvBuilder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickpisacane%2FCsvBuilder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickpisacane%2FCsvBuilder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickpisacane%2FCsvBuilder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nickpisacane","download_url":"https://codeload.github.com/nickpisacane/CsvBuilder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248155001,"owners_count":21056542,"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-converter","csv-export","csv-stream","stream"],"created_at":"2024-11-17T10:42:32.657Z","updated_at":"2025-04-10T03:56:13.043Z","avatar_url":"https://github.com/nickpisacane.png","language":"JavaScript","readme":"# Csvbuilder\n![travis](https://travis-ci.org/nickpisacane/CsvBuilder.svg?branch=master)\n\nEasily encode complex JSON objects to CSV with CsvBuilder's schema-like API.\n\n# Table Of Contents\n* [Usage](#usage)\n* [New Features](#new-features)\n* [Installation](#installation)\n* [API](#api)\n* [Migration To 1.0.0](#migration-to-100)\n\n# Usage\n```js\n\nconst CsvBuilder = require('csv-builder')\n\nconst data = [\n  {\n    name: 'Foo Bar',\n    meta: {\n      active: true,\n      roles: [\n        'user',\n        'admin'\n      ]\n    }\n  }\n]\n\nconst builder = new CsvBuilder({\n  headers: ['Firstname', 'Lastname', 'Role 1', 'Role 2', 'Active'],\n  alias: {\n    'Role 1': 'meta.roles[0]',\n    'Role 2': 'meta.roles[1]',\n    'Active': 'meta.active'\n  }\n})\n  .virtual('Firstname', user =\u003e user.name.split(' ')[0])\n  .virtual('Lastname', user =\u003e user.name.split(' ')[1])\n\n/* Each of the following produces the following CSV contents:\n\n\"Firstname\",\"Lastname\",\"Role 1\",\"Role 2\",\"Active\"\n\"Foo\",\"Bar\",\"user\",\"admin\",\"true\"\n\n*/\n\n\n// (1) Create from a Stream of objects (like a database)\ngetObjectStream()\n  .pipe(builder.createTransformStream())\n  .pipe(fs.createWriteStream('output.csv'))\n\n// (2) Create from an existing payload (`data` is an array of objects)\nbuilder.createReadStream(data)\n  .pipe(fs.createWriteStream('output.csv'))\n\n// (3) Roll your own\nlet csv = ''\ncsv += builder.getHeaders()\ndata.forEach(item =\u003e {\n  csv += builder.getRow(item)\n})\nfs.writeFileSync('output.csv', csv)\n```\n\n# Installation\n```bash\n$ npm i -s csv-builder\n# or\n$ yarn add csv-builder\n```\n\n# New Features\n* More cohesive API\n* Expanded API to support non-stream outputs, i.e. building a CSV string row-by-row with the `getHeaders()` and `getRow(object)` methods respectively.\n* Better CSV encoding (proper quoting by default)\n\n# API\n##### CsvBuilder([options])\n* `headers` *String|Array\u003cString\u003e* Space separated headers, or array of headers **(required)**\n* `delimiter` *String* The column delimiter. Default `','`\n* `terminator` *String* The row terminator. Default `'\\n'`\n* `quoted` *Boolean* Quote columns? Default `true`\n* `alias` *Object* An object in the format of { \"csv header\": \"object prop\" }, `object prop` will be aliased to `csv header`. Default `{}`\n\n## Methods\n\n##### CsvBuilder#`createReadStream`(payload): *Stream.Readable*\nCreates a readable stream and consumes the payload.\n* `payload` *Array\\\u003cObject\\\u003e* Incoming data.\n\n##### CsvBuilder#`createTransformStream`(): *Stream.Transform*\nCreates a transform stream. The stream expects either Objects or JSON.\n\n##### CsvBuilder#`headers`(headers): *this*\n* `headers` *String|Array* Space separated headers, or array of headers\n\n##### CsvBuilder#`alias`(header, prop): *this*\nSet single or multiple contraints. If `header` is an object, it will extend any existing constraints, not replace.\n* `header` *String|Object* Either object {\"header\": \"property\"} Or a string \"Header\"\n* `prop` *String|undefined* Property to correspond to header, omit if using object.\n\n##### CsvBuilder#`virtual`(prop, fn): *this*\nCreate a virtual property. Virtual properties are treated the same as normal\nproperties. If there is no corresponding header or alias, the virtual will not be present in resulting CSV.\n* `prop` *String* Virtual property name\n* `fn` *(item: any) =\u003e any* Where `item` is an element from the incoming data, and the return value is the corresponding value for the virtualized property.\n\n##### CsvBuilder#`getHeaders`(): *String*\nThe headers in CSV format\n\n##### CsvBuilder#`getRow`(item): *String*\nReturns the CSV formated row for a given `item`.\n*  `item` *Object* A n item matching the \"schema\".\n\n# Migration to 1.0.0\n* `constraints` attribute in options (for constructor) is deprecated, use `alias` instead.\n* `set(prop, value)` method is deprecated, use `alias(prop, value)` instead.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickpisacane%2Fcsvbuilder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickpisacane%2Fcsvbuilder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickpisacane%2Fcsvbuilder/lists"}