{"id":22273111,"url":"https://github.com/mac-/csv-split-easy","last_synced_at":"2025-03-25T16:27:08.671Z","repository":{"id":147571010,"uuid":"123990928","full_name":"mac-/csv-split-easy","owner":"mac-","description":":scissors: Splits the CSV string into array of arrays, each representing a row of columns","archived":false,"fork":false,"pushed_at":"2018-03-06T00:03:03.000Z","size":191,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T14:41:01.645Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"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/mac-.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"license.md","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":"2018-03-05T23:11:47.000Z","updated_at":"2018-06-11T08:14:33.000Z","dependencies_parsed_at":"2023-05-31T16:16:01.843Z","dependency_job_id":null,"html_url":"https://github.com/mac-/csv-split-easy","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-%2Fcsv-split-easy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-%2Fcsv-split-easy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-%2Fcsv-split-easy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mac-%2Fcsv-split-easy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mac-","download_url":"https://codeload.github.com/mac-/csv-split-easy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245499019,"owners_count":20625289,"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":[],"created_at":"2024-12-03T13:09:49.175Z","updated_at":"2025-03-25T16:27:08.659Z","avatar_url":"https://github.com/mac-.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# csv-split-easy\n\n\u003ca href=\"https://github.com/revelt/eslint-on-airbnb-base-badge\" style=\"float: right; padding: 0 0 20px 20px;\"\u003e\u003cimg src=\"https://cdn.rawgit.com/revelt/eslint-on-airbnb-base-badge/0c3e46c9/lint-badge.svg\" alt=\"ESLint on airbnb-base with caveats\" width=\"100\" align=\"right\"\u003e\u003c/a\u003e\n\n\u003e Splits the CSV string into array of arrays, each representing a row of columns\n\n[![Minimum Node version required][node-img]][node-url]\n[![Link to npm page][npm-img]][npm-url]\n[![Build Status][travis-img]][travis-url]\n[![Coverage][cov-img]][cov-url]\n[![bitHound Overall Score][overall-img]][overall-url]\n[![bitHound Dependencies][deps-img]][deps-url]\n[![View dependencies as 2D chart][deps2d-img]][deps2d-url]\n[![bitHound Dev Dependencies][dev-img]][dev-url]\n[![Known Vulnerabilities][vulnerabilities-img]][vulnerabilities-url]\n[![Downloads/Month][downloads-img]][downloads-url]\n[![Test in browser][runkit-img]][runkit-url]\n[![MIT License][license-img]][license-url]\n\n## Table of Contents\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Install](#install)\n- [Idea](#idea)\n- [API](#api)\n- [The algorithm](#the-algorithm)\n- [Contributing](#contributing)\n- [Licence](#licence)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Install\n\n```bash\nnpm i csv-split-easy\n```\n\n```js\n// consume its main function as a CommonJS require:\nconst splitEasy = require('csv-split-easy')\n// or as a ES module:\nimport splitEasy from 'csv-split-easy'\n```\n\nHere's what you'll get:\n\nType            | Key in `package.json` | Path  | Size\r\n----------------|-----------------------|-------|--------\nMain export - **CommonJS version**, transpiled to ES5, contains `require` and `module.exports` | `main`                | `dist/csv-split-easy.cjs.js` | 8\u0026nbsp;KB\n**ES module** build that Webpack/Rollup understands. Untranspiled ES6 code with `import`/`export`. | `module`              | `dist/csv-split-easy.esm.js` | 7\u0026nbsp;KB\n**UMD build** for browsers, transpiled, minified, containing `iife`'s and has all dependencies baked-in | `browser`            | `dist/csv-split-easy.umd.js` | 31\u0026nbsp;KB\n\n**[⬆ \u0026nbsp;back to top](#)**\n\n## Idea\n\nSplit a string representing a CSV file into an array of arrays, so that we can traverse it later.\n\nAcceptance Criteria:\n\n- It should accept CSV's with or without a header row\n- Header row might have different amount of columns than the rest of the rows\n- Content (not header) rows might be offset and have different amount of columns from the rest\n- There can be various line break types (`\\n`, `\\r`, `\\n\\r` or `\\n\\n\\n\\n\\n\\n\\r\\r\\r\\r\\r\\n\\n\\n\\n` or whatever)\n- It should ignore any values wrapped with double quotes\n- It should interpret commas as part of the value if it is wrapped in double quotes\n- It should accept empty fields and output them as empty strings\n- It should automatically detect (dot/comma) and remove thousand separators from digit-only cells\n- Minimal dependencies and 100% unit test code coverage in all ways: per-branch, per-statement, per-function and per-line.\n\nOutside of the scope:\n\n- Trimming the values of leading and trailing empty space. Just use `String.prototype.trim()`\n- Parsing numeric values. Parse them yourself. It's outside of the scope of this lib.\n- Smart detection of the offset columns. See [csv-fix-offset](https://github.com/codsen/csv-fix-offset)\n- Sorting rows of double-entry, accounting CSV's. See [csv-sort](https://github.com/codsen/csv-sort)\n\n**[⬆ \u0026nbsp;back to top](#)**\n\n## API\n\n**splitEasy(str[, opts])**\n\nString-in, an array of arrays-out.\nEmpty values, same as numbers too, are set as empty strings.\n\n### Options object\n\n**Defaults**:\n\n```js\n    {\n      removeThousandSeparatorsFromNumbers: true,\n      padSingleDecimalPlaceNumbers: true,\n      forceUKStyle: false\n    }\n```\n\n`options` object's key                | Type     | Obligatory? | Default     | Description\n--------------------------------------|----------|-------------|-------------|----------------------\n{                                     |          |             |             |\n`removeThousandSeparatorsFromNumbers` | Boolean  | no          | `true`      | Should remove thousand separators? `1,000,000` → `1000000`? Or Swiss-style, `1'000'000` → `1000000`? Or Russian-style, `1 000 000` → `1000000`?\n`padSingleDecimalPlaceNumbers`        | Boolean  | no          | `true`      | Should we pad one decimal place numbers with zero? `100.2` → `100.20`?\n`forceUKStyle`                        | Boolean  | no          | `false`     | Should we convert the decimal separator commas into dots? `1,5` → `1.5`?\n}                                     |          |             |             |\n\n**[⬆ \u0026nbsp;back to top](#)**\n\n### Returns\n\nReturns an array of arrays. When there's nothing given, returns `[['']]`\n\nThere's always one array within the main array and there's always something there, at least an empty string.\n\n### For example\n\n```js\nconst splitEasy = require('csv-split-easy')\nconst fs = require('fs')\nconst path = require('path')\n// let's say our CSV sits in the root:\n// its contents, let's say, are:\n// 'Product Name,Main Price,Discounted Price\\nTestarossa (Type F110),\"100,000\",\"90,000\"\\nF50,\"2,500,000\",\"1,800,000\"'\nconst testCSVFile = fs.readFileSync(path.join(__dirname, './csv_test.csv'), 'utf8')\n\nlet source = splitEasy(testCSVFile)\nconsole.log('source = ' + JSON.stringify(source, null, 4))\n// =\u003e [\n//        [\n//            \"Product Name\",\n//            \"Main Price\",\n//            \"Discounted Price\"\n//        ],\n//        [\n//            \"Testarossa (Type F110)\",\n//            \"100000\",\n//            \"90000\"\n//        ],\n//        [\n//            \"F50\",\n//            \"2500000\",\n//            \"1800000\"\n//        ]\n//    ]\n```\n\n**[⬆ \u0026nbsp;back to top](#)**\n\n## The algorithm\n\nCSV files, especially accounting-ones, are different from just _any_ files. We assume that **we don't want any empty rows** in the parsed arrays. It means, [conventional](https://github.com/sindresorhus/split-lines/) string splitting libraries would be inefficient here because after splitting, we'd have to clean up any empty rows.\n\nThe second requirement is that any of the column values in CSV can be wrapped with double quotes. We have to support _mixed_, wrapped and not wrapped-value CSV's because Metro bank used to produce these when I banked with them back in 2015.\n\nThe third requirement is that any of the values can be wrapped with double quotes and have commas within as values.\n\nThe requirements mentioned above pretty much rule out the conventional regex-based split algorithms. You [can](https://github.com/sindresorhus/split-lines/blob/master/index.js) just split by `/\\r?\\n/` but later you'll need to clean up possible empty rows. You can't `string.split` each row by comma because that comma might be a value, you need to check for wrapping double quotes first!\n\nSo, the best algorithm is a single `for`-loop traversal on the input string, detecting and `array.push`ing the values one by one. It worked very well on [email-remove-unused-css](https://github.com/codsen/email-remove-unused-css) where I remove unused CSS from an HTML template within around 2.5 times more characters \"travelled\" than there are in the file. Traversing as a string also worked well on [html-img-alt](https://github.com/codsen/html-img-alt) which needs only a single traversal through the string to fix all the `img` tag `alt` attributes and clean all the crap in/around them.\n\n**[⬆ \u0026nbsp;back to top](#)**\n\n## Contributing\n\n* If you **want a new feature** in this package or you would like us to change some of its functionality, raise an [issue on this repo](https://github.com/codsen/csv-split-easy/issues).\n\n* If you tried to use this library but it misbehaves, or **you need an advice setting it up**, and its readme doesn't make sense, just document it and raise an [issue on this repo](https://github.com/codsen/csv-split-easy/issues).\n\n* If you would like to **add or change some features**, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Code style is `airbnb-base`, only without semicolons. If you use a good code editor, it will pick up the established ESLint setup.\n\n**[⬆ \u0026nbsp;back to top](#)**\n\n## Licence\n\nMIT License (MIT)\n\nCopyright © 2018 Codsen Ltd, Roy Revelt\n\n\n[node-img]: https://img.shields.io/node/v/csv-split-easy.svg?style=flat-square\u0026label=works%20on%20node\n[node-url]: https://www.npmjs.com/package/csv-split-easy\n\n[npm-img]: https://img.shields.io/npm/v/csv-split-easy.svg?style=flat-square\u0026label=release\n[npm-url]: https://www.npmjs.com/package/csv-split-easy\n\n[travis-img]: https://img.shields.io/travis/codsen/csv-split-easy.svg?style=flat-square\n[travis-url]: https://travis-ci.org/codsen/csv-split-easy\n\n[cov-img]: https://coveralls.io/repos/github/codsen/csv-split-easy/badge.svg?style=flat-square?branch=master\n[cov-url]: https://coveralls.io/github/codsen/csv-split-easy?branch=master\n\n[overall-img]: https://img.shields.io/bithound/code/github/codsen/csv-split-easy.svg?style=flat-square\n[overall-url]: https://www.bithound.io/github/codsen/csv-split-easy\n\n[deps-img]: https://img.shields.io/bithound/dependencies/github/codsen/csv-split-easy.svg?style=flat-square\n[deps-url]: https://www.bithound.io/github/codsen/csv-split-easy/master/dependencies/npm\n\n[deps2d-img]: https://img.shields.io/badge/deps%20in%202D-see_here-08f0fd.svg?style=flat-square\n[deps2d-url]: http://npm.anvaka.com/#/view/2d/csv-split-easy\n\n[dev-img]: https://img.shields.io/bithound/devDependencies/github/codsen/csv-split-easy.svg?style=flat-square\n[dev-url]: https://www.bithound.io/github/codsen/csv-split-easy/master/dependencies/npm\n\n[vulnerabilities-img]: https://snyk.io/test/github/codsen/csv-split-easy/badge.svg?style=flat-square\n[vulnerabilities-url]: https://snyk.io/test/github/codsen/csv-split-easy\n\n[downloads-img]: https://img.shields.io/npm/dm/csv-split-easy.svg?style=flat-square\n[downloads-url]: https://npmcharts.com/compare/csv-split-easy\n\n[runkit-img]: https://img.shields.io/badge/runkit-test_in_browser-a853ff.svg?style=flat-square\n[runkit-url]: https://npm.runkit.com/csv-split-easy\n\n[license-img]: https://img.shields.io/npm/l/csv-split-easy.svg?style=flat-square\n[license-url]: https://github.com/codsen/csv-split-easy/blob/master/license.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmac-%2Fcsv-split-easy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmac-%2Fcsv-split-easy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmac-%2Fcsv-split-easy/lists"}