Skip to content

Commit d78b739

Browse files
committed
Added groups, match, tests
1 parent b12d2ce commit d78b739

File tree

4 files changed

+76
-22
lines changed

4 files changed

+76
-22
lines changed

cli-index.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ if (process.argv.length <= 2) {
1717
var flags = {
1818
"exact": ['-e', '--exact'],
1919
"declared": ['-d', '--declared'],
20-
"match": ['-m', '--match']
20+
"match": ['-m', '--match'],
21+
"groups": ['-g', '--groups']
2122
};
2223

2324
for (var index in flags) {
@@ -27,22 +28,27 @@ for (var index in flags) {
2728
flags[index] = true
2829
}
2930
})
31+
32+
if (flags[index] !== true) {
33+
flags[index] = null
34+
}
3035
}
3136

3237
var doi = process.argv[2];
3338

3439
if (flags.match === true) {
3540
console.log(doi.match(doiRegex()))
41+
} else if (flags.groups === true) {
42+
console.log(doiRegex.groups(doi));
3643
} else {
37-
if (flags.exact !== true && flags.declared !== true) {
38-
console.log('Does a DOI exist?', doiRegex().test(doi))
39-
} else if (flags.exact === true && flags.declared !== true) {
44+
if (flags.exact && flags.declared) {
45+
console.log('Is this a declared DOI',
46+
doiRegex.declared({exact: true}).test(doi))
47+
} else if (flags.exact && !flags.declared) {
4048
console.log('Is this a DOI?', doiRegex({exact: true}).test(doi))
41-
} else if (flags.exact !== true && flags.declared === true) {
49+
} else if (!flags.exact && flags.declared) {
4250
console.log('Is the DOI declared?', doiRegex.declared().test(doi))
43-
} else if (flags.match !== true) {
44-
console.log('Is this a declared DOI',
45-
doiRegex.declared({expect: true}).test(doi))
51+
} else {
52+
console.log('Does a DOI exist?', doiRegex().test(doi))
4653
}
4754
}
48-

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ var doi = module.exports = function (opts) {
1111
new RegExp('(?:' + doiRegex + ')', 'g')
1212
}
1313

14+
doi.groups = function (str) {
15+
// Javascript fails at lookaheads for optional groups. This circumvents that
16+
// problem by just automatically removing and saving suffixes if they are in
17+
// as specific format - .a000 is the format used by PLoS, but this may need
18+
// to be filled out.
19+
var suffixes = [];
20+
var newStr = str.replace(/\.[a-zA-Z]{1}[0-9]{3}$/g, function(s){
21+
suffixes.push(s)
22+
return ''
23+
})
24+
var match = doi().exec(newStr)
25+
match[0] = str;
26+
match.push((!!suffixes.length) ? suffixes[0] : '')
27+
return match
28+
}
29+
1430
doi.declared = function(opts) {
1531
opts = opts || {}
1632
return opts.exact ? new RegExp('^' + doiTextPrefix + doiRegex + '$') :

readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ doiRegex({exact: true}).test('unicorn 10.1000/xyz000');
2626
doiRegex.declared({exact: true}).test('doi:10.1000/xyz000');
2727
//=> true
2828

29+
doiRegex.groups().test('10.1000/xyz1000.a001');
30+
//=> ['10.1000/xyz1000', '10.1000/xyz1000', '.a001']
31+
2932
'unicorn 10.1000/xyz000 cake 10.1000/xyz001 rainbow'.match(doiRegex());
3033
//=> ['10.1000/xyz000', '10.1000/xyz000']
3134
```
@@ -41,6 +44,10 @@ Returns a regex for matching a DOI.
4144

4245
Returns a regex for matching a DOI that has been declared with a `doi:` string in front.
4346

47+
### doiRegex.groups(doi)
48+
49+
Returns a regex match object with a final string as the first two indices, and any suffixes that are commonly used for supplemental information if they are attached to the doi. For instance, `10.1000/journal.pone.0000000.g001` would return `10.1000/journal.pone.0000000` and `.g001`.
50+
4451
#### options.exact
4552

4653
Type: `boolean`
@@ -59,11 +66,14 @@ $ node cli-index.js -e `10.000/xyz1000`
5966
true
6067
```
6168

69+
Run it without arguments to see all possible flags.
70+
6271
Possible Flags:
6372

64-
* `-e`, `--exact` Find an exact match \n\
65-
* `-d`, `--declared` Find a DOI with a 'doi:' prefix\n\
66-
* `-m`, `--match` Find all matches within the given string");
73+
* `-e`, `--exact` Find an exact match
74+
* `-d`, `--declared` Find a DOI with a 'doi:' prefix
75+
* `-m`, `--match` Find all matches within the given string
76+
* `-g`, `--groups` Find the stripped DOI and any suffix it might have
6777

6878
## License
6979

test.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,70 @@ var doiNotDeclared = [
2525
'10.1000/journal.pone.0011111',
2626
];
2727

28+
var doiGroups = [
29+
'10.1000/journal.pone.0011111.a001',
30+
'doi:10.1000/journal.pone.0011111.a001'
31+
]
32+
2833
test('exact DOIs as passing', function (t) {
2934
_(doi).each(function (el) {
3035
t.assert(doiRegex({exact: true}).test(el), el)
3136
})
32-
t.end()
3337
})
3438

3539
test('embeded DOIs as passing', function (t) {
3640
_(doi).each(function (el) {
3741
t.assert(doiRegex().exec('foo' + el)[0] === el, el)
3842
})
39-
t.end()
4043
})
4144

4245
test('non-exact DOIs as failing', function (t) {
4346
_(doiNot).each(function (el) {
4447
t.assert(!doiRegex({exact: true}).test(el), el)
4548
})
46-
47-
t.end()
4849
})
4950

5051
test('DOI declared as passing', function (t) {
5152
_(doiDeclared).each(function (el) {
5253
t.assert(doiRegex.declared({exact: true}).test(el), el)
5354
})
54-
55-
t.end()
5655
})
5756

5857
test('DOI declared embeded as passing', function (t) {
5958
_(doiDeclared).each(function (el) {
6059
t.assert((doiRegex.declared().exec('foo' + el) || [])[0] === el, el)
6160
})
62-
63-
t.end()
6461
})
6562

66-
test('DOI not declared as failing', function(t) {
63+
test('DOI not declared as failing', function (t) {
6764
_(doiNotDeclared).each(function (el) {
6865
t.assert(!doiRegex.declared({exact: true}).test(el), el)
6966
})
67+
})
68+
69+
test('DOI group catching returns original', function (t) {
70+
_(doiGroups).each(function (el) {
71+
t.assert(doiRegex.groups(el)[0] === el, el)
72+
})
73+
_(doi).each(function (el) {
74+
t.assert(doiRegex.groups(el)[0] === el, el)
75+
})
76+
})
7077

71-
t.end()
78+
test('DOI group catching returns DOI', function (t) {
79+
_(doiGroups).each(function (el) {
80+
t.assert(doiRegex(doiRegex.groups(el)[1]), el)
81+
})
82+
_(doi).each(function (el) {
83+
t.assert(doiRegex(doiRegex.groups(el)[1]), el)
84+
})
85+
})
86+
87+
test('DOI group catching returns extension', function (t) {
88+
_(doiGroups).each(function (el) {
89+
t.assert(doiRegex.groups(el)[2].length === 5, el)
90+
})
91+
_(doi).each(function (el) {
92+
t.assert(doiRegex.groups(el)[2].length === 0, el)
93+
})
7294
})

0 commit comments

Comments
 (0)