diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e290b6..29d3df9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/index.js b/index.js index fd55add..71b5efd 100644 --- a/index.js +++ b/index.js @@ -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) diff --git a/test.js b/test.js index 1321c3c..7c95e90 100644 --- a/test.js +++ b/test.js @@ -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