Skip to content

Commit

Permalink
fix: pass array keys as string to replacer function
Browse files Browse the repository at this point in the history
Array keys were wrongly passed through as number before.

Fixes: #42
  • Loading branch information
BridgeAR committed Mar 19, 2023
1 parent c466573 commit 5c078f2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v2.4.3

- Fixed replacer function receiving array keys as number instead of string

## v2.4.2

- Improved ESM TypeScript types.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ function configure (options) {
const maximumValuesToStringify = Math.min(value.length, maximumBreadth)
let i = 0
for (; i < maximumValuesToStringify - 1; i++) {
const tmp = stringifyFnReplacer(i, value, stack, replacer, spacer, indentation)
const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation)
res += tmp !== undefined ? tmp : 'null'
res += join
}
const tmp = stringifyFnReplacer(i, value, stack, replacer, spacer, indentation)
const tmp = stringifyFnReplacer(String(i), value, stack, replacer, spacer, indentation)
res += tmp !== undefined ? tmp : 'null'
if (value.length - 1 > maximumBreadth) {
const removedKeys = value.length - maximumBreadth - 1
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ test('replacer removing elements and indentation', function (assert) {

test('replacer removing all elements', function (assert) {
const replacer = function (k, v) {
assert.type(k, 'string')
if (k !== '') return
return k
}
Expand Down

0 comments on commit 5c078f2

Please sign in to comment.