Skip to content

Commit 5fb1724

Browse files
committed
fix: call replacer functions for typed array entries as well
1 parent dbc5143 commit 5fb1724

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v2.4.3
44

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

89
## v2.4.2

index.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,8 @@ function configure (options) {
246246
join = `,\n${indentation}`
247247
whitespace = ' '
248248
}
249-
let maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth)
250-
if (isTypedArrayWithEntries(value)) {
251-
res += stringifyTypedArray(value, join, maximumBreadth)
252-
keys = keys.slice(value.length)
253-
maximumPropertiesToStringify -= value.length
254-
separator = join
255-
}
256-
if (deterministic) {
249+
const maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth)
250+
if (deterministic && !isTypedArrayWithEntries(value)) {
257251
keys = insertSort(keys)
258252
}
259253
stack.push(value)

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,22 @@ test('invalid replacer being ignored', function (assert) {
288288

289289
test('replacer removing elements', function (assert) {
290290
const replacer = function (k, v) {
291+
assert.type(k, 'string')
291292
if (k === 'remove') return
293+
if (k === '0') typedkeysInReplacer = true
292294
return v
293295
}
294296
const obj = { f: null, remove: true, typed: new Int32Array(1) }
297+
298+
let typedkeysInReplacer = false
295299
const expected = JSON.stringify(obj, replacer)
300+
assert.ok(typedkeysInReplacer)
301+
typedkeysInReplacer = false
302+
296303
let actual = stringify(obj, replacer)
304+
assert.ok(typedkeysInReplacer)
305+
typedkeysInReplacer = false
306+
297307
assert.equal(actual, expected)
298308

299309
obj.obj = obj

0 commit comments

Comments
 (0)