1
- const sorter = require ( './sort-json-utils' ) ;
2
1
const babylon = require ( 'babylon' ) ;
3
2
const generate = require ( '@babel/generator' ) ;
4
3
@@ -28,166 +27,10 @@ function getAutoIndent(selectedText, indent) {
28
27
return autoIndent ;
29
28
}
30
29
31
- var MARK_START = String . fromCharCode ( 193 ) ;
32
- var MARK_END = String . fromCharCode ( 201 ) ;
33
-
34
- function generateMark ( number ) {
35
- return `${ MARK_START } ${ number } ${ MARK_END } ` ;
36
- }
37
-
38
- function getMarkRegex ( ) {
39
- return new RegExp ( `${ MARK_START } ([0-9]+)${ MARK_END } ` , 'g' ) ;
40
- }
41
-
42
- function getNeedReverseMarkRegex ( ) {
43
- return new RegExp ( `"${ MARK_START } ([0-9]+)${ MARK_END } ([^"]*)"` , 'g' ) ;
44
- }
45
-
46
- function getReverseBackMarkRegex ( ) {
47
- return new RegExp ( `"([^"]*)${ MARK_END } ([0-9]+)${ MARK_START } "` , 'g' ) ;
48
- }
49
-
50
- function addMark ( marks , value ) {
51
- var length = marks . length ;
52
- var mark = generateMark ( length ) ;
53
- marks . push ( value ) ;
54
- return mark ;
55
- }
56
-
57
- function decodeMark ( sortedJsonText , marks ) {
58
- return (
59
- sortedJsonText
60
- // replace the reverse mark
61
- . replace ( getReverseBackMarkRegex ( ) , ( match , value , markIndex ) => {
62
- return `"${ marks [ markIndex ] } ${ value } "` ;
63
- } )
64
- // replace mark
65
- . replace ( getMarkRegex ( ) , ( match , markIndex ) => {
66
- return marks [ markIndex ] ;
67
- } )
68
- ) ;
69
- }
70
-
71
- function objectStringToJson ( objectString , marks , autoIndent ) {
72
- var autoIndentString = new Array ( autoIndent ) . fill ( ' ' ) . join ( '' ) ;
73
-
74
- var json = objectString
75
- // Add mark for \' and \"
76
- . replace ( / \\ ( \" | \' ) / g, match => addMark ( marks , match ) )
77
- // Add mark for "
78
- . replace ( / \" / g, match => addMark ( marks , match ) )
79
- // Add mark for end line comments
80
- . replace (
81
- / ( \/ \/ .* ) ( \n | \t ) \s * \} / g,
82
- ( match , comments ) => `${ addMark ( marks , `${ comments } ` ) } }`
83
- )
84
- // Add mark for line comments
85
- . replace ( / ( \/ \/ .* ) / g, comments =>
86
- addMark ( marks , `${ comments } \n${ autoIndentString } ` )
87
- )
88
- // Change the multiple lines to one line
89
- . replace ( / \s * ( \n | \t ) \s * / g, '' )
90
- // Replace the " with '
91
- // .replace(/"/g, `'`)
92
- // Add "" to all the keys and values
93
- . replace ( / ( [ ^ \{ \} \[ \] : , ] + ) / g, match => {
94
- var realMatch = match . trim ( ) ;
95
- return realMatch ? `"${ match . trim ( ) } "` : realMatch ;
96
- } )
97
- // reverse mark if it is at the beginning of the line
98
- . replace (
99
- getNeedReverseMarkRegex ( ) ,
100
- ( match , markIndex , value ) =>
101
- `"${ value } ${ MARK_END } ${ markIndex } ${ MARK_START } "`
102
- ) ;
103
-
104
- var es6ShortLandRegex = / ( [ \{ , ] + ) \s * ( " [ ^ \{ \} : , ] + " ) \s * ( [ \} , ] + ) / g;
105
- var isEs6 = false ;
106
- while ( json . match ( es6ShortLandRegex ) ) {
107
- // Change ES6 single value to be JSON
108
- json = json . replace (
109
- es6ShortLandRegex ,
110
- ( match , $1 , $2 , $3 ) => `${ $1 } ${ $2 } :${ $2 } ${ $3 } `
111
- ) ;
112
- isEs6 = true ;
113
- }
114
-
115
- return {
116
- json,
117
- isEs6,
118
- } ;
119
- }
120
-
121
- function jsonToObjectString ( json , needTailingComma , autoIndent , isEs6 ) {
122
- var objectString = json
123
- // remove the "" to all the keys and values
124
- . replace ( / " ( [ ^ \{ \} : , ] + ) " / g, ( match , $1 ) => $1 ) ;
125
-
126
- if ( autoIndent ) {
127
- var objectStringLines = objectString . split ( '\n' ) ;
128
- var autoIndentString = new Array ( autoIndent ) . fill ( ' ' ) . join ( '' ) ;
129
-
130
- for ( var i = 1 ; i < objectStringLines . length ; i ++ ) {
131
- objectStringLines [ i ] = autoIndentString + objectStringLines [ i ] ;
132
- }
133
-
134
- objectString = objectStringLines . join ( '\n' ) ;
135
- }
136
-
137
- if ( needTailingComma ) {
138
- var tailingCommaRegex = / ( [ ^ \{ : , \s \t \n ] + ) ( [ \s \t \n ] * [ \] \} ] ) / g;
139
- while ( objectString . match ( tailingCommaRegex ) ) {
140
- objectString = objectString . replace (
141
- tailingCommaRegex ,
142
- ( match , $1 , $2 ) => `${ $1 } ,${ $2 } `
143
- ) ;
144
- }
145
- }
146
-
147
- if ( isEs6 ) {
148
- console . log ( `isEs6: ${ isEs6 } ` ) ;
149
-
150
- objectString = objectString
151
- . split ( '\n' )
152
- . map ( line => {
153
- console . log ( `line: ${ line } ` ) ;
154
-
155
- var keyValuePair = line . replace ( / , [ \s ] * / , '' ) . split ( ':' ) ;
156
- if (
157
- keyValuePair . length === 2 &&
158
- keyValuePair [ 0 ] . trim ( ) === keyValuePair [ 1 ] . trim ( )
159
- ) {
160
- console . log ( `return: ${ `${ keyValuePair [ 0 ] } ,` } ` ) ;
161
- return `${ keyValuePair [ 0 ] } ,` ;
162
- } else {
163
- console . log ( `origin return: ${ line } ` ) ;
164
- return line ;
165
- }
166
- } )
167
- . join ( '\n' ) ;
168
- }
169
-
170
- // replace the comma at the end
171
- objectString = objectString . replace (
172
- / ( \/ \/ .* ) ( \n | \t ) ( \s * \} ) / g,
173
- ( match , comments , $2 , $3 ) => `${ comments . replace ( / , $ / , '' ) } ${ $2 } ${ $3 } `
174
- ) ;
175
-
176
- return objectString ;
177
- }
178
-
179
30
function checkIfNeedTailingComma ( objectString ) {
180
31
return objectString . match ( / , [ \s \t \n ] * \} / ) ;
181
32
}
182
33
183
- function checkIsObjectText ( selectedText ) {
184
- return (
185
- selectedText . length > 2 &&
186
- selectedText [ 0 ] === '{' &&
187
- selectedText [ selectedText . length - 1 ] === '}'
188
- ) ;
189
- }
190
-
191
34
function addAutoIndent ( text , autoIndent ) {
192
35
const autoIndentString = new Array ( autoIndent ) . fill ( ' ' ) . join ( '' ) ;
193
36
const lines = text . split ( '\n' ) ;
@@ -200,32 +43,29 @@ function addAutoIndent(text, autoIndent) {
200
43
] . join ( '\n' ) ;
201
44
}
202
45
203
- function selectedTextToSortedText (
204
- selectedText ,
205
- indent ,
206
- jsonParser ,
207
- sortOrder ,
208
- sortOptions
209
- ) {
210
- var needTailingComma = checkIfNeedTailingComma ( selectedText ) ;
211
-
212
- var autoIndent = getAutoIndent ( selectedText , indent ) ;
46
+ function addTaillingComma ( sortedText , needTailingComma ) {
47
+ if ( needTailingComma ) {
48
+ var tailingCommaRegex = / ( [ ^ \{ : , \s \t \n ] + ) ( [ \s \t \n ] * [ \] \} ] ) / g ;
49
+ while ( sortedText . match ( tailingCommaRegex ) ) {
50
+ sortedText = sortedText . replace (
51
+ tailingCommaRegex ,
52
+ ( match , $1 , $2 ) => ` ${ $1 } , ${ $2 } `
53
+ ) ;
54
+ }
55
+ }
213
56
214
- const declarPrefix = 'const a = ' ;
215
- let code = ` ${ declarPrefix } ${ selectedText } ` ;
57
+ return sortedText ;
58
+ }
216
59
217
- let ast ;
60
+ function swithCorrectIndent ( sortedText , indent ) {
61
+ return sortedText . replace ( / / g, new Array ( indent ) . fill ( ' ' ) . join ( '' ) ) ;
62
+ }
218
63
219
- try {
220
- ast = babylon . parse ( code , {
221
- sourceType : 'module' ,
222
- plugins : [ 'objectRestSpread' ] ,
223
- } ) ;
224
- } catch ( e ) {
225
- throw { message : 'Please make sure your selected text is a JS obejct!' } ;
226
- }
64
+ function removeWrapper ( sortedText , declarePrefix ) {
65
+ return sortedText . slice ( declarePrefix . length ) . replace ( / ; $ / , '' ) ;
66
+ }
227
67
228
- const object = ast . program . body [ 0 ] . declarations [ 0 ] . init ;
68
+ function sortObject ( object , sortOrder ) {
229
69
object . properties = object . properties . sort ( ( a , b ) => {
230
70
if ( ! a . key ) {
231
71
return 1 ;
@@ -247,23 +87,38 @@ function selectedTextToSortedText(
247
87
if ( sortOrder && sortOrder . indexOf ( 'desc' ) >= 0 ) {
248
88
object . properties = object . properties . reverse ( ) ;
249
89
}
90
+ }
250
91
251
- let sortedText = generate . default ( ast , { } , code ) . code ;
92
+ function selectedTextToSortedText ( selectedText , indent , sortOrder ) {
93
+ var needTailingComma = checkIfNeedTailingComma ( selectedText ) ;
252
94
253
- sortedText = sortedText . replace ( / / g, new Array ( indent ) . fill ( ' ' ) . join ( '' ) ) ;
254
- sortedText = sortedText . slice ( declarPrefix . length , sortedText . length - 1 ) ;
255
- sortedText = addAutoIndent ( sortedText , autoIndent ) ;
95
+ var autoIndent = getAutoIndent ( selectedText , indent ) ;
256
96
257
- if ( needTailingComma ) {
258
- var tailingCommaRegex = / ( [ ^ \{ : , \s \t \n ] + ) ( [ \s \t \n ] * [ \] \} ] ) / g;
259
- while ( sortedText . match ( tailingCommaRegex ) ) {
260
- sortedText = sortedText . replace (
261
- tailingCommaRegex ,
262
- ( match , $1 , $2 ) => `${ $1 } ,${ $2 } `
263
- ) ;
264
- }
97
+ const declarePrefix = 'const a = ' ;
98
+ let code = `${ declarePrefix } ${ selectedText } ` ;
99
+
100
+ let ast ;
101
+
102
+ try {
103
+ ast = babylon . parse ( code , {
104
+ sourceType : 'module' ,
105
+ plugins : [ 'objectRestSpread' ] ,
106
+ } ) ;
107
+ } catch ( e ) {
108
+ throw { message : 'Please make sure your selected text is a JS obejct!' } ;
265
109
}
266
110
111
+ let object = ast . program . body [ 0 ] . declarations [ 0 ] . init ;
112
+
113
+ sortObject ( object , sortOrder ) ;
114
+
115
+ let sortedText = generate . default ( ast , { } , code ) . code ;
116
+
117
+ sortedText = swithCorrectIndent ( sortedText , indent ) ;
118
+ sortedText = removeWrapper ( sortedText , declarePrefix ) ;
119
+ sortedText = addAutoIndent ( sortedText , autoIndent ) ;
120
+ sortedText = addTaillingComma ( sortedText , needTailingComma ) ;
121
+
267
122
return sortedText ;
268
123
}
269
124
0 commit comments