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"