diff --git a/server/utils/parsers/parseOpfMetadata.js b/server/utils/parsers/parseOpfMetadata.js index 4c057197..b51ceea5 100644 --- a/server/utils/parsers/parseOpfMetadata.js +++ b/server/utils/parsers/parseOpfMetadata.js @@ -103,12 +103,11 @@ function fetchSeries(metadataMeta) { if (!metadataMeta) return [] const result = [] for (let i = 0; i < metadataMeta.length; i++) { - if (metadataMeta[i].$.name === "calibre:series") { - const name = metadataMeta[i].$.content + if (metadataMeta[i].$?.name === "calibre:series" && metadataMeta[i].$.content?.trim()) { + const name = metadataMeta[i].$.content.trim() let sequence = null - if (i + 1 < metadataMeta.length && - metadataMeta[i + 1].$.name === "calibre:series_index" && metadataMeta[i + 1].$.content) { - sequence = metadataMeta[i + 1].$.content + if (metadataMeta[i + 1]?.$?.name === "calibre:series_index" && metadataMeta[i + 1].$?.content?.trim()) { + sequence = metadataMeta[i + 1].$.content.trim() } result.push({ name, sequence }) } diff --git a/test/server/utils/parsers/parseOpfMetadata.test.js b/test/server/utils/parsers/parseOpfMetadata.test.js index c0732273..f1d5ce89 100644 --- a/test/server/utils/parsers/parseOpfMetadata.test.js +++ b/test/server/utils/parsers/parseOpfMetadata.test.js @@ -2,27 +2,26 @@ const chai = require('chai') const expect = chai.expect const { parseOpfMetadataXML } = require('../../../../server/utils/parsers/parseOpfMetadata') - -describe('parseOpfMetadata - test series', async () => { - it('test one serie', async() => { +describe('parseOpfMetadata - test series', async () => { + it('test one series', async () => { const opf = ` - + - + ` const parsedOpf = await parseOpfMetadataXML(opf) - expect(parsedOpf.series).to.deep.equal([{"name": "Serie","sequence": "1"}]) + expect(parsedOpf.series).to.deep.equal([{ "name": "Serie", "sequence": "1" }]) }) - it('test more then 1 serie - in correct order', async() => { + it('test more then 1 series - in correct order', async () => { const opf = ` - + @@ -30,41 +29,41 @@ describe('parseOpfMetadata - test series', async () => { - + ` const parsedOpf = await parseOpfMetadataXML(opf) expect(parsedOpf.series).to.deep.equal([ - {"name": "Serie 1","sequence": "1"}, - {"name": "Serie 2","sequence": "2"}, - {"name": "Serie 3","sequence": "3"}, + { "name": "Serie 1", "sequence": "1" }, + { "name": "Serie 2", "sequence": "2" }, + { "name": "Serie 3", "sequence": "3" }, ]) }) - it('test messed order of series content and index', async() => { + it('test messed order of series content and index', async () => { const opf = ` - + - + ` const parsedOpf = await parseOpfMetadataXML(opf) expect(parsedOpf.series).to.deep.equal([ - {"name": "Serie 1","sequence": "1"}, - {"name": "Serie 3","sequence": null}, + { "name": "Serie 1", "sequence": "1" }, + { "name": "Serie 3", "sequence": null }, ]) }) - it('test different values of series content and index', async() => { + it('test different values of series content and index', async () => { const opf = ` - + @@ -72,14 +71,43 @@ describe('parseOpfMetadata - test series', async () => { - + ` const parsedOpf = await parseOpfMetadataXML(opf) - // console.log(JSON.stringify(parsedOpf, null, 4)) expect(parsedOpf.series).to.deep.equal([ - {"name": "Serie 1", "sequence": null}, - {"name": "Serie 2", "sequence": "abc"}, - {"name": "Serie 3", "sequence": null}, + { "name": "Serie 1", "sequence": null }, + { "name": "Serie 2", "sequence": "abc" }, + { "name": "Serie 3", "sequence": null }, + ]) + }) + + it('test empty series content', async () => { + const opf = ` + + + + + + + + ` + const parsedOpf = await parseOpfMetadataXML(opf) + expect(parsedOpf.series).to.deep.equal([]) + }) + + it('test series and index using an xml namespace', async () => { + const opf = ` + + + + + + + + ` + const parsedOpf = await parseOpfMetadataXML(opf) + expect(parsedOpf.series).to.deep.equal([ + { "name": "Serie 1", "sequence": null } ]) }) })