App patterns · real, tested code

Compare Semantic Versions in Larzscript

Parse major.minor.patch versions, compare them correctly (1.2.0 < 1.10.0, not a naive string compare), and check a version against a real range constraint.

semver.satisfies() understands >=/<=/>/</^/~ ranges and x-ranges, and max_satisfying() picks the highest matching version from a list - the same constraint logic a package manager needs.

larzscript pkg install semver

Code

import "semver" as semver

print(semver.compare("1.2.0", "1.10.0"))
print(semver.gt("2.0.0", "1.9.9"))
print(semver.satisfies("1.5.0", "^1.0.0"))
print(semver.max_satisfying(["1.0.0", "1.5.0", "2.0.0"], "^1.0.0"))
Output
-1
true
true
1.5.0