fix(duration): apply the leading sign when parsing a negative ISO duration#3147
Open
spokodev wants to merge 1 commit into
Open
fix(duration): apply the leading sign when parsing a negative ISO duration#3147spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
…ation The ISO-8601 duration parser called d.slice(2) and never read the sign captured in d[1], so a leading-minus string parsed as positive. toISOString() correctly emits -P..., so the round-trip duration(d.toISOString()) flipped the sign. Derive a sign multiplier from d[1] === '-' and apply it to each parsed component. Scope is the leading-sign case only; per-component signs are a separate serializer limitation. Refs iamkun#1516
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.
Problem
Parsing a negative ISO-8601 duration string drops the leading sign:
The parser does
d.slice(2)over the regex match and never reads the sign captured ind[1]. BecausetoISOString()correctly emits-P..., the round-trip invariant breaks:Refs #1516.
Fix
Derive a sign multiplier from
d[1] === '-'and apply it to each parsed component, so the serialized output parses back to the original value.Scope
Leading-sign case only. Per-component signs (e.g.
P-6M3D) are a separate serializer limitation and are out of scope here.Tests
Added a round-trip test covering
asYears,asHours,toISOString, and theduration(d.toISOString()) === dinvariant. Red before the fix, green after; duration suite 31/31, duration + relativeTime 40/40.