GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: Add `truncate` function by pjohnmeyer · Pull Request #855 · npm/node-semver

Conversation

@pjohnmeyer

@pjohnmeyer pjohnmeyer commented May 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This PR adds a truncate function to allow for portions of a version string "below" a certain level to be dropped.

The truncate function, as implemented in this first pass at a PR, returns a new valid version string. The function:

  1. Always removes build information
  2. Removes pre-release information, unless a pre-release type is passed as the truncation
  3. Zeroes out "lower" components of the semantic version (e.g. if the truncation performed is major, both minor and patch are zeroed)

An alternative I considered was returning strings without the valid version guarantee; for example, truncate('1.2.3', 'minor') === '1.2') instead of 1.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 for major, minor, and patch. For truncation = 'pre*' however, this did not feel right, as truncate('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 a 1.2.3-rc0 that is closer to the "floor", the name didn't seem right for that use case.

Lastly, I considered disallowing the pre* versions for the truncation argument, and only allowing major, minor, and patch. This, paired with the floor naming, was the most attractive alternative to me.

References

Related to #48

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
pjohnmeyer added 6 commits April 30, 2026 20:30
@pjohnmeyer
Create "truncate" function that handles patch, minor, and major
3106982
@pjohnmeyer
Use parse instead of new SemVer in truncate
6463847
@pjohnmeyer
Remove build info for pre* truncation
0ac2c71
@pjohnmeyer
Improve variable and function names
7058c78
@pjohnmeyer
Eliminate unused vars in truncate tests
4cc9cd5
@pjohnmeyer
Add truncate function to index, README
f76d67d
@pjohnmeyer
pjohnmeyer requested a review from a team as a code owner May 3, 2026 00:37
@pjohnmeyer

pjohnmeyer commented May 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

FWIW: I don't have a stake in whether or not truncate is added to the library. I don't know if it's truly needed. I just saw an old issue and decided to work on it for fun. This also was useful for me to prepare to work on the dec function from #48, which I do actually see more of a use case for.

Also, I will note that template-oss-check failed and wanted me to update a bunch of .github files, but I did not include those per the CONTRIBUTING guidelines.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pjohnmeyer pjohnmeyer mentioned this pull request May 3, 2026
[FEATURE] decrement/truncate #48
Open
mbtools
mbtools reviewed May 4, 2026
View reviewed changes

@mbtools mbtools left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add some cases with prerelease and build to the fixtures, but LGTM 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
owlstronaut
owlstronaut suggested changes May 4, 2026
View reviewed changes

@owlstronaut owlstronaut left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread README.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread functions/truncate.js Outdated
return null
}

const parsed = parse(version, options)

@owlstronaut owlstronaut May 4, 2026

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this returns the same instance of the SemVer class so you'll be mutating the caller's object.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pjohnmeyer pjohnmeyer May 5, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I saw how inc does it and didn't understand why it didn't use parse -- now it makes sense.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pjohnmeyer Michael Smith
Apply suggestion from @owlstronaut
40d5827
Co-authored-by: Michael Smith <[email protected]>
@pjohnmeyer

pjohnmeyer commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

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.

@owlstronaut yeah I agree. I covered that functionality with the valid-versions fixture, but that's a less obvious expression of intent.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pjohnmeyer added 2 commits May 6, 2026 20:23
@pjohnmeyer
Ensure SemVer object inputs are not mutated
924e60b
@pjohnmeyer
Add more prerelease+build test fixtures for truncate
d8bad09
@pjohnmeyer

pjohnmeyer commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

@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 Object.freeze in the tests as an assurance.

mbtools reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

owlstronaut
owlstronaut approved these changes May 7, 2026
View reviewed changes

@owlstronaut owlstronaut left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent, thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@owlstronaut
owlstronaut merged commit 0d0a0a2 into npm:main May 7, 2026
25 of 29 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@pjohnmeyer
pjohnmeyer deleted the feat/add-truncate-function branch May 8, 2026 17:28
@github-actions github-actions Bot mentioned this pull request Mar 20, 2026
chore: release 7.8.0 #847
Merged
owlstronaut pushed a commit that referenced this pull request May 8, 2026
@github-actions
chore: release 7.8.0 (#847)
efa4be6
🤖 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)
`[email protected]` (@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 was referenced May 9, 2026
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#75
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#79
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#83
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#87
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#91
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#95
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#100
Closed
This was referenced May 16, 2026
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#106
Closed
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#112
Closed
This was referenced May 21, 2026
deps: semver 7.7.4 → 7.8.0 suj1e/larkcc#137
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#143
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#149
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#155
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#161
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#167
Open
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#174
Closed
This was referenced May 28, 2026
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#180
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#186
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#193
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#200
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#208
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#216
Closed
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#224
Closed
This was referenced Jun 4, 2026
deps: semver 7.7.4 → 7.8.1 suj1e/larkcc#232
Closed
deps: semver 7.7.4 → 7.8.2 suj1e/larkcc#240
Closed
deps: semver 7.7.4 → 7.8.2 suj1e/larkcc#248
Closed
deps: semver 7.7.4 → 7.8.2 suj1e/larkcc#256
Closed
deps: semver 7.7.4 → 7.8.2 suj1e/larkcc#264
Closed
deps: semver 7.7.4 → 7.8.3 suj1e/larkcc#273
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#282
Closed
This was referenced Jun 11, 2026
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#291
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#300
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#309
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#318
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#327
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#336
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#345
Closed
This was referenced Jun 18, 2026
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#354
Closed
deps: semver 7.7.4 → 7.8.4 suj1e/larkcc#363
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

2 more reviewers
@mbtools mbtools mbtools left review comments
@owlstronaut owlstronaut owlstronaut approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@pjohnmeyer @owlstronaut @mbtools
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.