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"); } } }); it('should not parse words starting with symbols as last name', function() { let author = Zotero.Utilities.cleanAuthor('First Middle Last [CountryName]', false); assert.equal(author.firstName, 'First Middle'); // Brackets at the beginning and end of a string get removed for strings // such as [First Last] -> Last, First. // The current output is not ideal, but better than "[CountryName, First Middle Last" assert.equal(author.lastName, 'Last [CountryName'); }); it('should parse names starting with unicode characters correctly', function() { let author = Zotero.Utilities.cleanAuthor('Ąžuolas Žolynas', false); assert.equal(author.firstName, 'Ąžuolas'); assert.equal(author.lastName, 'Žolynas'); }) }); describe("#isHTTPURL()", function () { it("should return true for HTTP URL", function () { assert.isTrue(Zotero.Utilities.isHTTPURL('http://example.com')); }); it("should return true for HTTPS URL", function () { assert.isTrue(Zotero.Utilities.isHTTPURL('https://example.com')); }); it("should return false for plausible HTTP URL if allowNoScheme not provided", function () { assert.isFalse(Zotero.Utilities.isHTTPURL('example.com')); }); it("should return false for plausible HTTP URL if allowNoScheme is true", function () { assert.isTrue(Zotero.Utilities.isHTTPURL('example.com', true)); }); it("should return false for file URL", function () { assert.isFalse(Zotero.Utilities.isHTTPURL('file:///c:/path/to/file.txt')); }); it("should return false for mailto URLs in allowNoScheme mode", function () { assert.isFalse(Zotero.Utilities.isHTTPURL('mailto:foo@example.com', true)); }); it("should return false for zotero: URL", function () { assert.isFalse(Zotero.Utilities.isHTTPURL('zotero://select/library/items/AAAAAAAA')); }); }); describe("#cleanDOI()", function () { var cleanDOI = Zotero.Utilities.cleanDOI; var doi = '10.1088/1748-9326/11/4/048002'; var shortDOI = '10/aabbe'; it("should parse a DOI", function () { assert.equal(cleanDOI(`${doi}`), doi); }); it("should parse a short DOI", function () { assert.equal(cleanDOI(`${shortDOI}`), shortDOI); }); it("should parse a DOI at the end of a sentence", function () { assert.equal(cleanDOI(`Foo bar ${doi}. Foo bar`), doi); }); // FIXME it.skip("should parse a DOI in parentheses", function () { assert.equal(cleanDOI(`Foo bar (${doi}) foo bar`), doi); }); // FIXME it.skip("should parse a DOI in brackets", function () { assert.equal(cleanDOI(`Foo bar [${doi}] foo bar`), doi); }); }); 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\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("#ellipsize()", function () { describe("with wordBoundary", function () { it("should truncate at word boundary", function* () { assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 3, true), "abc…"); }); it("should trim whitespace after word boundary", function* () { assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 4, true), "abc…"); }); it("should trim characters after word boundary", function () { assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 5, true), "abc…"); }); it("should truncate in the middle of a word", function () { assert.equal(Zotero.Utilities.ellipsize("abcdefghi", 6, true), "abcdef…"); }); it("should truncate at word boundary with previous space within radius", function () { assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 7, true), "abc def…"); }); it("should return string as is if shorter than length", function () { assert.equal(Zotero.Utilities.ellipsize("abcdefg", 8, true), "abcdefg"); }); it("should return string as is if equal to length", function () { assert.equal(Zotero.Utilities.ellipsize("abcdefgh", 8, true), "abcdefgh"); }); }); }); });