Shares one fraction expander for durations - #160
Merged
Merged
Conversation
The grammar and the duration cast each turned a fractional component into whole units with their own arithmetic. Both now call one expandFraction in the unit-table module: whole-number arithmetic over the written digits, the remainder spent down the same ladder. The grammar keeps its duplicate-unit refusal and the cast keeps accumulation and its safe-range check where they were. Trailing zeros are counted back from the end instead of matched by an end-anchored pattern, which took time quadratic in a long zero run. A new test pins that a literal and a parsed text expand a fraction alike, refuse an inexact one alike, and read a long zero run in linear time. The comment above the safe-integer conversion test now states its reason. Refs: pts-kc88
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The grammar (a duration literal) and the parse behind
::durationandparseDurationeach turned a fractional duration component into whole units with their own arithmetic. They now call one function,expandFractioninsrc/duration-units.ts, beside the unit table they already shared.expandFractioninsrc/parser.tsis now a thin adapter that names each amount's unit by its suffix.expandComponentsand its refusal of a unit named twice after expansion are untouched.readDurationinsrc/cast.tscalls the shared expander and keeps accumulating a repeated unit. The check that refuses a component past the largest safe integer stays where it was, after the components are added.Provenance
parseDuration, which read such a text in about 2 ms. Both now read it in linear time. This is an engineering choice inside the bead's footprint. No answer changes, so there is no changelog fragment.test/fraction-expansion.test.ts. It checks that a literal and a parsed text expand a fraction alike and refuse an inexact one alike, and that a long zero run reads in linear time. The existing parser, cast and parse-duration test files are unchanged.test/duration-to-milliseconds.test.tsnow states the reason a plain number is kept, with its date: the bound is enforced where a text is read.Checks
mise exec -- pnpm run gate) green on the rebased head.scripts/sabotage.mjsand caught on an assertion in the new test file: keeping the trailing zeros; answering the integer part for an inexact fraction; adding every cast amount to the written unit; naming every grammar amount by the written unit; stripping the zeros with an end-anchored pattern. Turning off the factor cancellation in the shared expander also turns the existing parser and cast tests red.Review tier: gate (an internal refactor; the in-turn review round ran before this request).