Comparing version 1.0.6 to 1.0.7
@@ -5,19 +5,34 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "validatePart", { | ||
Object.defineProperty(exports, "DEFAULT_RESOLVERS", { | ||
enumerable: true, | ||
get: function() { | ||
return _validatePart.validatePart; | ||
return _resolvers.DEFAULT_RESOLVERS; | ||
} | ||
}); | ||
exports.validate = validate; | ||
exports.normalize = normalize; | ||
exports.buildUrl = buildUrl; | ||
Object.defineProperty(exports, "STRICT_RESOLVERS", { | ||
enumerable: true, | ||
get: function() { | ||
return _resolvers.STRICT_RESOLVERS; | ||
} | ||
}); | ||
exports.default = void 0; | ||
var _resolvers = require("./resolvers"); | ||
var _validatePart = require("./validatePart"); | ||
function validate(possibleDOI) { | ||
/** | ||
* Validate that the input string is valid. | ||
* | ||
* Uses DOI pattern described here: https://www.crossref.org/blog/dois-and-matching-regular-expressions/ | ||
* | ||
* @param possibleDOI | ||
* @returns true if DOI is valid | ||
*/ function validate(possibleDOI, opts) { | ||
if (!possibleDOI) return false; | ||
return !!normalize(possibleDOI); | ||
return !!normalize(possibleDOI, opts); | ||
} | ||
function normalize(possibleDOI) { | ||
/** | ||
* Normalize an input string to the component of the DOI | ||
* | ||
* @param possibleDOI | ||
* @returns a string if it is valid | ||
*/ function normalize(possibleDOI, opts) { | ||
let doi = undefined; | ||
@@ -32,3 +47,4 @@ if (!possibleDOI) return undefined; | ||
const url = new URL(possibleDOI.startsWith('http') ? possibleDOI : `http://${possibleDOI}`); | ||
const resolver = _resolvers.DEFAULT_RESOLVERS.find((r)=>r.test(url) | ||
const resolvers = (opts === null || opts === void 0 ? void 0 : opts.strict) ? _resolvers.STRICT_RESOLVERS : _resolvers.DEFAULT_RESOLVERS; | ||
const resolver = resolvers.find((r)=>r.test(url) | ||
); | ||
@@ -43,4 +59,9 @@ if (!resolver) return undefined; | ||
} | ||
function buildUrl(possibleDOI) { | ||
const doi = normalize(possibleDOI); | ||
/** | ||
* Builds a canonical URL pointing to https://doi.org | ||
* | ||
* @param possibleDOI | ||
* @returns the doi as a string | ||
*/ function buildUrl(possibleDOI, opts) { | ||
const doi = normalize(possibleDOI, opts); | ||
if (!doi) return undefined; | ||
@@ -47,0 +68,0 @@ return `https://doi.org/${doi}`; |
"use strict"; | ||
var _index = require("./index"); | ||
var _index = _interopRequireDefault(require("./index")); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
describe('validate', ()=>{ | ||
test('should return true if doi is correct', ()=>{ | ||
expect((0, _index).validate('10.1234/56789')).toBe(true); | ||
expect(_index.default.validate('10.1234/56789')).toBe(true); | ||
}); | ||
test('empty is not a validPart', ()=>{ | ||
expect((0, _index).validatePart()).toBe(false); | ||
expect((0, _index).validatePart('')).toBe(false); | ||
expect(_index.default.validatePart()).toBe(false); | ||
expect(_index.default.validatePart('')).toBe(false); | ||
}); | ||
test('empty is not valid', ()=>{ | ||
expect((0, _index).validate()).toBe(false); | ||
expect((0, _index).validate('')).toBe(false); | ||
expect(_index.default.validate()).toBe(false); | ||
expect(_index.default.validate('')).toBe(false); | ||
}); | ||
@@ -21,3 +26,3 @@ test.each([ | ||
])('Validate works for %p', (valid)=>{ | ||
expect((0, _index).validate(valid)).toBe(true); | ||
expect(_index.default.validate(valid)).toBe(true); | ||
}); | ||
@@ -30,3 +35,3 @@ // can expand on here if needed | ||
])('should not validatePart when it is %p', (invalid)=>{ | ||
expect((0, _index).validatePart(invalid)).toBe(false); | ||
expect(_index.default.validatePart(invalid)).toBe(false); | ||
}); | ||
@@ -36,3 +41,3 @@ }); | ||
test('should build doi.org url with doi', ()=>{ | ||
expect((0, _index).buildUrl('10.1234/56789')).toBe('https://doi.org/10.1234/56789'); | ||
expect(_index.default.buildUrl('10.1234/56789')).toBe('https://doi.org/10.1234/56789'); | ||
}); | ||
@@ -61,4 +66,4 @@ test.each([ | ||
], | ||
])('should normalize url when it is %p', (doi)=>{ | ||
expect((0, _index).buildUrl(doi)).toBe('https://doi.org/10.1016/j.cageo.2015.09.015'); | ||
])('should normalize url when it is %p', (doiString)=>{ | ||
expect(_index.default.buildUrl(doiString)).toBe('https://doi.org/10.1016/j.cageo.2015.09.015'); | ||
}); | ||
@@ -68,33 +73,50 @@ }); | ||
test('should remove "doi:"', ()=>{ | ||
expect((0, _index).normalize('doi:10.1234/56789')).toBe('10.1234/56789'); | ||
expect(_index.default.normalize('doi:10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
test('should extract doi from doi.org url', ()=>{ | ||
expect((0, _index).normalize('doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
expect(_index.default.normalize('doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
test('should extract doi from doi.org url with http protocol', ()=>{ | ||
expect((0, _index).normalize('https://doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
expect(_index.default.normalize('https://doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
test('should handle www.', ()=>{ | ||
expect((0, _index).normalize('http://www.doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
expect(_index.default.normalize('http://www.doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
}); | ||
describe('external DOI links', ()=>{ | ||
test('Pull out domains that look like DOIs (subdomain)', ()=>{ | ||
expect((0, _index).normalize('https://doi.pangaea.de/10.1594/PANGAEA.941238')).toBe('10.1594/PANGAEA.941238'); | ||
test.each([ | ||
[ | ||
'elife', | ||
'https://elifesciences.org/articles/59045', | ||
'10.7554/eLife.59045' | ||
], | ||
[ | ||
'zenodo', | ||
'https://zenodo.org/badge/latestdoi/169800572', | ||
'10.5281/zenodo.169800572' | ||
], | ||
[ | ||
'joss', | ||
'https://joss.theoj.org/papers/10.21105/joss.04767', | ||
'10.21105/joss.04767' | ||
], | ||
[ | ||
'pathnames', | ||
'https://pangaea.de/doi/10.1594/PANGAEA.941238', | ||
'10.1594/PANGAEA.941238' | ||
], | ||
[ | ||
'subdomains', | ||
'https://doi.pangaea.de/10.1594/PANGAEA.941238', | ||
'10.1594/PANGAEA.941238' | ||
], | ||
])('Test %s (%s) <%s>', (_, url, doiString)=>{ | ||
expect(_index.default.normalize(url)).toBe(doiString); | ||
expect(_index.default.validate(url)).toBe(true); | ||
expect(_index.default.validate(url, { | ||
strict: true | ||
})).toBe(false); | ||
}); | ||
test('Pull out domains that look like DOIs (url)', ()=>{ | ||
expect((0, _index).normalize('https://pangaea.de/doi/10.1594/PANGAEA.941238')).toBe('10.1594/PANGAEA.941238'); | ||
}); | ||
test('eLife', ()=>{ | ||
expect((0, _index).normalize('https://elifesciences.org/articles/59045')).toBe('10.7554/eLife.59045'); | ||
}); | ||
test('zenodo', ()=>{ | ||
expect((0, _index).normalize('https://zenodo.org/badge/latestdoi/169800572')).toBe('10.5281/zenodo.169800572'); | ||
expect((0, _index).normalize('https://zenodo.org/record/169800572')).toBe('10.5281/zenodo.169800572'); | ||
}); | ||
test('joss', ()=>{ | ||
expect((0, _index).normalize('https://joss.theoj.org/papers/10.21105/joss.04767')).toBe('10.21105/joss.04767'); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.spec.js.map |
@@ -5,3 +5,3 @@ "use strict"; | ||
}); | ||
exports.DEFAULT_RESOLVERS = void 0; | ||
exports.DEFAULT_RESOLVERS = exports.STRICT_RESOLVERS = void 0; | ||
var _validatePart = require("./validatePart"); | ||
@@ -54,2 +54,6 @@ const doiOrg = { | ||
}; | ||
const STRICT_RESOLVERS = [ | ||
doiOrg | ||
]; | ||
exports.STRICT_RESOLVERS = STRICT_RESOLVERS; | ||
const DEFAULT_RESOLVERS = [ | ||
@@ -56,0 +60,0 @@ doiOrg, |
@@ -1,4 +0,4 @@ | ||
import { DEFAULT_RESOLVERS } from './resolvers'; | ||
import { DEFAULT_RESOLVERS, STRICT_RESOLVERS } from './resolvers'; | ||
import { validatePart } from './validatePart'; | ||
export { validatePart } from './validatePart'; | ||
export { DEFAULT_RESOLVERS, STRICT_RESOLVERS } from './resolvers'; | ||
/** | ||
@@ -11,5 +11,5 @@ * Validate that the input string is valid. | ||
* @returns true if DOI is valid | ||
*/ export function validate(possibleDOI) { | ||
*/ function validate(possibleDOI, opts) { | ||
if (!possibleDOI) return false; | ||
return !!normalize(possibleDOI); | ||
return !!normalize(possibleDOI, opts); | ||
} | ||
@@ -21,3 +21,3 @@ /** | ||
* @returns a string if it is valid | ||
*/ export function normalize(possibleDOI) { | ||
*/ function normalize(possibleDOI, opts) { | ||
let doi = undefined; | ||
@@ -32,3 +32,4 @@ if (!possibleDOI) return undefined; | ||
const url = new URL(possibleDOI.startsWith('http') ? possibleDOI : `http://${possibleDOI}`); | ||
const resolver = DEFAULT_RESOLVERS.find((r)=>r.test(url) | ||
const resolvers = (opts === null || opts === void 0 ? void 0 : opts.strict) ? STRICT_RESOLVERS : DEFAULT_RESOLVERS; | ||
const resolver = resolvers.find((r)=>r.test(url) | ||
); | ||
@@ -48,4 +49,4 @@ if (!resolver) return undefined; | ||
* @returns the doi as a string | ||
*/ export function buildUrl(possibleDOI) { | ||
const doi = normalize(possibleDOI); | ||
*/ function buildUrl(possibleDOI, opts) { | ||
const doi = normalize(possibleDOI, opts); | ||
if (!doi) return undefined; | ||
@@ -52,0 +53,0 @@ return `https://doi.org/${doi}`; |
@@ -1,13 +0,13 @@ | ||
import { validate, buildUrl, normalize, validatePart } from './index'; | ||
import doi from './index'; | ||
describe('validate', ()=>{ | ||
test('should return true if doi is correct', ()=>{ | ||
expect(validate('10.1234/56789')).toBe(true); | ||
expect(doi.validate('10.1234/56789')).toBe(true); | ||
}); | ||
test('empty is not a validPart', ()=>{ | ||
expect(validatePart()).toBe(false); | ||
expect(validatePart('')).toBe(false); | ||
expect(doi.validatePart()).toBe(false); | ||
expect(doi.validatePart('')).toBe(false); | ||
}); | ||
test('empty is not valid', ()=>{ | ||
expect(validate()).toBe(false); | ||
expect(validate('')).toBe(false); | ||
expect(doi.validate()).toBe(false); | ||
expect(doi.validate('')).toBe(false); | ||
}); | ||
@@ -20,3 +20,3 @@ test.each([ | ||
])('Validate works for %p', (valid)=>{ | ||
expect(validate(valid)).toBe(true); | ||
expect(doi.validate(valid)).toBe(true); | ||
}); | ||
@@ -29,3 +29,3 @@ // can expand on here if needed | ||
])('should not validatePart when it is %p', (invalid)=>{ | ||
expect(validatePart(invalid)).toBe(false); | ||
expect(doi.validatePart(invalid)).toBe(false); | ||
}); | ||
@@ -35,3 +35,3 @@ }); | ||
test('should build doi.org url with doi', ()=>{ | ||
expect(buildUrl('10.1234/56789')).toBe('https://doi.org/10.1234/56789'); | ||
expect(doi.buildUrl('10.1234/56789')).toBe('https://doi.org/10.1234/56789'); | ||
}); | ||
@@ -60,4 +60,4 @@ test.each([ | ||
], | ||
])('should normalize url when it is %p', (doi)=>{ | ||
expect(buildUrl(doi)).toBe('https://doi.org/10.1016/j.cageo.2015.09.015'); | ||
])('should normalize url when it is %p', (doiString)=>{ | ||
expect(doi.buildUrl(doiString)).toBe('https://doi.org/10.1016/j.cageo.2015.09.015'); | ||
}); | ||
@@ -67,33 +67,50 @@ }); | ||
test('should remove "doi:"', ()=>{ | ||
expect(normalize('doi:10.1234/56789')).toBe('10.1234/56789'); | ||
expect(doi.normalize('doi:10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
test('should extract doi from doi.org url', ()=>{ | ||
expect(normalize('doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
expect(doi.normalize('doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
test('should extract doi from doi.org url with http protocol', ()=>{ | ||
expect(normalize('https://doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
expect(doi.normalize('https://doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
test('should handle www.', ()=>{ | ||
expect(normalize('http://www.doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
expect(doi.normalize('http://www.doi.org/10.1234/56789')).toBe('10.1234/56789'); | ||
}); | ||
}); | ||
describe('external DOI links', ()=>{ | ||
test('Pull out domains that look like DOIs (subdomain)', ()=>{ | ||
expect(normalize('https://doi.pangaea.de/10.1594/PANGAEA.941238')).toBe('10.1594/PANGAEA.941238'); | ||
test.each([ | ||
[ | ||
'elife', | ||
'https://elifesciences.org/articles/59045', | ||
'10.7554/eLife.59045' | ||
], | ||
[ | ||
'zenodo', | ||
'https://zenodo.org/badge/latestdoi/169800572', | ||
'10.5281/zenodo.169800572' | ||
], | ||
[ | ||
'joss', | ||
'https://joss.theoj.org/papers/10.21105/joss.04767', | ||
'10.21105/joss.04767' | ||
], | ||
[ | ||
'pathnames', | ||
'https://pangaea.de/doi/10.1594/PANGAEA.941238', | ||
'10.1594/PANGAEA.941238' | ||
], | ||
[ | ||
'subdomains', | ||
'https://doi.pangaea.de/10.1594/PANGAEA.941238', | ||
'10.1594/PANGAEA.941238' | ||
], | ||
])('Test %s (%s) <%s>', (_, url, doiString)=>{ | ||
expect(doi.normalize(url)).toBe(doiString); | ||
expect(doi.validate(url)).toBe(true); | ||
expect(doi.validate(url, { | ||
strict: true | ||
})).toBe(false); | ||
}); | ||
test('Pull out domains that look like DOIs (url)', ()=>{ | ||
expect(normalize('https://pangaea.de/doi/10.1594/PANGAEA.941238')).toBe('10.1594/PANGAEA.941238'); | ||
}); | ||
test('eLife', ()=>{ | ||
expect(normalize('https://elifesciences.org/articles/59045')).toBe('10.7554/eLife.59045'); | ||
}); | ||
test('zenodo', ()=>{ | ||
expect(normalize('https://zenodo.org/badge/latestdoi/169800572')).toBe('10.5281/zenodo.169800572'); | ||
expect(normalize('https://zenodo.org/record/169800572')).toBe('10.5281/zenodo.169800572'); | ||
}); | ||
test('joss', ()=>{ | ||
expect(normalize('https://joss.theoj.org/papers/10.21105/joss.04767')).toBe('10.21105/joss.04767'); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.spec.js.map |
@@ -48,2 +48,5 @@ import { validatePart } from './validatePart'; | ||
}; | ||
export const STRICT_RESOLVERS = [ | ||
doiOrg | ||
]; | ||
export const DEFAULT_RESOLVERS = [ | ||
@@ -50,0 +53,0 @@ doiOrg, |
import { validatePart } from './validatePart'; | ||
export { validatePart } from './validatePart'; | ||
export { DEFAULT_RESOLVERS, STRICT_RESOLVERS } from './resolvers'; | ||
export declare type Options = { | ||
strict?: boolean; | ||
}; | ||
/** | ||
@@ -11,3 +14,3 @@ * Validate that the input string is valid. | ||
*/ | ||
export declare function validate(possibleDOI?: string): boolean; | ||
declare function validate(possibleDOI?: string, opts?: Options): boolean; | ||
/** | ||
@@ -19,3 +22,3 @@ * Normalize an input string to the component of the DOI | ||
*/ | ||
export declare function normalize(possibleDOI: string): string | undefined; | ||
declare function normalize(possibleDOI: string, opts?: Options): string | undefined; | ||
/** | ||
@@ -27,3 +30,3 @@ * Builds a canonical URL pointing to https://doi.org | ||
*/ | ||
export declare function buildUrl(possibleDOI: string): string | undefined; | ||
declare function buildUrl(possibleDOI: string, opts?: Options): string | undefined; | ||
declare const _default: { | ||
@@ -30,0 +33,0 @@ validatePart: typeof validatePart; |
@@ -6,3 +6,4 @@ /// <reference types="node" /> | ||
} | ||
export declare const STRICT_RESOLVERS: Resolver[]; | ||
export declare const DEFAULT_RESOLVERS: Resolver[]; | ||
export {}; |
{ | ||
"name": "doi-utils", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Set of utility functions to help with handling DOI(Digital Object Identifier)", | ||
@@ -5,0 +5,0 @@ "author": "Curvenote Inc. <support@curvenote.com>", |
@@ -12,2 +12,4 @@ # `doi-utils` | ||
The utility also recognizes URLs that are likely DOIs, and has specific handling for some repositories (e.g. eLife, Zenodo). | ||
## Install | ||
@@ -22,10 +24,10 @@ | ||
```ts | ||
import { validate, buildUrl } from 'doi-utils'; | ||
import doi from 'doi-utils'; | ||
const isValid = validate('http://doi.org/10.1016/j.cageo.2015.09.015'); | ||
const isValid = doi.validate('http://doi.org/10.1016/j.cageo.2015.09.015'); | ||
buildUrl('http://dx.doi.org/10.1016/j.cageo.2015.09.015'); | ||
buildUrl('http://www.doi.org/10.1016/j.cageo.2015.09.015'); | ||
buildUrl('http://doi.org/10.1016/j.cageo.2015.09.015'); | ||
buildUrl('doi:10.1016/j.cageo.2015.09.015'); | ||
doi.buildUrl('http://dx.doi.org/10.1016/j.cageo.2015.09.015'); | ||
doi.buildUrl('http://www.doi.org/10.1016/j.cageo.2015.09.015'); | ||
doi.buildUrl('http://doi.org/10.1016/j.cageo.2015.09.015'); | ||
doi.buildUrl('doi:10.1016/j.cageo.2015.09.015'); | ||
// All of these produce a normalized, secure DOI url: | ||
@@ -38,5 +40,10 @@ // https://doi.org/10.1016/j.cageo.2015.09.015 | ||
- `validate` - Validates if a single DOI string is valid, is tolerant of leading link or `doi:` strings. | ||
- `validatePart` - Validate the "10.1016/j.cageo.2015.09.015" part of a DOI. | ||
- `normalize` - Normalizes a DOI url or string into a DOI of the form `10.1000/xyz000` | ||
- `buildUrl` - Builds a URL to https://doi.org, includes normalization | ||
## Options | ||
- `strict`: only accecpt doi.org URLs and `doi:` prefixes | ||
--- | ||
@@ -43,0 +50,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50467
550
54