Semver
An idiomatic Luau API wrapper for the Rust-style API found in cargo-semver
.
Functions
cmp
Semver.
cmp
(
lhs:
string
,
op:
"
<
"
|
"
<
="
|
"=="
|
"
>
"
|
"
>
="
|
"~="
,
rhs:
string
) →
boolean
Compare the major, minor, patch, Prerelease and BuildMetadata value of two Versions.
Usage:
-- Sort versions array
local versions = { "3.1.2", "1.0.0", "1.0.0-rc.1", "1.0.0-rc.2", "0.3.0-alpha", "0.3.0-beta" }
table.sort(versions, function(a, b)
return Semver.cmp(a, "<", b)
end)
cmpPrecedence
Semver.
cmpPrecedence
(
lhs:
string
,
op:
"
<
"
|
"
<
="
|
"=="
|
"
>
"
|
"
>
="
|
"~="
,
rhs:
string
) →
boolean
Compare the major, minor, patch and Prerelease value of two Versions, disregarding BuildMetadata. Versions that differ only in BuildMetadata are considered equal. This comparison is what the SemVer spec refers to as "precedence".
Usage:
assert(Semver.cmpPrecedence(newVersion, ">=", oldVersion), "newVersion must be >= oldVersion")
getMaxMatching
Semver.
getMaxMatching
(
versions:
{
string
}
,
versionReq:
string
) →
string?
Usage:
local versions = { "3.1.2", "0.3.0", "1.0.0", "1.6.0" }
print(Semver.getMaxMatching(versions, "^1.0.0")) --> 1.6.0
getMinMatching
Semver.
getMinMatching
(
versions:
{
string
}
,
versionReq:
string
) →
string?
Usage:
local versions = { "3.1.2", "0.3.0", "1.0.0", "1.6.0" }
print(Semver.getMinMatching(versions, "^1.0.0")) --> 1.0.0
matches
Semver.
matches
(
version:
string
,
versionReq:
string
) →
boolean
Usage:
assert(Semver.matches(versionStr, "^1.0.0"), "version does not match requirement")
setMemoizationFunc
Semver.
setMemoizationFunc
(
objToMemoize:
"Version"
|
"VersionReq"
,
memoizationFunc:
<
T
>
(
(
string
)
→
T
)
→
(
string
)
→
T
) →
(
)
Registers a memoization function for Version.parse or VersionReq.parse, this can prevent repeated parsing of semver strings and greatly improve the performance of the Semver library.
💡 Tip: Memoization functions can be found in the luau-caching-and-memoization repository.
Usage:
Semver.setMemoizationFunc("Version", memoizeFrame)
Semver.setMemoizationFunc("VersionReq", function(parseFunc)
return memoizeRecentlyUsed(50, parseFunc)
end)
validateVersion
Semver.
validateVersion
(
version:
string
) →
(
boolean
,
string?
)
Usage:
assert(Semver.validateVersion("1.0"))
--> Error: unexpected end of input while parsing minor version number
validateVersionReq
Semver.
validateVersionReq
(
versionReq:
string
) →
(
boolean
,
string?
)
Usage:
assert(Semver.validateVersionReq("1.0.a"))
--> Error: unexpected character 'a' while parsing patch version number