5
5
'use strict' ;
6
6
7
7
import * as assert from 'assert' ;
8
- import { FormattingOptions , Edit , ModificationOptions } from '../main' ;
9
- import { setProperty , removeProperty } from '../impl/edit' ;
8
+ import { FormattingOptions , Edit , ModificationOptions , modify } from '../main' ;
10
9
11
10
suite ( 'JSON - edits' , ( ) => {
12
11
@@ -36,190 +35,190 @@ suite('JSON - edits', () => {
36
35
37
36
test ( 'set property' , ( ) => {
38
37
let content = '{\n "x": "y"\n}' ;
39
- let edits = setProperty ( content , [ 'x' ] , 'bar' , options ) ;
38
+ let edits = modify ( content , [ 'x' ] , 'bar' , options ) ;
40
39
assertEdit ( content , edits , '{\n "x": "bar"\n}' ) ;
41
40
42
41
content = 'true' ;
43
- edits = setProperty ( content , [ ] , 'bar' , options ) ;
42
+ edits = modify ( content , [ ] , 'bar' , options ) ;
44
43
assertEdit ( content , edits , '"bar"' ) ;
45
44
46
45
content = '{\n "x": "y"\n}' ;
47
- edits = setProperty ( content , [ 'x' ] , { key : true } , options ) ;
46
+ edits = modify ( content , [ 'x' ] , { key : true } , options ) ;
48
47
assertEdit ( content , edits , '{\n "x": {\n "key": true\n }\n}' ) ;
49
48
50
49
content = '{\n "a": "b", "x": "y"\n}' ;
51
- edits = setProperty ( content , [ 'a' ] , null , options ) ;
50
+ edits = modify ( content , [ 'a' ] , null , options ) ;
52
51
assertEdit ( content , edits , '{\n "a": null, "x": "y"\n}' ) ;
53
52
} ) ;
54
53
55
54
test ( 'insert property' , ( ) => {
56
55
let content = '{}' ;
57
- let edits = setProperty ( content , [ 'foo' ] , 'bar' , options ) ;
56
+ let edits = modify ( content , [ 'foo' ] , 'bar' , options ) ;
58
57
assertEdit ( content , edits , '{\n "foo": "bar"\n}' ) ;
59
58
60
- edits = setProperty ( content , [ 'foo' , 'foo2' ] , 'bar' , options ) ;
59
+ edits = modify ( content , [ 'foo' , 'foo2' ] , 'bar' , options ) ;
61
60
assertEdit ( content , edits , '{\n "foo": {\n "foo2": "bar"\n }\n}' ) ;
62
61
63
62
content = '{\n}' ;
64
- edits = setProperty ( content , [ 'foo' ] , 'bar' , options ) ;
63
+ edits = modify ( content , [ 'foo' ] , 'bar' , options ) ;
65
64
assertEdit ( content , edits , '{\n "foo": "bar"\n}' ) ;
66
65
67
66
content = ' {\n }' ;
68
- edits = setProperty ( content , [ 'foo' ] , 'bar' , options ) ;
67
+ edits = modify ( content , [ 'foo' ] , 'bar' , options ) ;
69
68
assertEdit ( content , edits , ' {\n "foo": "bar"\n }' ) ;
70
69
71
70
content = '{\n "x": "y"\n}' ;
72
- edits = setProperty ( content , [ 'foo' ] , 'bar' , options ) ;
71
+ edits = modify ( content , [ 'foo' ] , 'bar' , options ) ;
73
72
assertEdit ( content , edits , '{\n "x": "y",\n "foo": "bar"\n}' ) ;
74
73
75
74
content = '{\n "x": "y"\n}' ;
76
- edits = setProperty ( content , [ 'e' ] , 'null' , options ) ;
75
+ edits = modify ( content , [ 'e' ] , 'null' , options ) ;
77
76
assertEdit ( content , edits , '{\n "x": "y",\n "e": "null"\n}' ) ;
78
77
79
- edits = setProperty ( content , [ 'x' ] , 'bar' , options ) ;
78
+ edits = modify ( content , [ 'x' ] , 'bar' , options ) ;
80
79
assertEdit ( content , edits , '{\n "x": "bar"\n}' ) ;
81
80
82
81
content = '{\n "x": {\n "a": 1,\n "b": true\n }\n}\n' ;
83
- edits = setProperty ( content , [ 'x' ] , 'bar' , options ) ;
82
+ edits = modify ( content , [ 'x' ] , 'bar' , options ) ;
84
83
assertEdit ( content , edits , '{\n "x": "bar"\n}\n' ) ;
85
84
86
- edits = setProperty ( content , [ 'x' , 'b' ] , 'bar' , options ) ;
85
+ edits = modify ( content , [ 'x' , 'b' ] , 'bar' , options ) ;
87
86
assertEdit ( content , edits , '{\n "x": {\n "a": 1,\n "b": "bar"\n }\n}\n' ) ;
88
87
89
- edits = setProperty ( content , [ 'x' , 'c' ] , 'bar' , { formattingOptions, getInsertionIndex : ( ) => 0 } ) ;
88
+ edits = modify ( content , [ 'x' , 'c' ] , 'bar' , { formattingOptions, getInsertionIndex : ( ) => 0 } ) ;
90
89
assertEdit ( content , edits , '{\n "x": {\n "c": "bar",\n "a": 1,\n "b": true\n }\n}\n' ) ;
91
90
92
- edits = setProperty ( content , [ 'x' , 'c' ] , 'bar' , { formattingOptions, getInsertionIndex : ( ) => 1 } ) ;
91
+ edits = modify ( content , [ 'x' , 'c' ] , 'bar' , { formattingOptions, getInsertionIndex : ( ) => 1 } ) ;
93
92
assertEdit ( content , edits , '{\n "x": {\n "a": 1,\n "c": "bar",\n "b": true\n }\n}\n' ) ;
94
93
95
- edits = setProperty ( content , [ 'x' , 'c' ] , 'bar' , { formattingOptions, getInsertionIndex : ( ) => 2 } ) ;
94
+ edits = modify ( content , [ 'x' , 'c' ] , 'bar' , { formattingOptions, getInsertionIndex : ( ) => 2 } ) ;
96
95
assertEdit ( content , edits , '{\n "x": {\n "a": 1,\n "b": true,\n "c": "bar"\n }\n}\n' ) ;
97
96
98
- edits = setProperty ( content , [ 'c' ] , 'bar' , options ) ;
97
+ edits = modify ( content , [ 'c' ] , 'bar' , options ) ;
99
98
assertEdit ( content , edits , '{\n "x": {\n "a": 1,\n "b": true\n },\n "c": "bar"\n}\n' ) ;
100
99
101
100
content = '{\n "a": [\n {\n } \n ] \n}' ;
102
- edits = setProperty ( content , [ 'foo' ] , 'bar' , options ) ;
101
+ edits = modify ( content , [ 'foo' ] , 'bar' , options ) ;
103
102
assertEdit ( content , edits , '{\n "a": [\n {\n } \n ],\n "foo": "bar"\n}' ) ;
104
103
105
104
content = '' ;
106
- edits = setProperty ( content , [ 'foo' , 0 ] , 'bar' , options ) ;
105
+ edits = modify ( content , [ 'foo' , 0 ] , 'bar' , options ) ;
107
106
assertEdit ( content , edits , '{\n "foo": [\n "bar"\n ]\n}' ) ;
108
107
109
108
content = '//comment' ;
110
- edits = setProperty ( content , [ 'foo' , 0 ] , 'bar' , options ) ;
109
+ edits = modify ( content , [ 'foo' , 0 ] , 'bar' , options ) ;
111
110
assertEdit ( content , edits , '{\n "foo": [\n "bar"\n ]\n} //comment' ) ;
112
111
} ) ;
113
112
114
113
test ( 'remove property' , ( ) => {
115
114
let content = '{\n "x": "y"\n}' ;
116
- let edits = removeProperty ( content , [ 'x' ] , options ) ;
115
+ let edits = modify ( content , [ 'x' ] , undefined , options ) ;
117
116
assertEdit ( content , edits , '{\n}' ) ;
118
117
119
118
content = '{\n "x": "y", "a": []\n}' ;
120
- edits = removeProperty ( content , [ 'x' ] , options ) ;
119
+ edits = modify ( content , [ 'x' ] , undefined , options ) ;
121
120
assertEdit ( content , edits , '{\n "a": []\n}' ) ;
122
121
123
122
content = '{\n "x": "y", "a": []\n}' ;
124
- edits = removeProperty ( content , [ 'a' ] , options ) ;
123
+ edits = modify ( content , [ 'a' ] , undefined , options ) ;
125
124
assertEdit ( content , edits , '{\n "x": "y"\n}' ) ;
126
125
} ) ;
127
126
128
127
test ( 'set item' , ( ) => {
129
128
let content = '{\n "x": [1, 2, 3],\n "y": 0\n}' ;
130
129
131
- let edits = setProperty ( content , [ 'x' , 0 ] , 6 , options ) ;
130
+ let edits = modify ( content , [ 'x' , 0 ] , 6 , options ) ;
132
131
assertEdit ( content , edits , '{\n "x": [6, 2, 3],\n "y": 0\n}' ) ;
133
132
134
- edits = setProperty ( content , [ 'x' , 1 ] , 5 , options ) ;
133
+ edits = modify ( content , [ 'x' , 1 ] , 5 , options ) ;
135
134
assertEdit ( content , edits , '{\n "x": [1, 5, 3],\n "y": 0\n}' ) ;
136
135
137
- edits = setProperty ( content , [ 'x' , 2 ] , 4 , options ) ;
136
+ edits = modify ( content , [ 'x' , 2 ] , 4 , options ) ;
138
137
assertEdit ( content , edits , '{\n "x": [1, 2, 4],\n "y": 0\n}' ) ;
139
138
140
- edits = setProperty ( content , [ 'x' , 3 ] , 3 , options ) ;
139
+ edits = modify ( content , [ 'x' , 3 ] , 3 , options ) ;
141
140
assertEdit ( content , edits , '{\n "x": [\n 1,\n 2,\n 3,\n 3\n ],\n "y": 0\n}' ) ;
142
141
} ) ;
143
142
144
143
test ( 'insert item at 0; isArrayInsertion = true' , ( ) => {
145
144
let content = '[\n 2,\n 3\n]' ;
146
- let edits = setProperty ( content , [ 0 ] , 1 , { formattingOptions, isArrayInsertion : true } ) ;
145
+ let edits = modify ( content , [ 0 ] , 1 , { formattingOptions, isArrayInsertion : true } ) ;
147
146
assertEdit ( content , edits , '[\n 1,\n 2,\n 3\n]' ) ;
148
147
} ) ;
149
148
150
149
test ( 'insert item at 0 in empty array' , ( ) => {
151
150
let content = '[\n]' ;
152
- let edits = setProperty ( content , [ 0 ] , 1 , options ) ;
151
+ let edits = modify ( content , [ 0 ] , 1 , options ) ;
153
152
assertEdit ( content , edits , '[\n 1\n]' ) ;
154
153
} ) ;
155
154
156
155
test ( 'insert item at an index; isArrayInsertion = true' , ( ) => {
157
156
let content = '[\n 1,\n 3\n]' ;
158
- let edits = setProperty ( content , [ 1 ] , 2 , { formattingOptions, isArrayInsertion : true } ) ;
157
+ let edits = modify ( content , [ 1 ] , 2 , { formattingOptions, isArrayInsertion : true } ) ;
159
158
assertEdit ( content , edits , '[\n 1,\n 2,\n 3\n]' ) ;
160
159
} ) ;
161
160
162
161
test ( 'insert item at an index in empty array' , ( ) => {
163
162
let content = '[\n]' ;
164
- let edits = setProperty ( content , [ 1 ] , 1 , options ) ;
163
+ let edits = modify ( content , [ 1 ] , 1 , options ) ;
165
164
assertEdit ( content , edits , '[\n 1\n]' ) ;
166
165
} ) ;
167
166
168
167
test ( 'insert item at end index' , ( ) => {
169
168
let content = '[\n 1,\n 2\n]' ;
170
- let edits = setProperty ( content , [ 2 ] , 3 , options ) ;
169
+ let edits = modify ( content , [ 2 ] , 3 , options ) ;
171
170
assertEdit ( content , edits , '[\n 1,\n 2,\n 3\n]' ) ;
172
171
} ) ;
173
172
174
173
test ( 'insert item at end to empty array' , ( ) => {
175
174
let content = '[\n]' ;
176
- let edits = setProperty ( content , [ - 1 ] , 'bar' , options ) ;
175
+ let edits = modify ( content , [ - 1 ] , 'bar' , options ) ;
177
176
assertEdit ( content , edits , '[\n "bar"\n]' ) ;
178
177
} ) ;
179
178
180
179
test ( 'insert item at end' , ( ) => {
181
180
let content = '[\n 1,\n 2\n]' ;
182
- let edits = setProperty ( content , [ - 1 ] , 'bar' , options ) ;
181
+ let edits = modify ( content , [ - 1 ] , 'bar' , options ) ;
183
182
assertEdit ( content , edits , '[\n 1,\n 2,\n "bar"\n]' ) ;
184
183
} ) ;
185
184
186
185
test ( 'remove item in array with one item' , ( ) => {
187
186
let content = '[\n 1\n]' ;
188
- let edits = setProperty ( content , [ 0 ] , void 0 , options ) ;
187
+ let edits = modify ( content , [ 0 ] , void 0 , options ) ;
189
188
assertEdit ( content , edits , '[]' ) ;
190
189
} ) ;
191
190
192
191
test ( 'remove item in the middle of the array' , ( ) => {
193
192
let content = '[\n 1,\n 2,\n 3\n]' ;
194
- let edits = setProperty ( content , [ 1 ] , void 0 , options ) ;
193
+ let edits = modify ( content , [ 1 ] , void 0 , options ) ;
195
194
assertEdit ( content , edits , '[\n 1,\n 3\n]' ) ;
196
195
} ) ;
197
196
198
197
test ( 'remove last item in the array' , ( ) => {
199
198
let content = '[\n 1,\n 2,\n "bar"\n]' ;
200
- let edits = setProperty ( content , [ 2 ] , void 0 , options ) ;
199
+ let edits = modify ( content , [ 2 ] , void 0 , options ) ;
201
200
assertEdit ( content , edits , '[\n 1,\n 2\n]' ) ;
202
201
} ) ;
203
202
204
203
test ( 'remove last item in the array if ends with comma' , ( ) => {
205
204
let content = '[\n 1,\n "foo",\n "bar",\n]' ;
206
- let edits = setProperty ( content , [ 2 ] , void 0 , options ) ;
205
+ let edits = modify ( content , [ 2 ] , void 0 , options ) ;
207
206
assertEdit ( content , edits , '[\n 1,\n "foo"\n]' ) ;
208
207
} ) ;
209
208
210
209
test ( 'remove last item in the array if there is a comment in the beginning' , ( ) => {
211
210
let content = '// This is a comment\n[\n 1,\n "foo",\n "bar"\n]' ;
212
- let edits = setProperty ( content , [ 2 ] , void 0 , options ) ;
211
+ let edits = modify ( content , [ 2 ] , void 0 , options ) ;
213
212
assertEdit ( content , edits , '// This is a comment\n[\n 1,\n "foo"\n]' ) ;
214
213
} ) ;
215
214
216
215
test ( 'set property without formatting' , ( ) => {
217
216
let content = '{\n "x": [1, 2, 3],\n "y": 0\n}' ;
218
217
219
- let edits = setProperty ( content , [ 'x' , 0 ] , { a : 1 , b : 2 } , { formattingOptions } ) ;
218
+ let edits = modify ( content , [ 'x' , 0 ] , { a : 1 , b : 2 } , { formattingOptions } ) ;
220
219
assertEdit ( content , edits , '{\n "x": [{\n "a": 1,\n "b": 2\n }, 2, 3],\n "y": 0\n}' ) ;
221
220
222
- edits = setProperty ( content , [ 'x' , 0 ] , { a : 1 , b : 2 } , { formattingOptions : undefined } ) ;
221
+ edits = modify ( content , [ 'x' , 0 ] , { a : 1 , b : 2 } , { formattingOptions : undefined } ) ;
223
222
assertEdit ( content , edits , '{\n "x": [{"a":1,"b":2}, 2, 3],\n "y": 0\n}' ) ;
224
223
} ) ;
225
224
} ) ;
0 commit comments