1
1
const nearley = require ( 'nearley' )
2
2
const grammar = require ( './grammar' )
3
3
4
- function simplify ( data ) {
5
- function transform ( value , type ) {
4
+ function simplify ( data ) {
5
+ function transform ( value , type ) {
6
6
if ( type === 'compound' ) {
7
7
return Object . keys ( value ) . reduce ( function ( acc , key ) {
8
8
acc [ key ] = simplify ( value [ key ] )
@@ -17,53 +17,59 @@ function simplify (data) {
17
17
return transform ( data . value , data . type )
18
18
}
19
19
20
- function stringify ( { value, type } ) {
20
+ function stringify ( { value, type } , quotes = false ) {
21
21
if ( type === 'compound' ) {
22
22
const str = [ ]
23
23
const entries = Object . entries ( value )
24
24
for ( let i = 0 ; i < entries . length ; i ++ ) {
25
25
const _type = entries [ i ] [ 0 ]
26
- let _value = stringify ( entries [ i ] [ 1 ] )
27
- if ( _type === 'string' ) _value = normalizeString ( _value )
28
- str . push ( `${ _type } :${ _value } ` )
26
+ let _value = stringify ( entries [ i ] [ 1 ] , quotes )
27
+ if ( _type === 'string' ) _value = normalizeString ( _value , quotes )
28
+ if ( quotes )
29
+ str . push ( `"${ _type } ":${ _value } ` )
30
+ else
31
+ str . push ( `${ _type } :${ _value } ` )
32
+
29
33
}
30
34
return `{${ str . join ( ',' ) } }`
31
35
} else if ( type === 'list' ) {
32
36
if ( ! Array . isArray ( value . value ) ) return '[]'
33
- const arrayElements = getArrayValues ( value )
37
+ const arrayElements = getArrayValues ( value , quotes )
34
38
return `[${ arrayElements } ]`
35
39
} else if ( type === 'byteArray' || type === 'intArray' || type === 'longArray' ) {
36
40
const prefix = getArrayPrefix ( type )
37
- const arrayElements = getArrayValues ( value )
41
+ const arrayElements = getArrayValues ( value , quotes )
38
42
return `[${ prefix } ${ arrayElements } ]`
39
43
}
40
44
let str = value + getSuffix ( value , type )
41
- if ( type === 'string' ) str = normalizeString ( str )
45
+ if ( type === 'string' ) str = normalizeString ( str , quotes )
42
46
return str
43
47
}
44
48
45
- function normalizeString ( str ) {
49
+ function normalizeString ( str , quotes ) {
46
50
str = str . replace ( / " / g, '\\"' )
47
51
if ( / ' | { | } | \[ | \] | : | ; | , | \( | \) | § | = / g. test ( str ) || str === '' ) str = `"${ str } "`
52
+ if ( quotes && ! str . startsWith ( '"' ) )
53
+ str = `"${ str } "`
48
54
return str
49
55
}
50
56
51
- function getArrayValues ( { value : arr , type } ) {
57
+ function getArrayValues ( { value : arr , type } , quotes ) {
52
58
const hasMissingEl = hasMissingElements ( arr )
53
59
const str = [ ]
54
60
// add nullable length that way [] is pased as []
55
61
for ( let i = 0 ; i < arr . length ; i ++ ) {
56
62
let curr = arr [ i ]
57
63
if ( curr !== undefined ) {
58
- curr = stringify ( { value : curr , type } )
64
+ curr = stringify ( { value : curr , type } , quotes )
59
65
if ( hasMissingEl ) str . push ( `${ i } :${ curr } ` )
60
66
else str . push ( curr )
61
67
}
62
68
}
63
69
return str . join ( ',' )
64
70
}
65
71
66
- function hasMissingElements ( arr ) {
72
+ function hasMissingElements ( arr ) {
67
73
for ( let i = 0 ; i < arr . length ; i ++ ) {
68
74
if ( arr [ i ] === undefined ) return true
69
75
}
@@ -72,7 +78,7 @@ function hasMissingElements (arr) {
72
78
73
79
const getArrayPrefix = type => type . charAt ( 0 ) . toUpperCase ( ) + ';'
74
80
75
- function getSuffix ( val , type ) {
81
+ function getSuffix ( val , type ) {
76
82
if ( type === 'double' ) return ( ( val >> 0 ) === val ) ? 'd' : ''
77
83
return { int : '' , byte : 'b' , short : 's' , float : 'f' , long : 'l' , string : '' } [ type ]
78
84
}
@@ -83,11 +89,11 @@ function getSuffix (val, type) {
83
89
* @returns {string } the normalized mojangson
84
90
*/
85
91
86
- function normalize ( str ) {
87
- return stringify ( parse ( str ) )
92
+ function normalize ( str , quotes = false ) {
93
+ return stringify ( parse ( str ) , quotes )
88
94
}
89
95
90
- function parse ( text ) {
96
+ function parse ( text ) {
91
97
try {
92
98
const parserNE = new nearley . Parser ( nearley . Grammar . fromCompiled ( grammar ) )
93
99
parserNE . feed ( text )
@@ -103,4 +109,4 @@ module.exports = {
103
109
simplify,
104
110
stringify,
105
111
normalize
106
- }
112
+ }
0 commit comments