fix(core): sum repeated units in parseNaturalLanguageDurationInMs - #4373
fix(core): sum repeated units in parseNaturalLanguageDurationInMs#4373eeshsaxena wants to merge 1 commit into
Conversation
The valid-input pattern is `/^(\d+(?:w|d|hr|h|m|s))+$/`, which accepts a unit appearing more than once (e.g. "1h2h"). But each unit was read with a single `.match()`, so only the first occurrence of each was counted and the rest were silently dropped - "1h2h" returned 1h. Tokenize the whole string and sum every match instead. All existing single-occurrence and multi-unit behaviour is unchanged (verified against the existing suite); repeated units now add up. This also collapses five near-identical unit blocks into one loop. Adds tests for the repeated-unit case.
🦋 Changeset detectedLatest commit: 29cfa42 The changes in this PR will be included in the next version bump. This PR includes changesets to release 26 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
WalkthroughUpdated ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hi @eeshsaxena, thanks for your interest in contributing! This project requires that pull request authors are vouched, and you are not in the list of vouched users. This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details. |
There was a problem hiding this comment.
🔍 Identical repeated-unit bug remains in parseNaturalLanguageDurationAgo
This PR fixes the repeated-unit summing bug only in parseNaturalLanguageDurationInMs. The sibling function parseNaturalLanguageDurationAgo (packages/core/src/v3/isomorphic/duration.ts:91-128) still uses the old single-.match() approach for each unit, so it retains the exact same bug: a validated input like "1h2h" will drop all but the first occurrence of each repeated unit. Since this function was not touched by the diff (context-only lines, no +/-), it cannot be reported as a diff bug, but the reviewer may want to apply the same fix there for consistency.
(Refers to lines 91-128)
Was this helpful? React with 👍 or 👎 to provide feedback.
parseNaturalLanguageDurationInMssilently drops repeated units.Problem
The valid-input check is
/^(\d+(?:w|d|hr|h|m|s))+$/, whose+accepts a unit appearing more than once. But each unit was then read with a single.match(), so only the first occurrence counted:So the validation and the parsing disagree: a string the function accepts as valid is parsed to the wrong number.
Fix
Tokenize the whole string and sum every match.
hris matched beforehso"1hr"stays one hour. All existing single-occurrence and combined-unit behaviour is unchanged; repeated units now add up. As a side effect this replaces five near-identical unit blocks with one loop.If you would rather reject duplicates than sum them, that is a one-line change to the pattern instead - happy to switch. Summing seemed the least surprising (it is how
"1h30m"already combines units).Tests
The full
duration.test.tssuite passes (56 tests). Added cases for the repeated-unit sum; I confirmed the new "sums a unit that appears more than once" case fails against the current code and passes with the fix. Included a changeset (@trigger.dev/corepatch).