From 3e1f77d5a2362251f49ceafc55710e3cc0d4696d Mon Sep 17 00:00:00 2001 From: Jussi Laasonen Date: Fri, 6 Jul 2018 16:26:24 +0200 Subject: [PATCH] Parse measures to value and note --- lib/parseCsv.js | 15 ++++++++++++++- spec/csvHelpers.js | 11 +++++++---- spec/parseCsv.spec.js | 12 ++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/parseCsv.js b/lib/parseCsv.js index f057bd8..f763114 100644 --- a/lib/parseCsv.js +++ b/lib/parseCsv.js @@ -32,6 +32,16 @@ function parseRecord (record) { })) } +function parseMeasure (measure) { + const match = /^(?\d+,?\d*)(?.*)$/.exec(measure) + return match + ? { + value: Number(match.groups.value.replace(',', '.')), + note: match.groups.note.trim() + } + : {} +} + module.exports = async function parseCsv (csv) { return new Promise((resolve, reject) => { Papa.parse(csv, { @@ -42,7 +52,10 @@ module.exports = async function parseCsv (csv) { joins: parseJoins(rawObject.joins), folio: parseFolio(rawObject.folio), record: parseRecord(rawObject.record), - script: parseScript(rawObject.script) + script: parseScript(rawObject.script), + length: parseMeasure(rawObject.length), + width: parseMeasure(rawObject.width), + thickness: parseMeasure(rawObject.thickness) }))) } }) diff --git a/spec/csvHelpers.js b/spec/csvHelpers.js index dac0c2d..ec55423 100644 --- a/spec/csvHelpers.js +++ b/spec/csvHelpers.js @@ -9,9 +9,9 @@ const csvDefaults = { joins: '', subcollection: 'subcollection', description: 'description', - length: 'length', - width: 'width', - thickness: 'thickness', + length: '', + width: '', + thickness: '', collection: 'collection', script: 'script', date: 'date', @@ -26,7 +26,10 @@ const objectDefaults = { ...csvDefaults, joins: [], folio: [], - record: [] + record: [], + width: {}, + length: {}, + thickness: {} } function buildObject (params) { diff --git a/spec/parseCsv.spec.js b/spec/parseCsv.spec.js index 1ec3f58..37de7c3 100644 --- a/spec/parseCsv.spec.js +++ b/spec/parseCsv.spec.js @@ -16,6 +16,18 @@ describe('parseCsv', () => { expectCsv({}).toParseTo({}) }) + it('length', () => { + expectCsv({length: '0,7 (notes)'}).toParseTo({length: {value: 0.7, note: '(notes)'}}) + }) + + it('width', () => { + expectCsv({width: '1,3'}).toParseTo({width: {value: 1.3, note: ''}}) + }) + + it('width', () => { + expectCsv({width: '13,0 (notes)'}).toParseTo({width: {value: 13.0, note: '(notes)'}}) + }) + it('parses + joins', () => { expectCsv({joins: 'join1 + join2 + join3'}).toParseTo({joins: ['join1', 'join2', 'join3']}) })