Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Parse measures to value and note
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaasonen committed Jul 6, 2018
1 parent 82c640a commit 3e1f77d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
15 changes: 14 additions & 1 deletion lib/parseCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ function parseRecord (record) {
}))
}

function parseMeasure (measure) {
const match = /^(?<value>\d+,?\d*)(?<note>.*)$/.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, {
Expand All @@ -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)
})))
}
})
Expand Down
11 changes: 7 additions & 4 deletions spec/csvHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -26,7 +26,10 @@ const objectDefaults = {
...csvDefaults,
joins: [],
folio: [],
record: []
record: [],
width: {},
length: {},
thickness: {}
}

function buildObject (params) {
Expand Down
12 changes: 12 additions & 0 deletions spec/parseCsv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']})
})
Expand Down

0 comments on commit 3e1f77d

Please sign in to comment.