Returns a negative number if a is a lower version than b, 0 if they are equal, and a positive number otherwise.
assertEquals(0, compareSemVer("1.0.0", "1.0.0")); assertEquals(0, compareSemVer("1.0.0+b1", "1.0.0+b2")); assertLessThan(compareSemVer("1.0.0", "2.0.0"), 0); assertLessThan(compareSemVer("1.0.0-beta", "1.0.0"), 0); assertGreaterThan(compareSemVer("1.0.1", "1.0.0"), 0); void assertLess(string a, string b) { assertLessThan(compareSemVer(a, b), 0); assertGreaterThan(compareSemVer(b, a), 0); assertEquals(0, compareSemVer(a, a)); assertEquals(0, compareSemVer(b, b)); } assertLess("1.0.0", "2.0.0"); assertLess("2.0.0", "2.1.0"); assertLess("2.1.0", "2.1.1"); assertLess("1.0.0-alpha", "1.0.0"); assertLess("1.0.0-alpha", "1.0.0-alpha.1"); assertLess("1.0.0-alpha.1", "1.0.0-alpha.beta"); assertLess("1.0.0-alpha.beta", "1.0.0-beta"); assertLess("1.0.0-beta", "1.0.0-beta.2"); assertLess("1.0.0-beta.2", "1.0.0-beta.11"); assertLess("1.0.0-beta.11", "1.0.0-rc.1"); assertLess("1.0.0-rc.1", "1.0.0"); assertEquals(0, compareSemVer("1.0.0", "1.0.0+1.2.3")); assertEquals(0, compareSemVer("1.0.0", "1.0.0+1.2.3-2")); assertEquals(0, compareSemVer("1.0.0+asdasd", "1.0.0+1.2.3")); assertLess("2.0.0", "10.0.0"); assertLess("1.0.0-2", "1.0.0-10"); assertLess("1.0.0-99", "1.0.0-1a"); assertLess("1.0.0-99", "1.0.0-a"); assertLess("1.0.0-alpha", "1.0.0-alphb"); assertLess("1.0.0-alphz", "1.0.0-alphz0"); assertLess("1.0.0-alphZ", "1.0.0-alpha");
Compares the precedence of two SemVer version strings.
The version strings must be validated using isSemVer before being passed to this function. Note that the build meta data suffix (if any) is being ignored when comparing version numbers.