-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ENHANCEMENT] add ability to clone a semver instance #378
Comments
I'd also be interested in this. Did you find a solution? |
Unfortunately, no, I had to store the components separately and use them to construct new instances. |
When the version is a string, it works as you expect it to @madhead, @pigulla. {
// works as you expect:
const version = '1.2.3';
console.log(new SemVer(version).inc('major'))
console.log(new SemVer(version).inc('minor'))
console.log(new SemVer(version).inc('patch'))
}
{
// When you already have a SemVer object and want to copy it, pass "version"
const semVersion = new SemVer('1.2.3');
console.log(new SemVer(semVersion.version).inc('major'))
console.log(new SemVer(semVersion.version).inc('minor'))
console.log(new SemVer(semVersion.version).inc('patch'))
} |
No fix, just closing? |
The previous comment is the best answer to the question at this time, since there is no way to fully clone a semver instance (with options). I would recommend making a wrapper function that takes a version and options and always returns a new instance. We don't have any plans at this time to add a new method to clone an instance. |
@lukekarrys it kind of seems like this then is a feature request to add that? something like |
I think fwiw here's what I was thinking previously for a wrapper function:
|
i'd expect i'm not sure why it'd be a breaking change; do you think anyone's relying on |
I'm erring on the side of caution, and not knowing the history of the decision. It appears to be an explicit design decision to reuse an instance (and I traced this back to at least v2.0.0) Lines 11 to 17 in e2d55e7
It does work to do I can imagine situations for a highly used library like this, in which this would be a breaking change. Even the fact that this would break strict equality checks, makes me lean towards breaking change. const s = new SemVer('1.0.0')
s === new SemVer(s) // true |
Fair point. |
Reopening this and labeling as |
What / Why
I'm trying to compute a bunch of various increments for a specific version (TypeScript):
The problem is the
SemVer
class itself is mutable, so each increment is computed against previous increment result. This is not what I need.So, I need a way to clone this object, like.
But the constructor of the
SemVer
(new SemVer(version)
) doesn't create a new instance when passed another instance with the same options. So, again, the code doesn't work.Is there a way to clone a SemVer?
The text was updated successfully, but these errors were encountered: