Parse scientific-notation string¶
parseScientificNotationString parses strict scientific‑notation literals into JavaScript numbers.
It enforces canonical formatting for both the mantissa and exponent and rejects any ambiguous or non‑standard numeric forms. If the value is not a string, does not match the scientific‑notation pattern, or cannot be converted into a valid number, the rule emits no parse result.
Signature¶
Through the API:
.parse('scientific');
And internally:
export const parseScientificNotationString: ParseRule<unknown>
(value: unknown, path: FieldPath) => ParseResult<unknown>[]
Events¶
| Event code | Description |
|---|---|
string.now.scientific |
Parsed string into a number using scientific notation. |
Design rationale¶
- Accepts only canonical scientific‑notation literals:
- Optional leading
-. - Digits, with optional decimal portion.
- Required exponent marker:
eorE. - Exponent may include an optional sign (
+or-). - Rejects ambiguous formats such as
"1e","1e-","1.e3","01e3","1e3.5"," 1e3 ", or"1_000e2". - Ensures contributors can rely on strict, predictable scientific‑notation parsing.
- Produces a single parse step containing the parsed number and one
parseEvent. - Never guesses, normalizes, or coerces — parsing is explicit and opt‑in.
Invoke¶
parseScientificNotationString 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 strict scientific‑notation pattern:
/^-?\d+(\.\d+)?[eE][+-]?\d+$/. Number(value)produces a valid finite number.
Examples¶
Valid scientific‑notation strings¶
parseScientificNotationString("1e3", "$");
// → [
// {
// path: "$",
// nextValue: 1000,
// events: [
// JaneEvent{ kind: "info", code: "string.now.scientific", ... }
// ]
// }
// ]
parseScientificNotationString("-2.5E+2", "$");
// → [
// {
// path: "$",
// nextValue: -250,
// events: [ JaneEvent{ code: "string.now.scientific", ... } ]
// }
// ]
Invalid formats — no parse¶
parseScientificNotationString("1e", "$"); // → []
parseScientificNotationString("1e-", "$"); // → []
parseScientificNotationString("01e3", "$"); // → []
parseScientificNotationString("1.e3", "$"); // → []
parseScientificNotationString("1e3.5", "$"); // → []
parseScientificNotationString(" 1e3 ", "$"); // → []
parseScientificNotationString("1_000e2", "$"); // → []
Non‑string value¶
parseScientificNotationString(42, "$"); // → []