Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 2.47 KB

File metadata and controls

61 lines (43 loc) · 2.47 KB

EasySwitchLua — Documentation

A builder-style switch / pattern-matching library for Lua. Thin wrapper around matchigo-lua with events, middleware, opt-in memoize, and a named registry.

← Back to repository root


Start here

API reference

  • ModuleEasySwitch.new(), registry, P, FALLTHROUGH, Map / Set / BigInt
  • Switch instance — Every method on the switch (:when, :execute, :memoize, ...)
  • Events — All seven events with their payloads
  • Patterns — Curated P.* reference

Guides

Examples


Quick reference

local EasySwitch = require("easyswitch")
local P = EasySwitch.P

local sw = EasySwitch.new()
    :when("GET",        function() return "list"   end)        -- literal
    :when({"POST", "PUT"}, function(m) return "write:" .. m end)  -- array of literals
    :when(P.string,     function(v) return "str:" .. v end)    -- pattern
    :when(P.number, function(n) return n > 0 end,
                    function() return "positive number" end)    -- guard
    :when("'DELETE' | 'PATCH'", function(m) return "modify" end)  -- DSL string
    :default(function() return "fallback" end)
    :on("noMatch", function(v) print("unhandled:", v) end)
    :memoize()                                                  -- opt-in cache

print(sw:execute("GET"))

License

MIT