Skip to content

Commit d2a92e5

Browse files
committed
Init
1 parent ec22ff5 commit d2a92e5

File tree

7 files changed

+221
-5
lines changed

7 files changed

+221
-5
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[package.json]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.jshintrc

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"node": true,
3+
"esnext": true,
4+
"bitwise": true,
5+
"camelcase": true,
6+
"curly": true,
7+
"immed": true,
8+
"newcap": true,
9+
"noarg": true,
10+
"undef": true,
11+
"unused": "vars",
12+
"strict": true,
13+
"asi": true
14+
}

index.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
// TODO Capture final segment for fragments
4+
// (\\.[a-zA-Z]{1}[0-9]{3})?
5+
var doiRegex = '(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\/\'<>])\\S)+)'
6+
var doiTextPrefix = 'doi\\:'
7+
8+
var doi = module.exports = function (opts) {
9+
opts = opts || {}
10+
return opts.exact ? new RegExp('(?:^' + doiRegex + '$)') :
11+
new RegExp('(?:' + doiRegex + ')', 'g')
12+
}
13+
14+
doi.declared = function(opts) {
15+
opts = opts || {}
16+
return opts.exact ? new RegExp('^' + doiTextPrefix + doiRegex + '$') :
17+
new RegExp(doiTextPrefix + doiRegex, 'g')
18+
}

license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Richard Littauer <[email protected]> (burntfen.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
{
22
"name": "doi-regex",
33
"version": "0.0.0",
4-
"description": "Regex all the dois",
4+
"description": "Regular expression for matching DOIs",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "node test.js"
88
},
99
"repository": {
1010
"type": "git",
1111
"url": "[email protected]:BeagleLab/doi.git"
1212
},
1313
"keywords": [
1414
"doi",
15-
"beagle"
15+
"beagle",
16+
"regex",
17+
"regexp",
18+
"re",
19+
"match",
20+
"test",
21+
"find",
22+
"pattern",
23+
"doi",
24+
"digital object identifier",
25+
"validate"
1626
],
17-
"author": "Richard Littauer <[email protected]>",
27+
"author": {
28+
"name": "Richard Littauer",
29+
"email": "[email protected]",
30+
"url": "http://www.burntfen.com"
31+
},
32+
"engines": {
33+
"node": ">=0.10.0"
34+
},
1835
"license": "MIT",
1936
"bugs": {
2037
"url": "https://github.com/BeagleLab/doi/issues"
2138
},
22-
"homepage": "https://github.com/BeagleLab/doi"
39+
"homepage": "https://github.com/BeagleLab/doi",
40+
"devDependencies": {
41+
"ava": "0.0.4",
42+
"lodash": "^2.4.1"
43+
}
2344
}

readme.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# doi-regex [![Build Status](https://travis-ci.org/BeagleLab/doi-regex.svg?branch=master)](https://travis-ci.org/BeagleLab/doi-regex)
2+
3+
> Regular expression for matching DOIs
4+
5+
6+
## Install
7+
8+
```sh
9+
$ npm install --save doi-regex
10+
```
11+
12+
13+
## Usage
14+
15+
```js
16+
var doiRegex = require('doi-regex');
17+
18+
// contains a DOI
19+
doiRegex().test('unicorn 10.1000/xyz000');
20+
//=> true
21+
22+
// is a DOI address
23+
doiRegex({exact: true}).test('unicorn 10.1000/xyz000');
24+
//=> false
25+
26+
doiRegex.declared({exact: true}).test('doi:10.1000/xyz000');
27+
//=> true
28+
29+
'unicorn 10.1000/xyz000 cake 10.1000/xyz001 rainbow'.match(doiRegex());
30+
//=> ['10.1000/xyz000', '10.1000/xyz000']
31+
```
32+
33+
34+
## API
35+
36+
### doiRegex(options)
37+
38+
Returns a regex for matching a DOI.
39+
40+
### doiRegex.declared(options)
41+
42+
Returns a regex for matching a DOI that has been declared with a `doi:` string in front.
43+
44+
#### options.exact
45+
46+
Type: `boolean`
47+
Default: `false` *(Matches any DOI in a string)*
48+
49+
Only match an exact string.
50+
Useful with `RegExp#test` to check if a string is an DOI.
51+
52+
53+
## License
54+
55+
MIT © [Richard Littauer](http://burntfen.com)

test.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
var test = require('ava')
3+
var _ = require('lodash')
4+
var doiRegex = require('./');
5+
6+
var doi = [
7+
'10.1371/journal.pone.0077056'
8+
];
9+
10+
var doiNot = [
11+
'10.1000//journal.pone.0011111',
12+
'10..1000/journal.pone.0011111',
13+
'1.1/1.1'
14+
];
15+
16+
var doiDeclared = [
17+
'doi:10.1000/journal.pone.0011111',
18+
];
19+
20+
var doiNotDeclared = [
21+
'do:10.1000/journal.pone.0011111',
22+
'doi:10..1000/journal.pone.0011111',
23+
'DO:10.1000/journal.pone.0011111',
24+
':10.1000/journal.pone.0011111',
25+
'10.1000/journal.pone.0011111',
26+
];
27+
28+
test('exact DOIs as passing', function (t) {
29+
_(doi).each(function (el) {
30+
t.assert(doiRegex({exact: true}).test(el), el)
31+
})
32+
t.end()
33+
})
34+
35+
test('embeded DOIs as passing', function (t) {
36+
_(doi).each(function (el) {
37+
t.assert(doiRegex().exec('foo' + el)[0] === el, el)
38+
})
39+
t.end()
40+
})
41+
42+
test('non-exact DOIs as failing', function (t) {
43+
_(doiNot).each(function (el) {
44+
t.assert(!doiRegex({exact: true}).test(el), el)
45+
})
46+
47+
t.end()
48+
})
49+
50+
test('DOI declared as passing', function (t) {
51+
_(doiDeclared).each(function (el) {
52+
t.assert(doiRegex.declared({exact: true}).test(el), el)
53+
})
54+
55+
t.end()
56+
})
57+
58+
test('DOI declared embeded as passing', function (t) {
59+
_(doiDeclared).each(function (el) {
60+
t.assert((doiRegex.declared().exec('foo' + el) || [])[0] === el, el)
61+
})
62+
63+
t.end()
64+
})
65+
66+
test('DOI not declared as failing', function(t) {
67+
_(doiNotDeclared).each(function (el) {
68+
t.assert(!doiRegex.declared({exact: true}).test(el), el)
69+
})
70+
71+
t.end()
72+
})

0 commit comments

Comments
 (0)