Skip to content

Parse binary string

parseBinaryString parses binary string literals (0b…) into bigint values.

It enforces a strict binary‑only format and rejects any malformed or non‑binary input. If the value is not a string, does not match the binary literal pattern, or cannot be parsed by BigInt, the rule emits no parse result.

Signature

Through the API:

.parse('binary');

And internally:

export const parseBinaryString: ParseRule<unknown>
(value: unknown, path: FieldPath) => ParseResult<unknown>[]

Events

Event code Description
string.now.binary Parsed binary string into a bigint value.

Design rationale

  • Accepts only canonical binary bigint literals:

  • Must start with 0b.

  • Must contain only 0 and 1.
  • No underscores, whitespace, signs, or formatting
  • Rejects ambiguous formats such as "0b", "0b102", "0B101", "0b101", or "0b001_101".
  • Ensures contributors can rely on strict, predictable binary parsing.
  • Produces a single parse step containing the parsed bigint and one parseEvent.
  • Never guesses, normalizes, or coerces — parsing is explicit and opt‑in.

Invoke

parseBinaryString runs only when explicitly included in a boundary or pipeline. It is not affected by normalization mode and does not run automatically.

The rule activates when:

  • The value is a string.
  • The string matches the binary literal pattern: /^0b[01]+$/.
  • BigInt(value) succeeds.

Examples

Note

It is not necessary to convert bigint values to strings if you enable scan(), as all bigint values are converted to strings for security purposes when scan is enabled.

Valid binary string

const value = "0b101101";

const result = parseBinaryString(value, "$");
// → [
//     {
//       path: "$",
//       nextValue: 45n,
//       events: [
//         JaneEvent{ kind: "info", code: "string.now.binary", ... }
//       ]
//     }
//   ]

Invalid binary — no parse

parseBinaryString("0b102", "$");     // → []
parseBinaryString("0b", "$");        // → []
parseBinaryString("0B101", "$");     // → []
parseBinaryString("0b10_01", "$");   // → []

Non‑string value

parseBinaryString(42, "$"); // → []