@@ -4,90 +4,92 @@ exports.parse = function (source, transform) {
4
4
return new ArrayParser ( source , transform ) . parse ( )
5
5
}
6
6
7
- function ArrayParser ( source , transform ) {
8
- this . source = source
9
- this . transform = transform || identity
10
- this . position = 0
11
- this . entries = [ ]
12
- this . recorded = [ ]
13
- this . dimension = 0
14
- }
7
+ class ArrayParser {
8
+ constructor ( source , transform ) {
9
+ this . source = source
10
+ this . transform = transform || identity
11
+ this . position = 0
12
+ this . entries = [ ]
13
+ this . recorded = [ ]
14
+ this . dimension = 0
15
+ }
15
16
16
- ArrayParser . prototype . isEof = function ( ) {
17
- return this . position >= this . source . length
18
- }
17
+ isEof ( ) {
18
+ return this . position >= this . source . length
19
+ }
19
20
20
- ArrayParser . prototype . nextCharacter = function ( ) {
21
- var character = this . source [ this . position ++ ]
22
- if ( character === '\\' ) {
21
+ nextCharacter ( ) {
22
+ var character = this . source [ this . position ++ ]
23
+ if ( character === '\\' ) {
24
+ return {
25
+ value : this . source [ this . position ++ ] ,
26
+ escaped : true
27
+ }
28
+ }
23
29
return {
24
- value : this . source [ this . position ++ ] ,
25
- escaped : true
30
+ value : character ,
31
+ escaped : false
26
32
}
27
33
}
28
- return {
29
- value : character ,
30
- escaped : false
31
- }
32
- }
33
34
34
- ArrayParser . prototype . record = function ( character ) {
35
- this . recorded . push ( character )
36
- }
35
+ record ( character ) {
36
+ this . recorded . push ( character )
37
+ }
37
38
38
- ArrayParser . prototype . newEntry = function ( includeEmpty ) {
39
- var entry
40
- if ( this . recorded . length > 0 || includeEmpty ) {
41
- entry = this . recorded . join ( '' )
42
- if ( entry === 'NULL' && ! includeEmpty ) {
43
- entry = null
39
+ newEntry ( includeEmpty ) {
40
+ var entry
41
+ if ( this . recorded . length > 0 || includeEmpty ) {
42
+ entry = this . recorded . join ( '' )
43
+ if ( entry === 'NULL' && ! includeEmpty ) {
44
+ entry = null
45
+ }
46
+ if ( entry !== null ) entry = this . transform ( entry )
47
+ this . entries . push ( entry )
48
+ this . recorded = [ ]
44
49
}
45
- if ( entry !== null ) entry = this . transform ( entry )
46
- this . entries . push ( entry )
47
- this . recorded = [ ]
48
50
}
49
- }
50
51
51
- ArrayParser . prototype . consumeDimensions = function ( ) {
52
- if ( this . source [ 0 ] === '[' ) {
53
- while ( ! this . isEof ( ) ) {
54
- var char = this . nextCharacter ( )
55
- if ( char . value === '=' ) break
52
+ consumeDimensions ( ) {
53
+ if ( this . source [ 0 ] === '[' ) {
54
+ while ( ! this . isEof ( ) ) {
55
+ var char = this . nextCharacter ( )
56
+ if ( char . value === '=' ) break
57
+ }
56
58
}
57
59
}
58
- }
59
60
60
- ArrayParser . prototype . parse = function ( nested ) {
61
- var character , parser , quote
62
- this . consumeDimensions ( )
63
- while ( ! this . isEof ( ) ) {
64
- character = this . nextCharacter ( )
65
- if ( character . value === '{' && ! quote ) {
66
- this . dimension ++
67
- if ( this . dimension > 1 ) {
68
- parser = new ArrayParser ( this . source . substr ( this . position - 1 ) , this . transform )
69
- this . entries . push ( parser . parse ( true ) )
70
- this . position += parser . position - 2
71
- }
72
- } else if ( character . value === '}' && ! quote ) {
73
- this . dimension --
74
- if ( ! this . dimension ) {
61
+ parse ( nested ) {
62
+ var character , parser , quote
63
+ this . consumeDimensions ( )
64
+ while ( ! this . isEof ( ) ) {
65
+ character = this . nextCharacter ( )
66
+ if ( character . value === '{' && ! quote ) {
67
+ this . dimension ++
68
+ if ( this . dimension > 1 ) {
69
+ parser = new ArrayParser ( this . source . substr ( this . position - 1 ) , this . transform )
70
+ this . entries . push ( parser . parse ( true ) )
71
+ this . position += parser . position - 2
72
+ }
73
+ } else if ( character . value === '}' && ! quote ) {
74
+ this . dimension --
75
+ if ( ! this . dimension ) {
76
+ this . newEntry ( )
77
+ if ( nested ) return this . entries
78
+ }
79
+ } else if ( character . value === '"' && ! character . escaped ) {
80
+ if ( quote ) this . newEntry ( true )
81
+ quote = ! quote
82
+ } else if ( character . value === ',' && ! quote ) {
75
83
this . newEntry ( )
76
- if ( nested ) return this . entries
84
+ } else {
85
+ this . record ( character . value )
77
86
}
78
- } else if ( character . value === '"' && ! character . escaped ) {
79
- if ( quote ) this . newEntry ( true )
80
- quote = ! quote
81
- } else if ( character . value === ',' && ! quote ) {
82
- this . newEntry ( )
83
- } else {
84
- this . record ( character . value )
85
87
}
88
+ if ( this . dimension !== 0 ) {
89
+ throw new Error ( 'array dimension not balanced' )
90
+ }
91
+ return this . entries
86
92
}
87
- if ( this . dimension !== 0 ) {
88
- throw new Error ( 'array dimension not balanced' )
89
- }
90
- return this . entries
91
93
}
92
94
93
95
function identity ( value ) {
0 commit comments