doi-utils
Version:
Set of utility functions to help with handling DOI(Digital Object Identifier)
61 lines (60 loc) • 1.84 kB
JavaScript
import { validatePart } from './validatePart.js';
const doiOrg = {
test(url) {
return !!url.hostname.match(/(?:dx\.)?(?:www\.)?doi\.org/);
},
parse(url) {
return url.pathname.replace(/^\//, '');
},
};
const elife = {
test(url) {
return (url.hostname.endsWith('elifesciences.org') &&
/^\/(?:articles|reviewed-preprints)\/\d+$/.test(url.pathname));
},
parse(url) {
return `10.7554/eLife.${url.pathname.replace(/^\/(?:articles|reviewed-preprints)\//, '')}`;
},
};
const zenodo = {
test(url) {
return (url.hostname.endsWith('zenodo.org') &&
!!url.pathname.match(/^\/(?:record|badge\/latestdoi)\//));
},
parse(url) {
return `10.5281/zenodo.${url.pathname.replace(/^\/(?:record|badge\/latestdoi)\//, '')}`;
},
};
const biorxiv = {
test(url) {
return url.hostname.endsWith('biorxiv.org') && !!clumpParts(url).find(validatePart);
},
parse(url) {
var _a;
return (_a = clumpParts(url)
.find(validatePart)) === null || _a === void 0 ? void 0 : _a.replace(/v([\d]*)$/, '');
},
};
function clumpParts(url) {
const parts = url.pathname.split('/').filter((p) => !!p);
return parts.slice(0, -1).map((a, i) => `${a}/${parts[i + 1]}`);
}
const pathParts = {
test(url) {
return !!clumpParts(url).find(validatePart);
},
parse(url) {
return clumpParts(url).find(validatePart);
},
};
const idInQuery = {
test(url) {
return validatePart(url.searchParams.get('id'));
},
parse(url) {
var _a;
return (_a = url.searchParams.get('id')) !== null && _a !== void 0 ? _a : undefined;
},
};
export const STRICT_RESOLVERS = [doiOrg];
export const DEFAULT_RESOLVERS = [doiOrg, biorxiv, pathParts, elife, zenodo, idInQuery];