Skip to content

Commit 4eab558

Browse files
committed
Fix undefined values and simplify code
Fixes: #4
1 parent 4196f87 commit 4eab558

File tree

2 files changed

+57
-78
lines changed

2 files changed

+57
-78
lines changed

stable.js

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function strEscape (str) {
6363
// Full version: supports all options
6464
function stringifyFullFn (key, parent, stack, replacer, indent) {
6565
var i, res, join
66-
const mind = gap
66+
const startGap = gap
6767
var value = parent[key]
6868

6969
if (typeof value === 'object' && value !== null && typeof value.toJSON === 'function') {
@@ -104,11 +104,11 @@ function stringifyFullFn (key, parent, stack, replacer, indent) {
104104
const tmp = stringifyFullFn(i, value, stack, replacer, indent)
105105
res += tmp !== undefined ? tmp : 'null'
106106
if (gap !== '') {
107-
res += `\n${mind}`
107+
res += `\n${startGap}`
108108
}
109109
res += ']'
110110
stack.pop()
111-
gap = mind
111+
gap = startGap
112112
return res
113113
}
114114

@@ -124,39 +124,25 @@ function stringifyFullFn (key, parent, stack, replacer, indent) {
124124
res += `\n${gap}`
125125
join = `,\n${gap}`
126126
}
127-
var last = false
128-
for (i = 0; i < keys.length - 1; i++) {
127+
let separator = ''
128+
for (i = 0; i < keys.length; i++) {
129129
key = keys[i]
130130
const tmp = stringifyFullFn(key, value, stack, replacer, indent)
131131
if (tmp !== undefined) {
132-
if (last) {
133-
res += join
134-
}
135-
res += `"${strEscape(key)}"${gap !== '' ? ': ' : ':'}${tmp}`
136-
last = true
132+
res += `${separator}"${strEscape(key)}"${gap !== '' ? ': ' : ':'}${tmp}`
133+
separator = join
137134
}
138135
}
139-
key = keys[i]
140-
const tmp = stringifyFullFn(key, value, stack, replacer, indent)
141-
if (tmp !== undefined) {
142-
if (last) {
143-
res += join
144-
}
145-
if (gap === '') {
146-
res += `"${strEscape(key)}":${tmp}`
147-
} else {
148-
res += `"${strEscape(key)}": ${tmp}\n${mind}`
149-
}
150-
} else if (gap !== '') {
151-
if (last) {
152-
res += `\n${mind}`
136+
if (gap !== '') {
137+
if (separator !== '') {
138+
res += `\n${startGap}`
153139
} else {
154140
res = '{'
155141
}
156142
}
157143
res += '}'
158144
stack.pop()
159-
gap = mind
145+
gap = startGap
160146
return res
161147
case 'string':
162148
return `"${strEscape(value)}"`
@@ -170,7 +156,7 @@ function stringifyFullFn (key, parent, stack, replacer, indent) {
170156

171157
function stringifyFullArr (key, value, stack, replacer, indent) {
172158
var i, res, join
173-
const mind = gap
159+
const startGap = gap
174160

175161
if (typeof value === 'object' && value !== null && typeof value.toJSON === 'function') {
176162
value = value.toJSON(key)
@@ -209,11 +195,11 @@ function stringifyFullArr (key, value, stack, replacer, indent) {
209195
const tmp = stringifyFullArr(i, value[i], stack, replacer, indent)
210196
res += tmp !== undefined ? tmp : 'null'
211197
if (gap !== '') {
212-
res += `\n${mind}`
198+
res += `\n${startGap}`
213199
}
214200
res += ']'
215201
stack.pop()
216-
gap = mind
202+
gap = startGap
217203
return res
218204
}
219205

@@ -228,30 +214,27 @@ function stringifyFullArr (key, value, stack, replacer, indent) {
228214
res += `\n${gap}`
229215
join = `,\n${gap}`
230216
}
231-
var last = false
217+
let separator = ''
232218
for (i = 0; i < replacer.length; i++) {
233219
if (typeof replacer[i] === 'string' || typeof replacer[i] === 'number') {
234220
key = replacer[i]
235221
const tmp = stringifyFullArr(key, value[key], stack, replacer, indent)
236222
if (tmp !== undefined) {
237-
if (last) {
238-
res += join
239-
}
240-
res += `"${strEscape(key)}"${gap ? ': ' : ':'}${tmp}`
241-
last = true
223+
res += `${separator}"${strEscape(key)}"${gap ? ': ' : ':'}${tmp}`
224+
separator = join
242225
}
243226
}
244227
}
245228
if (gap !== '') {
246-
if (last) {
247-
res += `\n${mind}`
229+
if (separator !== '') {
230+
res += `\n${startGap}`
248231
} else {
249232
res = '{'
250233
}
251234
}
252235
res += '}'
253236
stack.pop()
254-
gap = mind
237+
gap = startGap
255238
return res
256239
case 'string':
257240
return `"${strEscape(value)}"`
@@ -265,8 +248,8 @@ function stringifyFullArr (key, value, stack, replacer, indent) {
265248

266249
// Supports only the spacer option
267250
function stringifyIndent (key, value, stack, indent) {
268-
var i, res, join, add
269-
const mind = gap
251+
var i, res, join
252+
const startGap = gap
270253

271254
switch (typeof value) {
272255
case 'object':
@@ -311,11 +294,11 @@ function stringifyIndent (key, value, stack, indent) {
311294
const tmp = stringifyIndent(i, value[i], stack, indent)
312295
res += tmp !== undefined ? tmp : 'null'
313296
if (gap !== '') {
314-
res += `\n${mind}`
297+
res += `\n${startGap}`
315298
}
316299
res += ']'
317300
stack.pop()
318-
gap = mind
301+
gap = startGap
319302
return res
320303
}
321304

@@ -327,33 +310,29 @@ function stringifyIndent (key, value, stack, indent) {
327310
res = '{'
328311
if (gap === '') {
329312
join = ','
330-
add = ''
331313
} else {
332-
add = `\n${gap}`
314+
res += `\n${gap}`
333315
join = `,\n${gap}`
334316
}
335-
for (i = 0; i < keys.length - 1; i++) {
317+
let separator = ''
318+
for (i = 0; i < keys.length; i++) {
336319
key = keys[i]
337320
const tmp = stringifyIndent(key, value[key], stack, indent)
338321
if (tmp !== undefined) {
339-
add += `"${strEscape(key)}"${gap ? ': ' : ':'}${tmp}${join}`
322+
res += `${separator}"${strEscape(key)}"${gap ? ': ' : ':'}${tmp}`
323+
separator = join
340324
}
341325
}
342-
key = keys[i]
343-
const tmp = stringifyIndent(key, value[key], stack, indent)
344-
if (tmp !== undefined) {
345-
if (gap === '') {
346-
add += `"${strEscape(key)}":${tmp}`
326+
if (gap !== '') {
327+
if (separator !== '') {
328+
res += `\n${startGap}`
347329
} else {
348-
add += `"${strEscape(key)}": ${tmp}\n${mind}`
330+
res = '{'
349331
}
350332
}
351-
if (add.length > gap.length + 1) {
352-
res += add
353-
}
354333
res += '}'
355334
stack.pop()
356-
gap = mind
335+
gap = startGap
357336
return res
358337
case 'string':
359338
return `"${strEscape(value)}"`
@@ -407,17 +386,14 @@ function stringifyReplacerArr (key, value, stack, replacer) {
407386
}
408387
stack.push(value)
409388
res = '{'
410-
var last = false
389+
let separator = ''
411390
for (i = 0; i < replacer.length; i++) {
412391
if (typeof replacer[i] === 'string' || typeof replacer[i] === 'number') {
413392
key = replacer[i]
414393
const tmp = stringifyReplacerArr(key, value[key], stack, replacer)
415394
if (tmp !== undefined) {
416-
if (last) {
417-
res += ','
418-
}
419-
res += `"${strEscape(key)}":${tmp}`
420-
last = true
395+
res += `${separator}"${strEscape(key)}":${tmp}`
396+
separator = ','
421397
}
422398
}
423399
}
@@ -478,25 +454,14 @@ function stringifyReplacerFn (key, parent, stack, replacer) {
478454
}
479455
stack.push(value)
480456
res = '{'
481-
var last = false
482-
for (i = 0; i < keys.length - 1; i++) {
457+
let separator = ''
458+
for (i = 0; i < keys.length; i++) {
483459
key = keys[i]
484460
const tmp = stringifyReplacerFn(key, value, stack, replacer)
485461
if (tmp !== undefined) {
486-
if (last === true) {
487-
res += ','
488-
}
489-
res += `"${strEscape(key)}":${tmp}`
490-
last = true
491-
}
492-
}
493-
key = keys[i]
494-
const tmp = stringifyReplacerFn(key, value, stack, replacer)
495-
if (tmp !== undefined) {
496-
if (last === true) {
497-
res += ','
462+
res += `${separator}"${strEscape(key)}":${tmp}`
463+
separator = ','
498464
}
499-
res += `"${strEscape(key)}":${tmp}`
500465
}
501466
res += '}'
502467
stack.pop()

test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,28 @@ test('indentation with elements', function (assert) {
361361
})
362362

363363
test('object with undefined values', function (assert) {
364-
let obj, expected, actual
364+
let obj = { a: 1, c: undefined, b: 'hello' }
365365

366-
obj = { a: 1, c: undefined, b: 'hello' }
366+
let expected = JSON.stringify(obj)
367+
let actual = stringify(obj)
368+
assert.is(actual, expected)
369+
370+
obj = { b: 'hello', a: undefined, c: 1 }
367371

368372
expected = JSON.stringify(obj)
369373
actual = stringify(obj)
370374
assert.is(actual, expected)
371375

376+
assert.end()
377+
})
378+
379+
test('undefined values and indented', function (assert) {
380+
let obj = { a: 1, c: undefined, b: 'hello' }
381+
382+
let expected = JSON.stringify(obj, null, 2)
383+
let actual = stringify(obj, null, 2)
384+
assert.is(actual, expected)
385+
372386
obj = { b: 'hello', a: undefined, c: 1 }
373387

374388
expected = JSON.stringify(obj)

0 commit comments

Comments
 (0)