Skip to content

Commit 26dc000

Browse files
committed
fix: toJSON function receiving array keys as number instead of string
1 parent 5fb1724 commit 26dc000

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v2.4.3
44

5+
- Fixed toJSON function receiving array keys as number instead of string
56
- Fixed replacer function receiving array keys as number instead of string
67
- Fixed replacer function not being called for TypedArray entries
78
- Improved performance to escape long strings that contain characters that need escaping

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,11 @@ function configure (options) {
322322
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
323323
let i = 0
324324
for (; i < maximumValuesToStringify - 1; i++) {
325-
const tmp = stringifyArrayReplacer(i, value[i], stack, replacer, spacer, indentation)
325+
const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation)
326326
res += tmp !== undefined ? tmp : 'null'
327327
res += join
328328
}
329-
const tmp = stringifyArrayReplacer(i, value[i], stack, replacer, spacer, indentation)
329+
const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation)
330330
res += tmp !== undefined ? tmp : 'null'
331331
if (value.length - 1 > maximumBreadth) {
332332
const removedKeys = value.length - maximumBreadth - 1
@@ -412,11 +412,11 @@ function configure (options) {
412412
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
413413
let i = 0
414414
for (; i < maximumValuesToStringify - 1; i++) {
415-
const tmp = stringifyIndent(i, value[i], stack, spacer, indentation)
415+
const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation)
416416
res += tmp !== undefined ? tmp : 'null'
417417
res += join
418418
}
419-
const tmp = stringifyIndent(i, value[i], stack, spacer, indentation)
419+
const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation)
420420
res += tmp !== undefined ? tmp : 'null'
421421
if (value.length - 1 > maximumBreadth) {
422422
const removedKeys = value.length - maximumBreadth - 1
@@ -520,11 +520,11 @@ function configure (options) {
520520
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
521521
let i = 0
522522
for (; i < maximumValuesToStringify - 1; i++) {
523-
const tmp = stringifySimple(i, value[i], stack)
523+
const tmp = stringifySimple(String(i), value[i], stack)
524524
res += tmp !== undefined ? tmp : 'null'
525525
res += ','
526526
}
527-
const tmp = stringifySimple(i, value[i], stack)
527+
const tmp = stringifySimple(String(i), value[i], stack)
528528
res += tmp !== undefined ? tmp : 'null'
529529
if (value.length - 1 > maximumBreadth) {
530530
const removedKeys = value.length - maximumBreadth - 1

test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@ const { test } = require('tap')
22
const { stringify } = require('./index.js')
33
const clone = require('clone')
44

5+
test('toJSON receives array keys as string', function (assert) {
6+
const object = [{
7+
toJSON (key) {
8+
assert.equal(key, '0')
9+
return 42
10+
}
11+
}]
12+
13+
const expected = JSON.stringify(object)
14+
15+
let actual = stringify(object)
16+
assert.equal(actual, expected)
17+
18+
actual = stringify(object, ['0'])
19+
assert.equal(actual, expected)
20+
21+
// @ts-expect-error
22+
actual = stringify(object, (key, value) => value)
23+
assert.equal(actual, expected)
24+
25+
actual = stringify(object, null, 2)
26+
assert.equal(actual, '[\n 42\n]')
27+
28+
assert.end()
29+
})
30+
531
test('circular reference to root', function (assert) {
632
const fixture = { name: 'Tywin Lannister' }
733
fixture.circle = fixture

0 commit comments

Comments
 (0)