You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ENHANCEMENT] add ability to clone a semver instance #378
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.
When the version is a string, it works as you expect it to @madhead, @pigulla.
{// works as you expect:constversion='1.2.3';console.log(newSemVer(version).inc('major'))console.log(newSemVer(version).inc('minor'))console.log(newSemVer(version).inc('patch'))}{// When you already have a SemVer object and want to copy it, pass "version"constsemVersion=newSemVer('1.2.3');console.log(newSemVer(semVersion.version).inc('major'))console.log(newSemVer(semVersion.version).inc('minor'))console.log(newSemVer(semVersion.version).inc('patch'))}
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.
I think new SemVer(instance) cloning the existing instance would be a breaking change, that I would rather not make. A static method on the class would make more sense, but I'm still hesitant to add api surface area. A PR or new issue with this feature proposal could get more input as to whether it should be added or not.
fwiw here's what I was thinking previously for a wrapper function:
const clone = (v) => new SemVer(v.version ?? v, v.options)
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)
It does work to do new Semver(instance) it just returns instance (if the options match).
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.
Reopening this and labeling as semver:major so we don't forget to figure out a proper API for this in the future. It might not end up being a breaking change, but grouping it with the rest of the semver:major issues will help.
Activity
pigulla commentedon May 20, 2021
I'd also be interested in this. Did you find a solution?
madhead commentedon May 20, 2021
Unfortunately, no, I had to store the components separately and use them to construct new instances.
rauno56 commentedon Jan 15, 2022
When the version is a string, it works as you expect it to @madhead, @pigulla.
madhead commentedon Apr 5, 2022
No fix, just closing?
lukekarrys commentedon Apr 5, 2022
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.
ljharb commentedon Apr 5, 2022
@lukekarrys it kind of seems like this then is a feature request to add that? something like
new Semver(instance)orSemver.clone(instance)lukekarrys commentedon Apr 5, 2022
I think
new SemVer(instance)cloning the existing instance would be a breaking change, that I would rather not make. A static method on the class would make more sense, but I'm still hesitant to add api surface area. A PR or new issue with this feature proposal could get more input as to whether it should be added or not.fwiw here's what I was thinking previously for a wrapper function:
ljharb commentedon Apr 5, 2022
i'd expect
v => v instanceof Semver ? new Semver(v.version, v.options) : new Semver(v).i'm not sure why it'd be a breaking change; do you think anyone's relying on
new Semver(instance)if it doesn't work right now?lukekarrys commentedon Apr 6, 2022
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)
node-semver/classes/semver.js
Lines 11 to 17 in e2d55e7
It does work to do
new Semver(instance)it just returnsinstance(if the options match).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.
ljharb commentedon Apr 6, 2022
Fair point.
[-][QUESTION] How do I clone a SemVer?[/-][+][ENHANCEMENT] add ability to clone a semver instance[/+]lukekarrys commentedon Oct 27, 2022
Reopening this and labeling as
semver:majorso we don't forget to figure out a proper API for this in the future. It might not end up being a breaking change, but grouping it with the rest of thesemver:majorissues will help.