From a61d230c7fb21b9ca4977cb8df9087c47312b383 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Tue, 17 Sep 2019 15:02:08 +0900 Subject: [PATCH] Add csv parser --- frontend/package.json | 1 + frontend/services/parsers/csv.service.js | 9 +++++++++ frontend/test/unit/services/csv.service.spec.js | 12 ++++++++++++ frontend/yarn.lock | 5 +++++ 4 files changed, 27 insertions(+) create mode 100644 frontend/services/parsers/csv.service.js create mode 100644 frontend/test/unit/services/csv.service.spec.js diff --git a/frontend/package.json b/frontend/package.json index cd6b5fbe..cb4c2ccb 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -19,6 +19,7 @@ "@toast-ui/vue-editor": "^1.1.1", "codemirror": "^5.48.2", "nuxt": "^2.0.0", + "papaparse": "^5.0.2", "tui-editor": "^1.4.5", "vuetify": "^2.0.2" }, diff --git a/frontend/services/parsers/csv.service.js b/frontend/services/parsers/csv.service.js new file mode 100644 index 00000000..53c88fac --- /dev/null +++ b/frontend/services/parsers/csv.service.js @@ -0,0 +1,9 @@ +import Papa from 'papaparse' + +class CSVParser { + parse(text, options = { header: true }) { + return Papa.parse(text, options) + } +} + +export default CSVParser diff --git a/frontend/test/unit/services/csv.service.spec.js b/frontend/test/unit/services/csv.service.spec.js new file mode 100644 index 00000000..3e0d23da --- /dev/null +++ b/frontend/test/unit/services/csv.service.spec.js @@ -0,0 +1,12 @@ +import CSVParser from '@/services/parsers/csv.service' + +describe('Request', () => { + const parser = new CSVParser() + + test('can parse text', () => { + const text = 'col 1,col 2\n1,2' + const parsed = parser.parse(text) + expect(parsed.meta.fields).toEqual(['col 1', 'col 2']) + expect(parsed.data[0]['col 1']).toEqual('1') + }) +}) diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 3d768fb9..04949c6d 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -6910,6 +6910,11 @@ pako@=1.0.10, pako@~1.0.5: resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.10.tgz#4328badb5086a426aa90f541977d4955da5c9732" integrity sha512-0DTvPVU3ed8+HNXOu5Bs+o//Mbdj9VNQMUOe9oKCwh8l0GNwpTDMKCWbRjgtD291AWnkAgkqA/LOnQS8AmS1tw== +papaparse@^5.0.2: + version "5.0.2" + resolved "https://registry.yarnpkg.com/papaparse/-/papaparse-5.0.2.tgz#447ef95a42015e0b0e52df717b44aedc78252dc9" + integrity sha512-FoaaFfNlCztJ7c+XD1Fgb0zIJ530HwSr6FBfM1mcMzLtIWoTxE5paBNJWiCWFjDrTzGiEG/uIUfqVzgKxqd+Sw== + parallel-transform@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/parallel-transform/-/parallel-transform-1.1.0.tgz#d410f065b05da23081fcd10f28854c29bda33b06"