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 |
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