Skip to content

Commit

Permalink
Update ID mapping and README
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaasonen committed Jul 15, 2019
1 parent d13161e commit 5694d59
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ The fragment ID is parsed from the file name:
- `BM <number>.csv` => `BM.<number without leading zeroes>`
- `AA-BB-CC 0DD.csv.csv` => `18AA,<BB padded to to digits><CC padded to two digits>.<DD without leading zeroes>`
- `AA-BB-CC,DD.csv` => `18AA,<BB padded to to digits><CC padded to two digits>.<DD without leading zeroes>`
- `CBS <id>.csv` => `CBS.<id>`
- `N <id>.csv` => `N.<id>`
- `UM <id>.csv` => `UM.<id>`

References from different files resulting in same fragment ID are combined for the final result.

Expand Down
51 changes: 39 additions & 12 deletions lib/mapNames.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const _ = require('lodash')

class IdMapper {
constructor (pattern, factory) {
this.pattern = pattern
this.factory = factory
this.match = null
}

createMatch (fileName) {
this.match = this.pattern.exec(fileName)
return this.getId()
}

getId () {
return this.match && this.factory(this.match)
}
}

function parseCollection (collection) {
return collection
? ['Rm 2', 'Rm II'].includes(collection)
Expand All @@ -14,6 +31,12 @@ function createCollectionName (match) {
return `${collection}.${number}`
}

function createPennMuseumName (match) {
const collection = parseCollection(match.groups.collection)
const number = match.groups.number
return `${collection}.${number}`
}

function createDateName (match) {
const year = `18${match.groups.year}`
const month = match.groups.month.padStart(2, '0')
Expand All @@ -23,16 +46,20 @@ function createDateName (match) {
}

module.exports = function mapName (fileName) {
const collectionPattern = /^(?:(?<collection>Sm|DT|Rm|Rm 2|Rm II|BM|CBS|N|UM) )?(?<number>\d+)\.csv$/
const collectionMatch = collectionPattern.exec(fileName)
const datePattern = /^(?<year>\d{2})-(?<month>\d{1,2})-(?<day>\d{1,2})[ ,](?<number>\d+)\.csv$/
const dateMatch = datePattern.exec(fileName)

if (collectionMatch) {
return createCollectionName(collectionMatch)
} else if (dateMatch) {
return createDateName(dateMatch)
} else {
return null
}
const id = [
new IdMapper(
/^(?:(?<collection>Sm|DT|Rm|Rm 2|Rm II|BM|CBS|N|UM) )?(?<number>\d+)\.csv$/,
createCollectionName
),
new IdMapper(
/^(?<collection>CBS|N|UM) (?<number>.+)\.csv$/,
createPennMuseumName
),
new IdMapper(
/^(?<year>\d{2})-(?<month>\d{1,2})-(?<day>\d{1,2})[ ,](?<number>\d+)\.csv$/,
createDateName
)
].map(mapper => mapper.createMatch(fileName)).find(_.negate(_.isNil))

return id || null
}
6 changes: 3 additions & 3 deletions lib/mapNames.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ test.each`
${'63-2-9,123.csv'} | ${'1863,0209.123'}
${'BM 12345.csv'} | ${'BM.12345'}
${'BM 123456.csv'} | ${'BM.123456'}
${'CBS 123456.csv'} | ${'CBS.123456'}
${'N 123456.csv'} | ${'N.123456'}
${'UM 123456.csv'} | ${'UM.123456'}
${'CBS 587bis.csv'} | ${'CBS.587bis'}
${'N 708A.csv'} | ${'N.708A'}
${'UM 29-16-055.csv'} | ${'UM.29-16-055'}
${'invalid.xlsx'} | ${null}
`('returns $fragmentId for $fileName', ({ fileName, fragmentId }) => {
expect(mapNames(fileName)).toBe(fragmentId)
Expand Down

0 comments on commit 5694d59

Please sign in to comment.