Skip to content

Add hidden option to drop fields from the result - #353

Open
keichi wants to merge 1 commit into
masterfrom
hidden-option
Open

keichi wants to merge 1 commit into
masterfrom
hidden-option

Conversation

@keichi

@keichi keichi commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Closes #74.

Adds a hidden: true common option. The field is parsed and stays readable by later fields of the same parser (as length, tag, in formatter/assert, via $parent from nested parsers), and is deleted from the result when that parser finishes.

new Parser()
  .uint8("len", { hidden: true })
  .array("xs", { type: "uint8", length: "len" })
  .parse(Uint8Array.of(2, 7, 8));
// => { xs: [7, 8] }

Implementation: Parser#generate() emits delete <field>; for each hidden field after generating the whole chain. generate() runs once per scope (top level, nest, array item, choice, pointer, aliased parser), so this covers every scope from one place. The field name comes from varName (already sanitized); only a boolean is read from the option.

Notes:

  • In an anonymous nest/choice (empty varName) the fields are merged into the parent, but a hidden field there is deleted when the nested parser ends, so later parent fields cannot reference it.
  • A hidden field cannot be used as an array's key, since the key is read after the item parser finishes.

Tests cover top level, nest, array items, $parent access, bit fields and a hidden nest.

A hidden field can be referenced by later fields of the same parser (e.g.
as an array length) and is deleted once that parser finishes. Closes #74.
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.

Take a parsed value for computing not for parsing

1 participant