{"id":13339459,"url":"https://github.com/tdast/tdast-util-to-csv","last_synced_at":"2025-08-11T01:36:38.794Z","repository":{"id":144056749,"uuid":"294624839","full_name":"tdast/tdast-util-to-csv","owner":"tdast","description":"tdast utility to serialize tdast to CSV (RFC-4180 compliant)","archived":false,"fork":false,"pushed_at":"2020-09-13T20:11:46.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-09T17:18:06.733Z","etag":null,"topics":["ast","csv","tabular-data","tdast","tdast-util","unist","util"],"latest_commit_sha":null,"homepage":"https://github.com/tdast","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/tdast.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-11T07:26:50.000Z","updated_at":"2020-09-13T20:13:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"57e48879-9b37-4da1-808b-e2be37de2553","html_url":"https://github.com/tdast/tdast-util-to-csv","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/tdast/tdast-util-to-csv","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdast%2Ftdast-util-to-csv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdast%2Ftdast-util-to-csv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdast%2Ftdast-util-to-csv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdast%2Ftdast-util-to-csv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tdast","download_url":"https://codeload.github.com/tdast/tdast-util-to-csv/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdast%2Ftdast-util-to-csv/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269804922,"owners_count":24477883,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["ast","csv","tabular-data","tdast","tdast-util","unist","util"],"created_at":"2024-07-29T19:20:03.562Z","updated_at":"2025-08-11T01:36:38.775Z","avatar_url":"https://github.com/tdast.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tdast-util-to-csv\n\n[**tdast**][tdast] utility to serialize tdast into CSV ([RFC-4180][] compliant).\n\n---\n\n## Install\n\n```sh\nnpm install tdast-util-to-csv\n```\n\n## Use\n\n```js\nimport toCsv from 'tdast-util-to-csv';\n\nconst tdast = {\n  type: 'table',\n  children: [\n    {\n      type: 'row',\n      index: 0,\n      children: [\n        {\n          type: 'column',\n          index: 0,\n          value: 'col1',\n        },\n        {\n          type: 'column',\n          index: 1,\n          value: 'col2',\n        },\n        {\n          type: 'column',\n          index: 2,\n          value: 'col3',\n        },\n      ],\n    },\n    {\n      type: 'row',\n      index: 1,\n      children: [\n        {\n          type: 'cell',\n          columnIndex: 0,\n          rowIndex: 1,\n          value: 'row2cell1',\n        },\n        {\n          type: 'cell',\n          columnIndex: 1,\n          rowIndex: 1,\n          value: 'row2cell2',\n        },\n        {\n          type: 'cell',\n          columnIndex: 2,\n          rowIndex: 1,\n          value: 'row2cell3',\n        },\n      ],\n    },\n  ],\n};\n\nexpect(toCsv(tdast))\n  .toEqual('col1,col2,col3\\nrow2cell1,row2cell2,row2cell3');\n```\n\n## API\n\n### `toCsv(tdast[, options])`\n#### Interface\n```ts\nfunction toCsv(\n  // tdast Table node\n  tdast: Table,\n  // options to configure serializer\n  options?: Options,\n): string;\n```\n\nSerializes a tdast `Table` node to [RFC-4180][] compliant CSV.\n\nAll CSV field values are properly escaped with double quotes whenever applicable.  If the tdast `Cell` and `Column` node contains non-string values, these are stringified with `JSON.stringify` and propery escaped.\n\n`toCsv` automatically infers `Column` node values to add in the CSV.  You may override this behavior by specifying columns explicitly with `options.columns`.  You can also always ensure field values are enclosed with double quotes by configuring `options.doubleQuotes`.  These are detailed in the example below.\n\n#### Example\nUsing the same tdast tree in the earlier example\n```js\nimport toCsv from 'tdast-util-to-csv';\n\nconst options = {\n  columns: ['colA', 'colB', 'colC'],\n  doubleQuotes: true,\n};\n\nexpect(toCsv(tdast, options))\n  .toEqual('\"colA\",\"colB\",\"colC\"\\n\"row2cell1\",\"row2cell2\",\"row2cell3\"');\n```\n\nAn example demonstrating how non-string values are stringified with `JSON.stringify` and properly escaped:\n\n```js\nimport td from 'tdastscript';\nimport toCsv from 'tdast-util-to-csv';\n\n// string, number, boolean, null values are easily stringified\nexpect(\n  toCsv(td('table', [td('row', ['one', 2, true, false, null])])),\n).toEqual('one,2,true,false,null');\n\n// undefined value is converted to an empty string\nexpect(\n  toCsv(td('table', [td('row', [undefined, undefined, undefined])])),\n).toEqual(',,');\n\n// array values are stringified and properly escaped with double quotes\nexpect(\n  toCsv(\n    td('table', [\n      td('row', [\n        td('cell', { value: ['one', 2, true, false, null, undefined] }),\n      ]),\n    ]),\n  ),\n).toEqual('\"[\"\"one\"\",2,true,false,null,null]\"');\n\n// object values are stringified and properly escaped with double quotes\nexpect(\n  toCsv(\n    td('table', [\n      td('row', [\n        td('cell', {\n          value: {\n            one: 2,\n            3: 'four',\n            five: [6, 'seven'],\n            eight: null,\n            nine: undefined,\n          },\n        }),\n      ]),\n    ]),\n  ),\n).toEqual(\n  '\"{\"\"3\"\":\"\"four\"\",\"\"one\"\":2,\"\"five\"\":[6,\"\"seven\"\"],\"\"eight\"\":null}\"',\n);\n```\n\n#### Related interfaces\n```ts\ninterface Options {\n  // array of column strings that will be used as object keys.  Overrides the column values detected in the tdast tree.\n  columns?: string[];\n  // if CSV field values should be doublequoted\n  doubleQuotes?: boolean;\n}\n```\n\n## Related\n- [`tdast-util-from-csv`][tdast-util-from-csv]\n- [`tdastscript`][tdastscript]\n\n\u003c!-- Definitions --\u003e\n[rfc-4180]: https://tools.ietf.org/html/rfc4180\n[tdast]: https://github.com/tdast/tdast\n[tdast-util-from-csv]: https://github.com/tdast/tdast-util-from-csv\n[tdastscript]: https://github.com/tdast/tdastscript\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdast%2Ftdast-util-to-csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftdast%2Ftdast-util-to-csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdast%2Ftdast-util-to-csv/lists"}