Skip to content

Commit

Permalink
fix: call replacer functions for typed array entries as well
Browse files Browse the repository at this point in the history
  • Loading branch information
BridgeAR committed Mar 19, 2023
1 parent dbc5143 commit 5fb1724
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v2.4.3

- 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

## v2.4.2
Expand Down
10 changes: 2 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,8 @@ function configure (options) {
join = `,\n${indentation}`
whitespace = ' '
}
let maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth)
if (isTypedArrayWithEntries(value)) {
res += stringifyTypedArray(value, join, maximumBreadth)
keys = keys.slice(value.length)
maximumPropertiesToStringify -= value.length
separator = join
}
if (deterministic) {
const maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth)
if (deterministic && !isTypedArrayWithEntries(value)) {
keys = insertSort(keys)
}
stack.push(value)
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,22 @@ test('invalid replacer being ignored', function (assert) {

test('replacer removing elements', function (assert) {
const replacer = function (k, v) {
assert.type(k, 'string')
if (k === 'remove') return
if (k === '0') typedkeysInReplacer = true
return v
}
const obj = { f: null, remove: true, typed: new Int32Array(1) }

let typedkeysInReplacer = false
const expected = JSON.stringify(obj, replacer)
assert.ok(typedkeysInReplacer)
typedkeysInReplacer = false

let actual = stringify(obj, replacer)
assert.ok(typedkeysInReplacer)
typedkeysInReplacer = false

assert.equal(actual, expected)

obj.obj = obj
Expand Down

0 comments on commit 5fb1724

Please sign in to comment.