Skip to content

fix: array-form replacer should not filter Array elements - #106

Open
spokodev wants to merge 1 commit into
WebReflection:mainfrom
spokodev:fix/array-replacer-keeps-array-elements
Open

fix: array-form replacer should not filter Array elements#106
spokodev wants to merge 1 commit into
WebReflection:mainfrom
spokodev:fix/array-replacer-keeps-array-elements

Conversation

@spokodev

@spokodev spokodev commented Sep 2, 2026

Copy link
Copy Markdown

stringify(value, replacer) with an array-form replacer diverges from native JSON.stringify. Per the spec, an allow-list replacer array filters only plain-object properties — array elements are always serialized regardless of index. flatted filters both, so array elements whose index isn't in the list are silently dropped, and the loss survives a parse(stringify(...)) round-trip:

JSON.stringify(['a', 'b', 'c'], ['0'])                    // '["a","b","c"]'  — all kept
Flatted.parse(Flatted.stringify(['a', 'b', 'c'], ['0']))  // ['a', null, null] — 2 lost

The wrapper was an arrow function, so it could not see the holder (this) that $.call(this, ...) passes it. Switching to a normal function and exempting array holders (Array.isArray(this)) matches native semantics. cjs/index.js is regenerated via npm run cjs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant