Skip to main content

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
Show raw api
{
    "functions": [
        {
            "name": "cmp",
            "desc": "Compare the major, minor, patch, [Prerelease] and [BuildMetadata] value of two [Versions](Version).\n\n**Usage:**\n```lua\n-- Sort versions array\nlocal 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\" }\ntable.sort(versions, function(a, b)\n\treturn Semver.cmp(a, \"<\", b)\nend)\n```",
            "params": [
                {
                    "name": "lhs",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "\"<\" | \"<=\" | \"==\" | \">\" | \">=\" | \"~=\""
                },
                {
                    "name": "rhs",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 41,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "cmpPrecedence",
            "desc": "Compare the major, minor, patch and [Prerelease] value of two [Versions](Version),\ndisregarding [BuildMetadata]. [Versions](Version) that differ only in [BuildMetadata]\nare considered equal. This comparison is what the SemVer spec refers to\nas \"precedence\".\n\n**Usage:**\n```lua\nassert(Semver.cmpPrecedence(newVersion, \">=\", oldVersion), \"newVersion must be >= oldVersion\")\n```",
            "params": [
                {
                    "name": "lhs",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "op",
                    "desc": "",
                    "lua_type": "\"<\" | \"<=\" | \"==\" | \">\" | \">=\" | \"~=\""
                },
                {
                    "name": "rhs",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 72,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "getMaxMatching",
            "desc": "**Usage:**\n```lua\nlocal versions = { \"3.1.2\", \"0.3.0\", \"1.0.0\", \"1.6.0\" }\nprint(Semver.getMaxMatching(versions, \"^1.0.0\")) --> 1.6.0\n```",
            "params": [
                {
                    "name": "versions",
                    "desc": "",
                    "lua_type": "{ string }"
                },
                {
                    "name": "versionReq",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string?\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 100,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "getMinMatching",
            "desc": "**Usage:**\n```lua\nlocal versions = { \"3.1.2\", \"0.3.0\", \"1.0.0\", \"1.6.0\" }\nprint(Semver.getMinMatching(versions, \"^1.0.0\")) --> 1.0.0\n```",
            "params": [
                {
                    "name": "versions",
                    "desc": "",
                    "lua_type": "{ string }"
                },
                {
                    "name": "versionReq",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "string?\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 121,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "matches",
            "desc": "**Usage:**\n```lua\nassert(Semver.matches(versionStr, \"^1.0.0\"), \"version does not match requirement\")\n```",
            "params": [
                {
                    "name": "version",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "versionReq",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 141,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "setMemoizationFunc",
            "desc": "Registers a memoization function for [Version.parse] or [VersionReq.parse],\nthis can prevent repeated parsing of semver strings and greatly improve the performance of the [Semver] library.\n\n**💡 Tip:**\nMemoization functions can be found in the [luau-caching-and-memoization](https://github.com/tim7775/luau-caching-and-memoization) repository.\n\n**Usage:**\n```lua\nSemver.setMemoizationFunc(\"Version\", memoizeFrame)\nSemver.setMemoizationFunc(\"VersionReq\", function(parseFunc)\n\treturn memoizeRecentlyUsed(50, parseFunc)\nend)\n```",
            "params": [
                {
                    "name": "objToMemoize",
                    "desc": "",
                    "lua_type": "\"Version\" | \"VersionReq\""
                },
                {
                    "name": "memoizationFunc",
                    "desc": "",
                    "lua_type": "<T>((string) -> T)->(string) -> T"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 164,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "validateVersion",
            "desc": "**Usage:**\n```lua\nassert(Semver.validateVersion(\"1.0\"))\n--> Error: unexpected end of input while parsing minor version number\n```",
            "params": [
                {
                    "name": "version",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                },
                {
                    "desc": "",
                    "lua_type": "string?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 179,
                "path": "lib/Semver/init.luau"
            }
        },
        {
            "name": "validateVersionReq",
            "desc": "**Usage:**\n```lua\nassert(Semver.validateVersionReq(\"1.0.a\"))\n--> Error: unexpected character 'a' while parsing patch version number\n```",
            "params": [
                {
                    "name": "versionReq",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                },
                {
                    "desc": "",
                    "lua_type": "string?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 191,
                "path": "lib/Semver/init.luau"
            }
        }
    ],
    "properties": [],
    "types": [],
    "name": "Semver",
    "desc": "An idiomatic Luau API wrapper for the Rust-style API found in `cargo-semver`.",
    "source": {
        "line": 27,
        "path": "lib/Semver/init.luau"
    }
}