-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoi.ts
34 lines (31 loc) · 965 Bytes
/
doi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// DOI validate and normalize routines.
//
export const DOIPattern = "^10\\.\\d{4,9}\\/[^\\s]+$";
export const reDOI = new RegExp(DOIPattern);
/**
* Normalizes a DOI by removing extraneous characters and enforcing lowercase.
* @param doi - The DOI string to normalize.
* @returns string - The normalized DOI string.
*/
export function normalizeDOI(doi: string): string {
const lowercaseDOI = doi.toLowerCase().trim();
if (URL.canParse(lowercaseDOI)) {
const u = URL.parse(lowercaseDOI);
if (
u !== undefined && u !== null && u.pathname !== null && u.pathname !== ""
) {
return u.pathname.replace(/^\//, "");
}
}
return lowercaseDOI;
}
/**
* Validates the format of a DOI.
* @param doi - The DOI string to validate.
* @returns boolean - True if the DOI is valid, otherwise false.
*/
export function validateDOI(doi: string): boolean {
const normalizedDOI = normalizeDOI(doi);
return reDOI.test(normalizedDOI);
}