utilitiesTest.js (21795B)
1 describe("Zotero.Utilities", function() { 2 describe("cleanAuthor", function() { 3 it('should parse author names', function() { 4 for(let useComma of [false, true]) { 5 for(let first_expected of [["First", "First"], 6 ["First Middle", "First Middle"], 7 ["F. R. S.", "F. R. S."], 8 ["F.R.S.", "F. R. S."], 9 ["F R S", "F. R. S."], 10 ["FRS", "F. R. S."]]) { 11 let [first, expected] = first_expected; 12 let str = useComma ? "Last, "+first : first+" Last"; 13 let author = Zotero.Utilities.cleanAuthor(str, "author", useComma); 14 assert.equal(author.firstName, expected); 15 assert.equal(author.lastName, "Last"); 16 } 17 } 18 }); 19 20 it('should not parse words starting with symbols as last name', function() { 21 let author = Zotero.Utilities.cleanAuthor('First Middle Last [CountryName]', false); 22 assert.equal(author.firstName, 'First Middle'); 23 // Brackets at the beginning and end of a string get removed for strings 24 // such as [First Last] -> Last, First. 25 // The current output is not ideal, but better than "[CountryName, First Middle Last" 26 assert.equal(author.lastName, 'Last [CountryName'); 27 }); 28 29 it('should parse names starting with unicode characters correctly', function() { 30 let author = Zotero.Utilities.cleanAuthor('Ąžuolas Žolynas', false); 31 assert.equal(author.firstName, 'Ąžuolas'); 32 assert.equal(author.lastName, 'Žolynas'); 33 }) 34 }); 35 36 37 describe("#cleanDOI()", function () { 38 var cleanDOI = Zotero.Utilities.cleanDOI; 39 var doi = '10.1088/1748-9326/11/4/048002'; 40 var shortDOI = '10/aabbe'; 41 42 it("should parse a DOI", function () { 43 assert.equal(cleanDOI(`${doi}`), doi); 44 }); 45 46 it("should parse a short DOI", function () { 47 assert.equal(cleanDOI(`${shortDOI}`), shortDOI); 48 }); 49 50 it("should parse a DOI at the end of a sentence", function () { 51 assert.equal(cleanDOI(`Foo bar ${doi}. Foo bar`), doi); 52 }); 53 54 // FIXME 55 it.skip("should parse a DOI in parentheses", function () { 56 assert.equal(cleanDOI(`Foo bar (${doi}) foo bar`), doi); 57 }); 58 59 // FIXME 60 it.skip("should parse a DOI in brackets", function () { 61 assert.equal(cleanDOI(`Foo bar [${doi}] foo bar`), doi); 62 }); 63 }); 64 65 66 describe("#cleanISBN()", function() { 67 let cleanISBN = Zotero.Utilities.cleanISBN; 68 it("should return false for non-ISBN string", function() { 69 assert.isFalse(cleanISBN(''), 'returned false for empty string'); 70 assert.isFalse(cleanISBN('Random String 123'), 'returned false for non-ISBN string'); 71 assert.isFalse(cleanISBN('1234X67890'), 'returned false for ISBN10-looking string with X in the middle'); 72 assert.isFalse(cleanISBN('987123456789X'), 'returned false for ISBN13-looking string with X as check-digit'); 73 }); 74 it("should return false for invalid ISBN string", function() { 75 assert.isFalse(cleanISBN('1234567890'), 'returned false for invalid ISBN10'); 76 assert.isFalse(cleanISBN('9871234567890'), 'returned false for invalid ISBN13'); 77 }); 78 it("should return valid ISBN string given clean, valid ISBN string", function() { 79 assert.equal(cleanISBN('123456789X'), '123456789X', 'passed through valid ISBN10'); 80 assert.equal(cleanISBN('123456789x'), '123456789X', 'passed through valid ISBN10 with lower case input'); 81 assert.equal(cleanISBN('9781234567897'), '9781234567897', 'passed through valid ISBN13'); 82 assert.equal(cleanISBN('9791843123391'), '9791843123391', 'passed through valid ISBN13 in 979 range'); 83 }); 84 it("should strip off internal characters in ISBN string", function() { 85 let ignoredChars = '\x2D\xAD\u2010\u2011\u2012\u2013\u2014\u2015\u2043\u2212' // Dashes 86 + ' \xA0\r\n\t\x0B\x0C\u1680\u2000\u2001\u2002\u2003\u2004\u2005' // Spaces 87 + '\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF'; 88 for (let i=0; i<ignoredChars.length; i++) { 89 let charCode = '\\u' + Zotero.Utilities.lpad(ignoredChars.charCodeAt(i).toString(16).toUpperCase(), '0', 4); 90 assert.equal(cleanISBN('9781' + ignoredChars.charAt(i) + '234567897'), '9781234567897', 'stripped off ' + charCode); 91 } 92 assert.equal(cleanISBN('9781' + ignoredChars + '234567897'), '9781234567897', 'stripped off all ignored characters'); 93 94 let isbnChars = ignoredChars + '1234567890'; 95 for (let i=1; i<1327; i++) { // More common characters through Cyrillic letters 96 let c = String.fromCharCode(i); 97 if (isbnChars.indexOf(c) != -1) continue; 98 99 let charCode = '\\u' + Zotero.Utilities.lpad(i.toString(16).toUpperCase(), '0', 4); 100 assert.isFalse(cleanISBN('9781' + c + '234567897'), 'did not ignore internal character ' + charCode); 101 } 102 }); 103 it("should strip off surrounding non-ISBN string", function() { 104 assert.equal(cleanISBN('ISBN 9781234567897'), '9781234567897', 'stripped off preceding string (with space)'); 105 assert.equal(cleanISBN('ISBN:9781234567897'), '9781234567897', 'stripped off preceding string (without space)'); 106 assert.equal(cleanISBN('9781234567897 ISBN13'), '9781234567897', 'stripped off trailing string (with space)'); 107 assert.equal(cleanISBN('9781234567897(ISBN13)'), '9781234567897', 'stripped off trailing string (without space)'); 108 assert.equal(cleanISBN('ISBN13:9781234567897 (print)'), '9781234567897', 'stripped off surrounding string'); 109 assert.equal(cleanISBN('978 9781234567 897'), '9781234567897', 'stripped off pseudo-ISBN prefix'); 110 }); 111 it("should return the first valid ISBN from a string with multiple ISBNs", function() { 112 assert.equal(cleanISBN('9781234567897, 9791843123391'), '9781234567897', 'returned first valid ISBN13 from list of valid ISBN13s'); 113 assert.equal(cleanISBN('123456789X, 0199535922'), '123456789X', 'returned first valid ISBN13 from list of valid ISBN13s'); 114 assert.equal(cleanISBN('123456789X 9781234567897'), '123456789X', 'returned first valid ISBN (10) from a list of mixed-length ISBNs'); 115 assert.equal(cleanISBN('9781234567897 123456789X'), '9781234567897', 'returned first valid ISBN (13) from a list of mixed-length ISBNs'); 116 assert.equal(cleanISBN('1234567890 9781234567897'), '9781234567897', 'returned first valid ISBN in the list with valid and invalid ISBNs'); 117 }); 118 it("should not return an ISBN from a middle of a longer number string", function() { 119 assert.isFalse(cleanISBN('1239781234567897'), 'did not ignore number prefix'); 120 assert.isFalse(cleanISBN('9781234567897123'), 'did not ignore number suffix'); 121 assert.isFalse(cleanISBN('1239781234567897123'), 'did not ignore surrounding numbers'); 122 }); 123 it("should return valid ISBN from a dirty string", function() { 124 assert.equal(cleanISBN('<b>ISBN</b>:978-1 234\xA056789 - 7(print)\n<b>ISBN-10</b>:123\x2D456789X (print)'), '9781234567897'); 125 }); 126 it("should not validate check digit when dontValidate is set", function() { 127 assert.equal(cleanISBN('9781234567890', true), '9781234567890', 'plain ISBN13 with wrong check digit'); 128 assert.equal(cleanISBN('1234567890', true), '1234567890', 'plain ISBN10 with wrong check digit'); 129 assert.equal(cleanISBN('1234567890 9781234567897', true), '1234567890', 'returned first ISBN10 (invalid) in the list with valid and invalid ISBNs'); 130 assert.equal(cleanISBN('9781234567890 123456789X', true), '9781234567890', 'returned first ISBN13 (invalid) in the list with valid and invalid ISBNs'); 131 }); 132 it("should not pass non-ISBN strings if dontValidate is set", function() { 133 assert.isFalse(cleanISBN('', true), 'returned false for empty string'); 134 assert.isFalse(cleanISBN('Random String 123', true), 'returned false for non-ISBN string'); 135 assert.isFalse(cleanISBN('1234X67890', true), 'returned false for ISBN10-looking string with X in the middle'); 136 assert.isFalse(cleanISBN('123456789Y', true), 'returned false for ISBN10-looking string with Y as check digit'); 137 assert.isFalse(cleanISBN('987123456789X', true), 'returned false for ISBN13-looking string with X as check-digit'); 138 assert.isFalse(cleanISBN('1239781234567897', true), 'did not ignore number prefix'); 139 assert.isFalse(cleanISBN('9781234567897123', true), 'did not ignore number suffix'); 140 assert.isFalse(cleanISBN('1239781234567897123', true), 'did not ignore surrounding numbers'); 141 }); 142 }); 143 describe("toISBN13", function() { 144 let toISBN13 = Zotero.Utilities.toISBN13; 145 it("should throw on invalid ISBN", function() { 146 let errorMsg = 'ISBN not found in "', 147 invalidStrings = ['', 'random string', '1234567890123']; 148 for (let i=0; i<invalidStrings.length; i++) { 149 assert.throws(toISBN13.bind(null,invalidStrings[i]), errorMsg + invalidStrings[i] + '"'); 150 } 151 }); 152 it("should convert to ISBN13", function() { 153 assert.equal(toISBN13('123456789X'), '9781234567897', 'converts ISBN10 to ISBN13'); 154 assert.equal(toISBN13('9781234567897'), '9781234567897', 'ISBN13 stays the same'); 155 assert.equal(toISBN13('9791843123391'), '9791843123391', '979 ISBN13 stays the same'); 156 assert.equal(toISBN13('978-1234567897'), '9781234567897', 'accepts hyphenated ISBN'); 157 }); 158 it("should ignore invalid check digit", function() { 159 assert.equal(toISBN13('1234567890'), '9781234567897', 'converts ISBN10 with invalid check digit to ISBN13'); 160 assert.equal(toISBN13('9781234567890'), '9781234567897', 'corrects invalid ISBN13 check digit'); 161 }); 162 }); 163 describe("cleanISSN", function() { 164 let cleanISSN = Zotero.Utilities.cleanISSN; 165 it("should return false for non-ISSN string", function() { 166 assert.isFalse(cleanISSN(''), 'returned false for empty string'); 167 assert.isFalse(cleanISSN('Random String 123'), 'returned false for non-ISSN string'); 168 assert.isFalse(cleanISSN('123X-5679'), 'returned false for ISSN-looking string with X in the middle'); 169 }); 170 it("should return false for invalid ISSN string", function() { 171 assert.isFalse(cleanISSN('12345678'), 'returned false for invalid ISSN'); 172 assert.isFalse(cleanISSN('1234-5678'), 'returned false for invalid ISSN with hyphen'); 173 }); 174 it("should return valid ISSN string given clean, valid ISSN string", function() { 175 assert.equal(cleanISSN('1234-5679'), '1234-5679', 'passed through valid ISSN'); 176 assert.equal(cleanISSN('2090-424X'), '2090-424X', 'passed through valid ISSN with X check digit'); 177 }); 178 it("should hyphenate valid ISSN", function() { 179 assert.equal(cleanISSN('12345679'), '1234-5679', 'hyphenated valid ISSN'); 180 }); 181 it("should strip off internal characters in ISSN string", function() { 182 let ignoredChars = '\x2D\xAD\u2010\u2011\u2012\u2013\u2014\u2015\u2043\u2212' // Dashes 183 + ' \xA0\r\n\t\x0B\x0C\u1680\u2000\u2001\u2002\u2003\u2004\u2005' // Spaces 184 + '\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF'; 185 for (let i=0; i<ignoredChars.length; i++) { 186 let charCode = '\\u' + Zotero.Utilities.lpad(ignoredChars.charCodeAt(i).toString(16).toUpperCase(), '0', 4); 187 assert.equal(cleanISSN('1' + ignoredChars.charAt(i) + '2345679'), '1234-5679', 'stripped off ' + charCode); 188 } 189 assert.equal(cleanISSN('1' + ignoredChars + '2345679'), '1234-5679', 'stripped off all ignored characters'); 190 191 let isbnChars = ignoredChars + '1234567890'; 192 for (let i=1; i<1327; i++) { // More common characters through Cyrillic letters 193 let c = String.fromCharCode(i); 194 if (isbnChars.indexOf(c) != -1) continue; 195 196 let charCode = '\\u' + Zotero.Utilities.lpad(i.toString(16).toUpperCase(), '0', 4); 197 assert.isFalse(cleanISSN('1' + c + '2345679'), 'did not ignore internal character ' + charCode); 198 } 199 }); 200 it("should strip off surrounding non-ISSN string", function() { 201 assert.equal(cleanISSN('ISSN 1234-5679'), '1234-5679', 'stripped off preceding string (with space)'); 202 assert.equal(cleanISSN('ISSN:1234-5679'), '1234-5679', 'stripped off preceding string (without space)'); 203 assert.equal(cleanISSN('1234-5679 ISSN'), '1234-5679', 'stripped off trailing string (with space)'); 204 assert.equal(cleanISSN('1234-5679(ISSN)'), '1234-5679', 'stripped off trailing string (without space)'); 205 assert.equal(cleanISSN('ISSN:1234-5679 (print)'), '1234-5679', 'stripped off surrounding string'); 206 assert.equal(cleanISSN('123 12345 679'), '1234-5679', 'stripped off pseudo-ISSN prefix'); 207 }); 208 it("should return the first valid ISSN from a string with multiple ISSNs", function() { 209 assert.equal(cleanISSN('1234-5679, 0028-0836'), '1234-5679', 'returned first valid ISSN from list of valid ISSNs'); 210 assert.equal(cleanISSN('1234-5678, 0028-0836'), '0028-0836', 'returned first valid ISSN in the list with valid and invalid ISSNs'); 211 }); 212 it("should not return an ISSN from a middle of a longer number string", function() { 213 assert.isFalse(cleanISSN('12312345679'), 'did not ignore number prefix'); 214 assert.isFalse(cleanISSN('12345679123'), 'did not ignore number suffix'); 215 assert.isFalse(cleanISSN('12312345679123'), 'did not ignore surrounding numbers'); 216 }); 217 it("should return valid ISSN from a dirty string", function() { 218 assert.equal(cleanISSN('<b>ISSN</b>:1234\xA0-\t5679(print)\n<b>eISSN (electronic)</b>:0028-0836'), '1234-5679'); 219 }); 220 }); 221 describe("itemToCSLJSON", function() { 222 it("should accept Zotero.Item and Zotero export item format", Zotero.Promise.coroutine(function* () { 223 let data = yield populateDBWithSampleData(loadSampleData('journalArticle')); 224 let item = yield Zotero.Items.getAsync(data.journalArticle.id); 225 226 let fromZoteroItem; 227 try { 228 fromZoteroItem = Zotero.Utilities.itemToCSLJSON(item); 229 } catch(e) { 230 assert.fail(e, null, 'accepts Zotero Item'); 231 } 232 assert.isObject(fromZoteroItem, 'converts Zotero Item to object'); 233 assert.isNotNull(fromZoteroItem, 'converts Zotero Item to non-null object'); 234 235 236 let fromExportItem; 237 try { 238 fromExportItem = Zotero.Utilities.itemToCSLJSON( 239 Zotero.Utilities.Internal.itemToExportFormat(item) 240 ); 241 } catch(e) { 242 assert.fail(e, null, 'accepts Zotero export item'); 243 } 244 assert.isObject(fromExportItem, 'converts Zotero export item to object'); 245 assert.isNotNull(fromExportItem, 'converts Zotero export item to non-null object'); 246 247 assert.deepEqual(fromZoteroItem, fromExportItem, 'conversion from Zotero Item and from export item are the same'); 248 })); 249 it("should convert standalone notes to expected format", Zotero.Promise.coroutine(function* () { 250 let note = new Zotero.Item('note'); 251 note.setNote('Some note longer than 50 characters, which will become the title.'); 252 yield note.saveTx(); 253 254 let cslJSONNote = Zotero.Utilities.itemToCSLJSON(note); 255 assert.equal(cslJSONNote.type, 'article', 'note is exported as "article"'); 256 assert.equal(cslJSONNote.title, note.getNoteTitle(), 'note title is set to Zotero pseudo-title'); 257 })); 258 it("should convert standalone attachments to expected format", Zotero.Promise.coroutine(function* () { 259 let file = getTestDataDirectory(); 260 file.append("empty.pdf"); 261 262 let attachment = yield Zotero.Attachments.importFromFile({"file":file}); 263 attachment.setField('title', 'Empty'); 264 attachment.setField('accessDate', '2001-02-03 12:13:14'); 265 attachment.setField('url', 'http://example.com'); 266 attachment.setNote('Note'); 267 268 yield attachment.saveTx(); 269 270 let cslJSONAttachment = Zotero.Utilities.itemToCSLJSON(attachment); 271 assert.equal(cslJSONAttachment.type, 'article', 'attachment is exported as "article"'); 272 assert.equal(cslJSONAttachment.title, 'Empty', 'attachment title is correct'); 273 assert.deepEqual(cslJSONAttachment.accessed, {"date-parts":[["2001",2,3]]}, 'attachment access date is mapped correctly'); 274 })); 275 it("should refuse to convert unexpected item types", Zotero.Promise.coroutine(function* () { 276 let data = yield populateDBWithSampleData(loadSampleData('journalArticle')); 277 let item = yield Zotero.Items.getAsync(data.journalArticle.id); 278 279 let exportFormat = Zotero.Utilities.Internal.itemToExportFormat(item); 280 exportFormat.itemType = 'foo'; 281 282 assert.throws(Zotero.Utilities.itemToCSLJSON.bind(Zotero.Utilities, exportFormat), /^Unexpected Zotero Item type ".*"$/, 'throws an error when trying to map invalid item types'); 283 })); 284 285 it("should parse particles in creator names", function* () { 286 let creators = [ 287 { 288 // No particles 289 firstName: 'John', 290 lastName: 'Smith', 291 creatorType: 'author', 292 expect: { 293 given: 'John', 294 family: 'Smith' 295 } 296 }, 297 { 298 // dropping and non-dropping 299 firstName: 'Jean de', 300 lastName: 'la Fontaine', 301 creatorType: 'author', 302 expect: { 303 given: 'Jean', 304 "dropping-particle": 'de', 305 "non-dropping-particle": 'la', 306 family: 'Fontaine' 307 } 308 }, 309 { 310 // only non-dropping 311 firstName: 'Vincent', 312 lastName: 'van Gogh', 313 creatorType: 'author', 314 expect: { 315 given: 'Vincent', 316 "non-dropping-particle": 'van', 317 family: 'Gogh' 318 } 319 }, 320 { 321 // only dropping 322 firstName: 'Alexander von', 323 lastName: 'Humboldt', 324 creatorType: 'author', 325 expect: { 326 given: 'Alexander', 327 "dropping-particle": 'von', 328 family: 'Humboldt' 329 } 330 }, 331 { 332 // institutional author 333 lastName: 'Jean de la Fontaine', 334 creatorType: 'author', 335 fieldMode: 1, 336 expect: { 337 literal: 'Jean de la Fontaine' 338 } 339 }, 340 { 341 // protected last name 342 firstName: 'Jean de', 343 lastName: '"la Fontaine"', 344 creatorType: 'author', 345 expect: { 346 given: 'Jean de', 347 family: 'la Fontaine' 348 } 349 } 350 ]; 351 352 let data = yield populateDBWithSampleData({ 353 item: { 354 itemType: 'journalArticle', 355 creators: creators 356 } 357 }); 358 359 let item = Zotero.Items.get(data.item.id); 360 let cslCreators = Zotero.Utilities.itemToCSLJSON(item).author; 361 362 assert.deepEqual(cslCreators[0], creators[0].expect, 'simple name is not parsed'); 363 assert.deepEqual(cslCreators[1], creators[1].expect, 'name with dropping and non-dropping particles is parsed'); 364 assert.deepEqual(cslCreators[2], creators[2].expect, 'name with only non-dropping particle is parsed'); 365 assert.deepEqual(cslCreators[3], creators[3].expect, 'name with only dropping particle is parsed'); 366 assert.deepEqual(cslCreators[4], creators[4].expect, 'institutional author is not parsed'); 367 assert.deepEqual(cslCreators[5], creators[5].expect, 'protected last name prevents parsing'); 368 }); 369 }); 370 describe("itemFromCSLJSON", function () { 371 it("should stably perform itemToCSLJSON -> itemFromCSLJSON -> itemToCSLJSON", function* () { 372 this.timeout(10000); 373 let data = loadSampleData('citeProcJSExport'); 374 375 for (let i in data) { 376 let json = data[i]; 377 378 let item = new Zotero.Item(); 379 Zotero.Utilities.itemFromCSLJSON(item, json); 380 yield item.saveTx(); 381 382 let newJSON = Zotero.Utilities.itemToCSLJSON(item); 383 384 delete newJSON.id; 385 delete json.id; 386 387 assert.deepEqual(newJSON, json, i + ' export -> import -> export is stable'); 388 } 389 390 }); 391 it("should import exported standalone note", function* () { 392 let note = new Zotero.Item('note'); 393 note.setNote('Some note longer than 50 characters, which will become the title.'); 394 yield note.saveTx(); 395 396 let jsonNote = Zotero.Utilities.itemToCSLJSON(note); 397 398 let item = new Zotero.Item(); 399 Zotero.Utilities.itemFromCSLJSON(item, jsonNote); 400 401 assert.equal(item.getField('title'), jsonNote.title, 'title imported correctly'); 402 }); 403 it("should import exported standalone attachment", function* () { 404 let attachment = yield importFileAttachment("empty.pdf"); 405 attachment.setField('title', 'Empty'); 406 attachment.setField('accessDate', '2001-02-03 12:13:14'); 407 attachment.setField('url', 'http://example.com'); 408 attachment.setNote('Note'); 409 yield attachment.saveTx(); 410 411 let jsonAttachment = Zotero.Utilities.itemToCSLJSON(attachment); 412 413 let item = new Zotero.Item(); 414 Zotero.Utilities.itemFromCSLJSON(item, jsonAttachment); 415 416 assert.equal(item.getField('title'), jsonAttachment.title, 'title imported correctly'); 417 }); 418 // For Zotero.Item created in translation sandbox in connectors 419 it("should not depend on Zotero.Item existing", function* () { 420 let item = new Zotero.Item; 421 var Item = Zotero.Item; 422 delete Zotero.Item; 423 assert.throws(() => "" instanceof Zotero.Item); 424 425 let data = loadSampleData('citeProcJSExport'); 426 assert.doesNotThrow(Zotero.Utilities.itemFromCSLJSON.bind(Zotero.Utilities, item, Object.values(data)[0])); 427 428 Zotero.Item = Item; 429 assert.doesNotThrow(() => "" instanceof Zotero.Item); 430 }) 431 }); 432 433 describe("#ellipsize()", function () { 434 describe("with wordBoundary", function () { 435 it("should truncate at word boundary", function* () { 436 assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 3, true), "abc…"); 437 }); 438 439 it("should trim whitespace after word boundary", function* () { 440 assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 4, true), "abc…"); 441 }); 442 443 it("should trim characters after word boundary", function () { 444 assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 5, true), "abc…"); 445 }); 446 447 it("should truncate in the middle of a word", function () { 448 assert.equal(Zotero.Utilities.ellipsize("abcdefghi", 6, true), "abcdef…"); 449 }); 450 451 it("should truncate at word boundary with previous space within radius", function () { 452 assert.equal(Zotero.Utilities.ellipsize("abc def ghi", 7, true), "abc def…"); 453 }); 454 455 it("should return string as is if shorter than length", function () { 456 assert.equal(Zotero.Utilities.ellipsize("abcdefg", 8, true), "abcdefg"); 457 }); 458 459 it("should return string as is if equal to length", function () { 460 assert.equal(Zotero.Utilities.ellipsize("abcdefgh", 8, true), "abcdefgh"); 461 }); 462 }); 463 }); 464 });