A builder-style switch / pattern-matching library for Lua with middleware, events, opt-in memoize, and structural dispatch. Built on top of matchigo-lua (vendored in the bundle) for pattern-test logic and the optional Rust-style DSL.
Works on standard Lua 5.1+, LuaJIT, FiveM, Roblox (Luau), and LÖVE2D.
📖 Full documentation in docs/ — installation, API reference, guides, examples, v1→v2 migration.
Warning
Download the named release asset, not "Source code (zip)" — the source archive does not include the bundled dist (dist/ is gitignored).
Grab EasySwitchLua-vX.Y.Z.zip from the Releases page, extract easyswitch.lua, then :
local EasySwitch = require("easyswitch")FiveM / Roblox / LÖVE2D specifics — see docs/installation.md.
local EasySwitch = require("easyswitch")
local P = EasySwitch.P
local router = EasySwitch.new()
:when("GET", function() return "list" end)
:when({"POST", "PUT"}, function(m) return "write:" .. m end)
:when(P.union("DELETE", "PATCH"), function(m) return "modify:" .. m end)
:when(P.string, function(v) return "str:" .. v end)
:default(function(v) return "unhandled: " .. tostring(v) end)
print(router:execute("GET")) -- list
print(router:execute("POST")) -- write:POST
print(router:execute("hello")) -- str:hello
print(router:execute(42)) -- unhandled: 42→ Getting started for a 5-minute walkthrough, API reference for the full surface.
- Pattern matching — type sentinels, unions, intersections, shapes, ranges, predicates
- DSL strings — Rust-style match arms inside
:when(), parsed once at construction - Middleware — chain transforms before dispatch with
:use(fn) - Events — 7 hooks (
beforeExecute,afterExecute,noMatch,error, ...) - Memoize — opt-in result caching with optional verify mode
- Fallthrough —
EasySwitch.FALLTHROUGHsentinel for layered rules - Named registry —
EasySwitch("name")for cross-module dispatchers
Coming from EasySwitchLua v1 (cache + actionManager era) ? See Migration v1 → v2.