Skip to content

Commit 513507b

Browse files
committed
Fix serious compare bug; Fixes #42
1 parent a2db2af commit 513507b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/utils/semver.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ Deno.test("semver", async test => {
258258
// const c = semver.intersect(a, b)
259259
// assertEquals(c.toString(), "^11.3,^12.2")
260260
// })
261+
262+
/* https://github.com/pkgxdev/libpkgx/issues/42 */
263+
await test.step(">=1<1.0.19", async test => {
264+
await test.step("1", () => { new semver.Range(">=1<1.0.19") })
265+
await test.step("2", () => { new semver.Range(">=1.0<1.0.19") })
266+
await test.step("3", () => {
267+
assertEquals(new semver.Range(">=1<2").toString(), "^1")
268+
})
269+
270+
assert(new SemVer("1").lt(new SemVer("1.0.19")), "1.0.0 is less than 1.0.19")
271+
})
261272
})
262273
})
263274

src/utils/semver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,10 @@ function zip<T, U>(a: T[], b: U[]) {
330330

331331

332332
function _compare(a: SemVer, b: SemVer): number {
333-
for (const [c,d] of zip(cmpcomponents(a), cmpcomponents(b))) {
334-
if (c != d) return (c ?? 0) - (d ?? 0)
333+
for (let [c,d] of zip(cmpcomponents(a), cmpcomponents(b))) {
334+
c ??= 0
335+
d ??= 0
336+
if (c !== d) return c - d
335337
}
336338
return 0
337339

0 commit comments

Comments
 (0)