Skip to content

Commit

Permalink
fix: toJSON function receiving array keys as number instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
BridgeAR committed Mar 19, 2023
1 parent 5fb1724 commit 26dc000
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v2.4.3

- Fixed toJSON function receiving array keys as number instead of string
- Fixed replacer function receiving array keys as number instead of string
- Fixed replacer function not being called for TypedArray entries
- Improved performance to escape long strings that contain characters that need escaping
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ function configure (options) {
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
let i = 0
for (; i < maximumValuesToStringify - 1; i++) {
const tmp = stringifyArrayReplacer(i, value[i], stack, replacer, spacer, indentation)
const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation)
res += tmp !== undefined ? tmp : 'null'
res += join
}
const tmp = stringifyArrayReplacer(i, value[i], stack, replacer, spacer, indentation)
const tmp = stringifyArrayReplacer(String(i), value[i], stack, replacer, spacer, indentation)
res += tmp !== undefined ? tmp : 'null'
if (value.length - 1 > maximumBreadth) {
const removedKeys = value.length - maximumBreadth - 1
Expand Down Expand Up @@ -412,11 +412,11 @@ function configure (options) {
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
let i = 0
for (; i < maximumValuesToStringify - 1; i++) {
const tmp = stringifyIndent(i, value[i], stack, spacer, indentation)
const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation)
res += tmp !== undefined ? tmp : 'null'
res += join
}
const tmp = stringifyIndent(i, value[i], stack, spacer, indentation)
const tmp = stringifyIndent(String(i), value[i], stack, spacer, indentation)
res += tmp !== undefined ? tmp : 'null'
if (value.length - 1 > maximumBreadth) {
const removedKeys = value.length - maximumBreadth - 1
Expand Down Expand Up @@ -520,11 +520,11 @@ function configure (options) {
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
let i = 0
for (; i < maximumValuesToStringify - 1; i++) {
const tmp = stringifySimple(i, value[i], stack)
const tmp = stringifySimple(String(i), value[i], stack)
res += tmp !== undefined ? tmp : 'null'
res += ','
}
const tmp = stringifySimple(i, value[i], stack)
const tmp = stringifySimple(String(i), value[i], stack)
res += tmp !== undefined ? tmp : 'null'
if (value.length - 1 > maximumBreadth) {
const removedKeys = value.length - maximumBreadth - 1
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ const { test } = require('tap')
const { stringify } = require('./index.js')
const clone = require('clone')

test('toJSON receives array keys as string', function (assert) {
const object = [{
toJSON (key) {
assert.equal(key, '0')
return 42
}
}]

const expected = JSON.stringify(object)

let actual = stringify(object)
assert.equal(actual, expected)

actual = stringify(object, ['0'])
assert.equal(actual, expected)

// @ts-expect-error
actual = stringify(object, (key, value) => value)
assert.equal(actual, expected)

actual = stringify(object, null, 2)
assert.equal(actual, '[\n 42\n]')

assert.end()
})

test('circular reference to root', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.circle = fixture
Expand Down

0 comments on commit 26dc000

Please sign in to comment.