diff --git a/README.md b/README.md index ac0d6aa..d24e20d 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ The fragment ID is parsed from the file name: - `BM .csv` => `BM.` - `AA-BB-CC 0DD.csv.csv` => `18AA,.
` - `AA-BB-CC,DD.csv` => `18AA,.
` +- `CBS .csv` => `CBS.` +- `N .csv` => `N.` +- `UM .csv` => `UM.` References from different files resulting in same fragment ID are combined for the final result. diff --git a/lib/mapNames.js b/lib/mapNames.js index 185d5c4..13b0e5d 100644 --- a/lib/mapNames.js +++ b/lib/mapNames.js @@ -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) @@ -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') @@ -23,16 +46,20 @@ function createDateName (match) { } module.exports = function mapName (fileName) { - const collectionPattern = /^(?:(?Sm|DT|Rm|Rm 2|Rm II|BM|CBS|N|UM) )?(?\d+)\.csv$/ - const collectionMatch = collectionPattern.exec(fileName) - const datePattern = /^(?\d{2})-(?\d{1,2})-(?\d{1,2})[ ,](?\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( + /^(?:(?Sm|DT|Rm|Rm 2|Rm II|BM|CBS|N|UM) )?(?\d+)\.csv$/, + createCollectionName + ), + new IdMapper( + /^(?CBS|N|UM) (?.+)\.csv$/, + createPennMuseumName + ), + new IdMapper( + /^(?\d{2})-(?\d{1,2})-(?\d{1,2})[ ,](?\d+)\.csv$/, + createDateName + ) + ].map(mapper => mapper.createMatch(fileName)).find(_.negate(_.isNil)) + + return id || null } diff --git a/lib/mapNames.test.js b/lib/mapNames.test.js index 11207ca..501e8a5 100644 --- a/lib/mapNames.test.js +++ b/lib/mapNames.test.js @@ -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)