Skip to content

Commit

Permalink
feat: add support for ORCID id of creator in Zenodo submission (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjappelbaum authored Sep 23, 2020
1 parent 62d6f12 commit eea3faa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/roc-zenodo/getZenodoDeposition.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const okOrcid = require('../util/orcid');

function getZenodoDeposition(entry, self) {
const result = {
metadata: {
Expand Down Expand Up @@ -67,10 +69,20 @@ function validateCreators(creators) {
if (typeof creator.affiliation !== 'string') {
throw new TypeError('creator must have an affiliation');
}
toReturn.push({

const creatorToReturn = {
name: creatorName,
affiliation: creator.affiliation,
});
};

if ('orcid' in creator) {
if (okOrcid(creator.orcid)) {
creatorToReturn.orcid = creator.orcid;
} else {
throw new TypeError(`${creator.orcid} is not a valid ORCID identifier`);
}
}
toReturn.push(creatorToReturn);
}
return toReturn;
}
Expand Down
33 changes: 33 additions & 0 deletions src/util/orcid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

function generateCheckDigit(noHyphenOrcid) {
// https://support.orcid.org/hc/en-us/articles/360006897674-Structure-of-the-ORCID-Identifier
let total = 0;
let zero = '0'.charCodeAt(0);
for (let i = 0; i < 15; i++) {
let digit = noHyphenOrcid.charCodeAt(i) - zero;
total = (total + digit) * 2;
}
let result = (12 - (total % 11)) % 11;
return result === 10 ? 'X' : String(result);
}

function okOrcid(orcid) {
// based on https://github.com/zimeon/orcid-feed-js
if (typeof orcid !== 'string') {
return false;
}
let noHyphenOrcid = orcid.replace(
/^(\d{4})-(\d{4})-(\d{4})-(\d\d\d[\dX])$/,
'$1$2$3$4',
);
if (noHyphenOrcid === orcid) {
return false;
}
if (noHyphenOrcid.charAt(15) !== generateCheckDigit(noHyphenOrcid)) {
return false;
}
return true;
}

module.exports = okOrcid;
10 changes: 10 additions & 0 deletions test/unit/orcid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const okORCID = require('../../src/util/orcid');

it('test ORCID checksum calculation', () => {
expect(okORCID('0000-0003-4894-4660')).toBe(true);
expect(okORCID(undefined)).toBe(false);
expect(okORCID(39070)).toBe(false);
expect(okORCID('0000-0003-4894-4661')).toBe(false);
});

0 comments on commit eea3faa

Please sign in to comment.