compare-versions icon indicating copy to clipboard operation
compare-versions copied to clipboard

Wrong result by comparing versions with leading zero for patch.

Open tr3v3r opened this issue 1 year ago • 1 comments

Hello!

First of all thanks for such a great library!

I've revealed the problem with comparing versions with leading zero for the patch. Example:

compare('2.0.0123123', '2.0.13', '<=')

Expected: true Actual: false

As I understood the patches are compared with the following logic.

compare('2.0.0123123', '2.0.13', '<=') 

// first operand patch: "123123"
// second operand patch: "13"

Number("0123123") <= Number("13")  // false

Is it considered as expected behavior? Is there any possibility to compare strings instead?

Thanks

tr3v3r avatar Nov 01 '23 11:11 tr3v3r

You are correct that leading zero is treated like Number('0123123') as per https://semver.org/#spec-item-2

In your second example Number("0123123") <= Number("13"), false is indeed the expected behavior, so in your first example compare should return false because 123123 is larger than 13.

omichelsen avatar Dec 09 '23 21:12 omichelsen