Skip to content

Commit d24eebd

Browse files
committed
New: Initial implementation
1 parent f6576f9 commit d24eebd

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
function parseNodeVersion(version) {
4+
var match = version.match(/^v(\d{1,2})\.(\d{1,2})\.(\d{1,2})$/);
5+
if (!match) {
6+
throw new Error('Unable to parse: ' + version);
7+
}
8+
9+
return {
10+
major: parseInt(match[1], 10),
11+
minor: parseInt(match[2], 10),
12+
patch: parseInt(match[3], 10),
13+
};
14+
}
15+
16+
module.exports = parseNodeVersion;

test/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
var expect = require('expect');
4+
5+
var parseNodeVersion = require('../');
6+
7+
describe('parseNodeVersion', function() {
8+
9+
it('takes process.version and returns all numbers', function(done) {
10+
var nodeVersion = parseNodeVersion(process.version);
11+
expect(nodeVersion.major).toBeA('number');
12+
expect(nodeVersion.minor).toBeA('number');
13+
expect(nodeVersion.patch).toBeA('number');
14+
done();
15+
});
16+
17+
it('takes any string formatted like a v8.8.8 and returns all numbers', function(done) {
18+
var nodeVersion = parseNodeVersion('v8.8.8');
19+
expect(nodeVersion.major).toEqual(8);
20+
expect(nodeVersion.minor).toEqual(8);
21+
expect(nodeVersion.patch).toEqual(8);
22+
done();
23+
});
24+
25+
it('throws if given an invalid version', function(done) {
26+
function invalid() {
27+
parseNodeVersion('8.8.8');
28+
}
29+
30+
expect(invalid).toThrow('Unable to parse: 8.8.8');
31+
done();
32+
});
33+
34+
it('matches on exactly the version - it cannot be padded', function(done) {
35+
function invalid() {
36+
parseNodeVersion('vv8.8.8');
37+
}
38+
39+
expect(invalid).toThrow('Unable to parse: vv8.8.8');
40+
done();
41+
});
42+
43+
it('only matches 2 digits in any position', function(done) {
44+
function invalid() {
45+
parseNodeVersion('v8.111.8');
46+
}
47+
48+
expect(invalid).toThrow('Unable to parse: v8.111.8');
49+
done();
50+
});
51+
});

0 commit comments

Comments
 (0)