File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments