Skip to content

Commit c9d6ee9

Browse files
amblpphated
authored andcommitted
Fix: Support pre-release and build metadata segments of a node version (fixes #3, closes #2, closes #4)
1 parent 2945763 commit c9d6ee9

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ Turn node's process.version into something useful.
1515
```js
1616
var nodeVersion = require('parse-node-version')(process.version);
1717

18-
console.log(nodeVersion.major, nodeVersion.minor, nodeVersion.patch);
18+
console.log(
19+
nodeVersion.major,
20+
nodeVersion.minor,
21+
nodeVersion.patch,
22+
nodeVersion.pre,
23+
nodeVersion.build
24+
);
1925
```
2026

2127
## API
2228

2329
### parseVersion(nodeVersionString)
2430

25-
Takes a node version string (usually `process.version`) and returns an object with the `major`, `minor`, and `patch` keys which will all be parsed digits.
31+
Takes a node version string (usually `process.version`) and returns an object with the `major`/`minor`/`patch` (which will all be numbers) and `pre`/`build` keys (which will always be a string). If the version doesn't contain any pre-release or build information, the properties will be returned as empty string.
2632

2733
## License
2834

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
'use strict';
22

33
function parseNodeVersion(version) {
4-
var match = version.match(/^v(\d{1,2})\.(\d{1,2})\.(\d{1,2})$/);
4+
var match = version.match(/^v(\d{1,2})\.(\d{1,2})\.(\d{1,2})(?:-([0-9A-Za-z-.]+))?(?:\+([0-9A-Za-z-.]+))?$/); // eslint-disable-line max-len
55
if (!match) {
66
throw new Error('Unable to parse: ' + version);
77
}
88

9-
return {
9+
var res = {
1010
major: parseInt(match[1], 10),
1111
minor: parseInt(match[2], 10),
1212
patch: parseInt(match[3], 10),
13+
pre: match[4] || '',
14+
build: match[5] || '',
1315
};
16+
17+
return res;
1418
}
1519

1620
module.exports = parseNodeVersion;

test/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,44 @@ describe('parseNodeVersion', function() {
4848
expect(invalid).toThrow('Unable to parse: v8.111.8');
4949
done();
5050
});
51+
52+
it('always returns strings if no pre-release or build metadata', function(done) {
53+
var nodeVersion = parseNodeVersion(process.version);
54+
expect(nodeVersion.pre).toBeA('string');
55+
expect(nodeVersion.pre).toEqual('');
56+
expect(nodeVersion.build).toBeA('string');
57+
expect(nodeVersion.build).toEqual('');
58+
done();
59+
});
60+
61+
it('matches pre-release label with single identifier', function(done) {
62+
var nodeVersion = parseNodeVersion('v1.2.3-alpha');
63+
expect(nodeVersion.pre).toEqual('alpha');
64+
done();
65+
});
66+
67+
it('matches pre-release label with multiple identifiers', function(done) {
68+
var nodeVersion = parseNodeVersion('v1.2.3-x.7.z.92');
69+
expect(nodeVersion.pre).toEqual('x.7.z.92');
70+
done();
71+
});
72+
73+
it('matches pre-release label with dashes', function(done) {
74+
var nodeVersion = parseNodeVersion('v1.2.3-x-7-z.92');
75+
expect(nodeVersion.pre).toEqual('x-7-z.92');
76+
done();
77+
});
78+
79+
it('matches build metadata', function(done) {
80+
var nodeVersion = parseNodeVersion('v10.15.0+0-b20181231T20014594');
81+
expect(nodeVersion.build).toEqual('0-b20181231T20014594');
82+
done();
83+
});
84+
85+
it('matches pre-release label and build metadata', function(done) {
86+
var nodeVersion = parseNodeVersion('v1.0.0-beta+exp.sha.5114f85');
87+
expect(nodeVersion.pre).toEqual('beta');
88+
expect(nodeVersion.build).toEqual('exp.sha.5114f85');
89+
done();
90+
});
5191
});

0 commit comments

Comments
 (0)