feat: add reviver option for decode#294
Conversation
Mirrors the existing `replacer` option for encode. The reviver function is called bottom-up (leaves first) during decode, matching JSON.parse reviver semantics. Supports value transformation, property filtering via `undefined` return, and path tracking. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The bottom-up traversal matches Three things before merge:
Once these land, this is ready to merge. |
Summary
Adds a
reviveroption toDecodeOptions, mirroring the existingreplacerforEncodeOptions. The reviver transforms values during decode, matchingJSON.parsereviver semantics (bottom-up traversal: leaves first, then parents).Why this matters
JSON.stringifyhasreplacer,JSON.parsehasreviver. TOON'sencodealready hasreplacer. The decode side had no equivalent, forcing users to walk the decoded tree manually for any post-decode transformation.PR #161 (quoteStrings) was closed in favor of the general-purpose
replacer, confirming the preference for general mechanisms over specific flags.reviverfollows the same philosophy for the decode side.Usage
Changes
packages/toon/src/types.ts: addedDecodeRevivertype andreviveroption toDecodeOptionspackages/toon/src/decode/reviver.ts: new module implementing bottom-up value traversal (mirrorsencode/replacer.ts)packages/toon/src/index.ts: wired reviver intodecodeFromLines(), exportedDecodeRevivertypepackages/toon/test/reviver.test.ts: 22 tests covering filtering, transformation, root handling, bottom-up traversal, path tracking, edge cases, and JSON.parse parityDemo
Testing
All 488 tests pass (22 new + 466 existing). Lint, build, and type checks clean.
This contribution was developed with AI assistance (Claude Code).