Skip to content

Commit 7d18606

Browse files
author
Morgan Brown
committed
isOrcidId allows sandbox-prefixed orcids
1 parent fa582b4 commit 7d18606

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/utils/assert.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,40 @@ export function assertIsLinkedIdentityId(
313313
}
314314
}
315315

316-
// ORCID
316+
/**
317+
* ORCID iDs in the sandbox environment should start with this prefix.
318+
*/
319+
export const ORCID_SANDBOX_PREFIX = 'sandbox-';
320+
321+
/**
322+
* Regex to match the sandbox prefix at the start of an ORCID iD.
323+
*/
324+
const ORCID_SANDBOX_PREFIX_REGEX = new RegExp(`^${ORCID_SANDBOX_PREFIX}`);
325+
326+
/**
327+
* Removes the sandbox prefix from an ORCID iD, if present.
328+
*
329+
* @param orcidId An ORCID iD, possibly with the sandbox prefix.
330+
* @returns The ORCID iD without the sandbox prefix.
331+
*/
332+
export function unprefixOrcidId(orcidId: string): string {
333+
return orcidId.replace(ORCID_SANDBOX_PREFIX_REGEX, '');
334+
}
335+
336+
/**
337+
* Determine if a given string is a valid ORCID iD. ORCID iDs can be
338+
* prefixed with "sandbox-".
339+
*
340+
* @param orcidId An ORCID iD
341+
* @returns true if the ORCID iD is valid, false otherwise.
342+
*/
317343
export function isOrcidId(orcidId: string): boolean {
318344
if (typeof orcidId !== 'string') {
319345
return false;
320346
}
321347

322-
const baseStr: string = orcidId.replace(/[-\s]/g, '');
348+
const unprefixedOrcidId = unprefixOrcidId(orcidId);
349+
const baseStr: string = unprefixedOrcidId.replace(/[-\s]/g, '');
323350

324351
const orcidPattern: RegExp = /^\d{15}[\dX]$/;
325352
if (!orcidPattern.test(baseStr.toUpperCase())) {

tests/utils/assert.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
import { describe, test, expect } from 'vitest';
22
import {
33
isOrcidId,
4+
unprefixOrcidId,
5+
ORCID_SANDBOX_PREFIX,
46
isLinkedIdentityId,
57
assertIsLinkedIdentityId,
68
} from '../../src/utils/assert';
79

10+
describe('unprefixOrcidId', () => {
11+
test('should remove sandbox prefix from ORCID ID', () => {
12+
expect(unprefixOrcidId(`${ORCID_SANDBOX_PREFIX}0009-0007-1106-8413`)).toBe(
13+
'0009-0007-1106-8413',
14+
);
15+
});
16+
17+
test('should return ORCID ID unchanged if no sandbox prefix', () => {
18+
expect(unprefixOrcidId('0009-0007-1106-8413')).toBe('0009-0007-1106-8413');
19+
});
20+
});
21+
822
describe('isOrcidId', () => {
923
test('should return true for valid ORCID IDs', () => {
1024
// Valid ORCID ID patterns with correct checksums
1125
expect(isOrcidId('0000-0003-1527-0030')).toBe(true);
1226
});
1327

28+
test('should return true for valid sandbox-prefixed ORCID IDs', () => {
29+
// Valid sandbox ORCID ID with correct checksum after removing prefix
30+
expect(isOrcidId(`${ORCID_SANDBOX_PREFIX}0009-0007-1106-8413`)).toBe(true);
31+
});
32+
1433
test('should return false for invalid ORCID IDs', () => {
1534
// Invalid patterns
1635
expect(isOrcidId('0000-0002-1825-009X')).toBe(false); // Invalid checksum
@@ -33,6 +52,11 @@ describe('isOrcidId', () => {
3352
expect(isOrcidId('abc-def-ghi-jkl')).toBe(false); // Letters instead of digits
3453
expect(isOrcidId('0000-0000-0000-000@')).toBe(false); // Invalid special character
3554
});
55+
56+
test('should return false for sandbox-prefixed invalid ORCID IDs', () => {
57+
// Invalid checksum even with sandbox prefix
58+
expect(isOrcidId('sandbox-0000-0002-1825-009X')).toBe(false);
59+
});
3660
});
3761

3862
describe('isLinkedIdentityId', () => {

0 commit comments

Comments
 (0)