describe("Zotero.Utilities", function() { describe("cleanAuthor", function() { it('should parse author names', function() { for(let useComma of [false, true]) { for(let first_expected of [["First", "First"], ["First Middle", "First Middle"], ["F. R. S.", "F. R. S."], ["F.R.S.", "F. R. S."], ["F R S", "F. R. S."], ["FRS", "F. R. S."]]) { let [first, expected] = first_expected; let str = useComma ? "Last, "+first : first+" Last"; let author = Zotero.Utilities.cleanAuthor(str, "author", useComma); assert.equal(author.firstName, expected); assert.equal(author.lastName, "Last"); } } }); }); describe("cleanISBN", function() { let cleanISBN = Zotero.Utilities.cleanISBN; it("should return false for non-ISBN string", function() { assert.isFalse(cleanISBN(''), 'returned false for empty string'); assert.isFalse(cleanISBN('Random String 123'), 'returned false for non-ISBN string'); assert.isFalse(cleanISBN('1234X67890'), 'returned false for ISBN10-looking string with X in the middle'); assert.isFalse(cleanISBN('987123456789X'), 'returned false for ISBN13-looking string with X as check-digit'); }); it("should return false for invalid ISBN string", function() { assert.isFalse(cleanISBN('1234567890'), 'returned false for invalid ISBN10'); assert.isFalse(cleanISBN('9871234567890'), 'returned false for invalid ISBN13'); }); it("should return valid ISBN string given clean, valid ISBN string", function() { assert.equal(cleanISBN('123456789X'), '123456789X', 'passed through valid ISBN10'); assert.equal(cleanISBN('123456789x'), '123456789X', 'passed through valid ISBN10 with lower case input'); assert.equal(cleanISBN('9781234567897'), '9781234567897', 'passed through valid ISBN13'); assert.equal(cleanISBN('9791843123391'), '9791843123391', 'passed through valid ISBN13 in 979 range'); }); it("should strip off internal characters in ISBN string", function() { let ignoredChars = '\x2D\xAD\u2010\u2011\u2012\u2013\u2014\u2015\u2043\u2212' // Dashes + ' \xA0\r\n\t\x0B\x0C\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005' // Spaces + '\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF'; for (let i=0; iISBN:978-1 234\xA056789 - 7(print)\nISBN-10:123\x2D456789X (print)'), '9781234567897'); }); it("should not validate check digit when dontValidate is set", function() { assert.equal(cleanISBN('9781234567890', true), '9781234567890', 'plain ISBN13 with wrong check digit'); assert.equal(cleanISBN('1234567890', true), '1234567890', 'plain ISBN10 with wrong check digit'); assert.equal(cleanISBN('1234567890 9781234567897', true), '1234567890', 'returned first ISBN10 (invalid) in the list with valid and invalid ISBNs'); assert.equal(cleanISBN('9781234567890 123456789X', true), '9781234567890', 'returned first ISBN13 (invalid) in the list with valid and invalid ISBNs'); }); it("should not pass non-ISBN strings if dontValidate is set", function() { assert.isFalse(cleanISBN('', true), 'returned false for empty string'); assert.isFalse(cleanISBN('Random String 123', true), 'returned false for non-ISBN string'); assert.isFalse(cleanISBN('1234X67890', true), 'returned false for ISBN10-looking string with X in the middle'); assert.isFalse(cleanISBN('123456789Y', true), 'returned false for ISBN10-looking string with Y as check digit'); assert.isFalse(cleanISBN('987123456789X', true), 'returned false for ISBN13-looking string with X as check-digit'); assert.isFalse(cleanISBN('1239781234567897', true), 'did not ignore number prefix'); assert.isFalse(cleanISBN('9781234567897123', true), 'did not ignore number suffix'); assert.isFalse(cleanISBN('1239781234567897123', true), 'did not ignore surrounding numbers'); }); }); describe("toISBN13", function() { let toISBN13 = Zotero.Utilities.toISBN13; it("should throw on invalid ISBN", function() { let errorMsg = 'ISBN not found in "', invalidStrings = ['', 'random string', '1234567890123']; for (let i=0; iISSN:1234\xA0-\t5679(print)\neISSN (electronic):0028-0836'), '1234-5679'); }); }); describe("itemToCSLJSON", function() { it("should accept Zotero.Item and Zotero export item format", Zotero.Promise.coroutine(function* () { let data = yield populateDBWithSampleData(loadSampleData('journalArticle')); let item = yield Zotero.Items.getAsync(data.journalArticle.id); let fromZoteroItem; try { fromZoteroItem = yield Zotero.Utilities.itemToCSLJSON(item); } catch(e) { assert.fail(e, null, 'accepts Zotero Item'); } assert.isObject(fromZoteroItem, 'converts Zotero Item to object'); assert.isNotNull(fromZoteroItem, 'converts Zotero Item to non-null object'); let fromExportItem; try { fromExportItem = Zotero.Utilities.itemToCSLJSON( yield Zotero.Utilities.Internal.itemToExportFormat(item) ); } catch(e) { assert.fail(e, null, 'accepts Zotero export item'); } assert.isObject(fromExportItem, 'converts Zotero export item to object'); assert.isNotNull(fromExportItem, 'converts Zotero export item to non-null object'); assert.deepEqual(fromZoteroItem, fromExportItem, 'conversion from Zotero Item and from export item are the same'); })); it("should convert standalone notes to expected format", Zotero.Promise.coroutine(function* () { let note = new Zotero.Item('note'); note.setNote('Some note longer than 50 characters, which will become the title.'); yield note.saveTx(); let cslJSONNote = yield Zotero.Utilities.itemToCSLJSON(note); assert.equal(cslJSONNote.type, 'article', 'note is exported as "article"'); assert.equal(cslJSONNote.title, note.getNoteTitle(), 'note title is set to Zotero pseudo-title'); })); it("should convert standalone attachments to expected format", Zotero.Promise.coroutine(function* () { let file = getTestDataDirectory(); file.append("empty.pdf"); let attachment = yield Zotero.Attachments.importFromFile({"file":file}); attachment.setField('title', 'Empty'); attachment.setField('accessDate', '2001-02-03 12:13:14'); attachment.setField('url', 'http://example.com'); attachment.setNote('Note'); yield attachment.saveTx(); cslJSONAttachment = yield Zotero.Utilities.itemToCSLJSON(attachment); assert.equal(cslJSONAttachment.type, 'article', 'attachment is exported as "article"'); assert.equal(cslJSONAttachment.title, 'Empty', 'attachment title is correct'); assert.deepEqual(cslJSONAttachment.accessed, {"date-parts":[["2001",2,3]]}, 'attachment access date is mapped correctly'); })); it("should refuse to convert unexpected item types", Zotero.Promise.coroutine(function* () { let data = yield populateDBWithSampleData(loadSampleData('journalArticle')); let item = yield Zotero.Items.getAsync(data.journalArticle.id); let exportFormat = Zotero.Utilities.Internal.itemToExportFormat(item); exportFormat.itemType = 'foo'; assert.throws(Zotero.Utilities.itemToCSLJSON.bind(Zotero.Utilities, exportFormat), /^Unexpected Zotero Item type ".*"$/, 'throws an error when trying to map invalid item types'); })); it("should map additional fields from Extra field", Zotero.Promise.coroutine(function* () { let item = new Zotero.Item('journalArticle'); item.setField('extra', 'PMID: 12345\nPMCID:123456'); yield item.saveTx(); let cslJSON = yield Zotero.Utilities.itemToCSLJSON(item); assert.equal(cslJSON.PMID, '12345', 'PMID from Extra is mapped to PMID'); assert.equal(cslJSON.PMCID, '123456', 'PMCID from Extra is mapped to PMCID'); item.setField('extra', 'PMID: 12345'); yield item.saveTx(); cslJSON = yield Zotero.Utilities.itemToCSLJSON(item); assert.equal(cslJSON.PMID, '12345', 'single-line entry is extracted correctly'); item.setField('extra', 'some junk: note\nPMID: 12345\nstuff in-between\nPMCID: 123456\nlast bit of junk!'); yield item.saveTx(); cslJSON = yield Zotero.Utilities.itemToCSLJSON(item); assert.equal(cslJSON.PMID, '12345', 'PMID from mixed Extra field is mapped to PMID'); assert.equal(cslJSON.PMCID, '123456', 'PMCID from mixed Extra field is mapped to PMCID'); item.setField('extra', 'a\n PMID: 12345\nfoo PMCID: 123456'); yield item.saveTx(); cslJSON = yield Zotero.Utilities.itemToCSLJSON(item); assert.isUndefined(cslJSON.PMCID, 'field label must not be preceded by other text'); assert.isUndefined(cslJSON.PMID, 'field label must not be preceded by a space'); assert.equal(cslJSON.note, 'a\n PMID: 12345\nfoo PMCID: 123456', 'note is left untouched if nothing is extracted'); item.setField('extra', 'something\npmid: 12345\n'); yield item.saveTx(); cslJSON = yield Zotero.Utilities.itemToCSLJSON(item); assert.isUndefined(cslJSON.PMID, 'field labels are case-sensitive'); })); }); });