Fix a bug in regex extracting fields to "extra"

Because regex is built using a template string, \s* is actually escaped
into s*, i.e. literal "s" appearing 0 or more times. In most cases this
would mean that output can have spacing slightly off. In extreme case,
when identifier starts with letter "s", this could this could lead to
identifier being stored incorrectly.

Also adjusted tests to be more strict and mock data to cover this case.
This commit is contained in:
Tom Najdek 2022-07-07 12:19:37 +02:00
parent 42aebccbf4
commit d418d79e70
No known key found for this signature in database
GPG key ID: EEC61A7B4C667D77
3 changed files with 3 additions and 4 deletions

View file

@ -1241,7 +1241,7 @@ Zotero_Import_Mendeley.prototype._makeCreator = function (creatorType, firstName
Zotero_Import_Mendeley.prototype._addExtraField = function (extra, field, val) {
// Strip the field if it appears at the beginning of the value (to avoid "DOI: DOI: 10...")
if (typeof val == 'string') {
val = val.replace(new RegExp(`^${field}:\s*`, 'i'), "");
val = val.replace(new RegExp(`^${field}:\\s*`, 'i'), "");
}
extra = extra ? extra + '\n' : '';
if (field != 'arXiv') {

View file

@ -14,7 +14,7 @@
"identifiers":
{
"doi": "10.1111",
"pmid": "11111111",
"pmid": "PMID: 11111111",
"arxiv": "1111.2222"
},
"starred": false,

View file

@ -174,8 +174,7 @@ describe('Zotero_Import_Mendeley', function () {
// identifiers
assert.equal(journal.getField('DOI'), '10.1111');
assert.include(journal.getField('extra'), 'PMID: 11111111');
assert.include(journal.getField('extra'), 'arXiv: 1111.2222');
assert.sameMembers(journal.getField('extra').split('\n'), ['PMID: 11111111', 'arXiv: 1111.2222']);
// attachment & annotations
assert.lengthOf(withpdf.getAttachments(), 1);