feat: Add truncate function#855
Conversation
|
FWIW: I don't have a stake in whether or not Also, I will note that |
mbtools
left a comment
There was a problem hiding this comment.
You could add some cases with prerelease and build to the fixtures, but LGTM 👍
owlstronaut
left a comment
There was a problem hiding this comment.
This is great. I like the idea from @mbtools to add some prerelease+build fixtures. I'd like to see a test that verifies/catches the mutation i think happens.
orig = new SemVer('1.2.3-alpha+build')
snapshot = { orig stuff }
truncate(orig, 'major')
org === snapshot
truncate(orig, 'patch') // second call should start from the orig| return null | ||
| } | ||
|
|
||
| const parsed = parse(version, options) |
There was a problem hiding this comment.
i think this returns the same instance of the SemVer class so you'll be mutating the caller's object.
There was a problem hiding this comment.
Good point! I saw how inc does it and didn't understand why it didn't use parse -- now it makes sense.
Co-authored-by: Michael Smith <owlstronaut@github.com>
@owlstronaut yeah I agree. I covered that functionality with the |
|
@mbtools I added some fixtures to address your concern. @owlstronaut I fixed the inadvertent mutation of input. Rather than add an explicit test for this, I used |
owlstronaut
left a comment
There was a problem hiding this comment.
This is excellent, thank you!
🤖 I have created a release *beep* *boop* --- ## [7.8.0](v7.7.4...v7.8.0) (2026-05-08) ### Features * [`0d0a0a2`](0d0a0a2) [#855](#855) Add `truncate` function (#855) (@pjohnmeyer, @owlstronaut) ### Bug Fixes * [`3905343`](3905343) [#859](#859) Warn when defaulting to --inc=patch in CLI (@pjohnmeyer) ### Documentation * [`c368af6`](c368af6) [#853](#853) fix typos in documentation (#853) (@ankitkumar572005) * [`37776c3`](37776c3) [#846](#846) fix BNF grammar to distinguish prerelease from build identifiers (#846) (@abhu85, @claude) ### Chores * [`9542e09`](9542e09) [#860](#860) template-oss-apply (@owlstronaut) * [`937bc2c`](937bc2c) [#860](#860) `template-oss-apply@5.0.0` (@owlstronaut) * [`6946fef`](6946fef) [#852](#852) bump @npmcli/template-oss from 4.29.0 to 4.30.0 (#852) (@dependabot[bot], @npm-cli-bot) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This PR adds a
truncatefunction to allow for portions of a version string "below" a certain level to be dropped.The
truncatefunction, as implemented in this first pass at a PR, returns a new valid version string. The function:buildinformationtruncationtruncationperformed ismajor, bothminorandpatchare zeroed)An alternative I considered was returning strings without the valid version guarantee; for example,
truncate('1.2.3', 'minor') === '1.2')instead of1.2.0. This is not what was specified by @isaacs in #48, however, and seemed to be out of place for the rest of the library.As a result, I also considered naming the function differently. One option I considered was
floor, which seemed conceptually correct formajor,minor, andpatch. Fortruncation = 'pre*'however, this did not feel right, astruncate('1.2.3-rc1+build', 'prerelease') === '1.2.3-rc1', but since a) build metadata "...MUST be ignored when determining version precedence...", and b) there could be a1.2.3-rc0that is closer to the "floor", the name didn't seem right for that use case.Lastly, I considered disallowing the
pre*versions for thetruncationargument, and only allowingmajor,minor, andpatch. This, paired with thefloornaming, was the most attractive alternative to me.References
Related to #48