{"id":18187291,"url":"https://github.com/kamionn/easyswitchlua","last_synced_at":"2026-01-28T01:31:36.891Z","repository":{"id":260813017,"uuid":"882034634","full_name":"Kamionn/EasySwitchLua","owner":"Kamionn","description":"EasySwitchLua is a lightweight library for managing control structures in Lua. Its intuitive interface makes it easy to define actions for multiple values and add a default action for unsupported cases. Ideal for menus, it simplifies conditional logic and enhances code readability.","archived":false,"fork":false,"pushed_at":"2024-11-03T10:42:12.000Z","size":43,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T21:52:10.176Z","etag":null,"topics":["easyswitch","easyswitchlua","lua","switch"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kamionn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T18:36:36.000Z","updated_at":"2024-11-03T11:20:18.000Z","dependencies_parsed_at":"2024-11-02T19:17:36.505Z","dependency_job_id":"83c92f55-dc21-4b12-bb2b-5f8b6413ad51","html_url":"https://github.com/Kamionn/EasySwitchLua","commit_stats":null,"previous_names":["kamionn/easyswitchlua"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamionn%2FEasySwitchLua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamionn%2FEasySwitchLua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamionn%2FEasySwitchLua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamionn%2FEasySwitchLua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kamionn","download_url":"https://codeload.github.com/Kamionn/EasySwitchLua/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119402,"owners_count":21050754,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["easyswitch","easyswitchlua","lua","switch"],"created_at":"2024-11-03T01:03:11.754Z","updated_at":"2026-01-28T01:31:36.849Z","avatar_url":"https://github.com/Kamionn.png","language":"Lua","readme":"# EasySwitchLua\n\nAn advanced and performant switch system for Lua with event handling, middleware, and optimized caching.\n\n## ⚡ Installation\n\n```lua\n-- Copy files into your project\nlocal Switch = require(\"easyswitch\")\n```\n\n## 🚀 Basic Usage\n\n```lua\nlocal Switch = require(\"easyswitch\")\n\n-- Creating a basic switch\nlocal menuSwitch = Switch(\"menu\")\n    :when(\"start\", function()\n        return \"Game started\"\n    end)\n    :when(\"quit\", function()\n        return \"Game ended\"\n    end)\n    :default(function(action)\n        return \"Unknown action: \" .. action\n    end)\n\n-- Usage\nprint(menuSwitch:execute(\"start\"))  -- \"Game started\"\nprint(menuSwitch:execute(\"quit\"))   -- \"Game ended\"\nprint(menuSwitch:execute(\"other\"))  -- \"Unknown action: other\"\n\n-- Multiple cases\nlocal commandSwitch = Switch(\"commands\")\n    :when({\"save\", \"backup\"}, function(action)\n        return \"Saving game...\"\n    end)\n\nprint(commandSwitch:execute(\"save\"))    -- \"Saving game...\"\nprint(commandSwitch:execute(\"backup\"))  -- \"Saving game...\"\n```\n\n## 🔥 Advanced Features\n\n### Middleware\n```lua\nlocal authSwitch = Switch(\"auth\")\n    -- Input transformation\n    :use(function(action)\n        return string.upper(action)\n    end)\n    :when(\"LOGIN\", function()\n        return \"Logging in...\"\n    end)\n\nprint(authSwitch:execute(\"login\"))  -- \"Logging in...\"\n```\n\n### Before Execution Checks\n```lua\nlocal secureSwitch = Switch(\"secure\")\n    :before(function(action)\n        local allowedActions = { start = true, stop = true }\n        return allowedActions[action] ~= nil\n    end)\n    :when(\"start\", function()\n        return \"Starting secure process...\"\n    end)\n    :when(\"stop\", function()\n        return \"Stopping secure process...\"\n    end)\n\nprint(secureSwitch:execute(\"start\")) -- \"Starting secure process...\"\nprint(secureSwitch:execute(\"invalid\")) -- nil, before check fails\n```\n\n\n### Events (Debug/Logging)\n```lua\nlocal debugSwitch = Switch(\"debug\")\n    -- Before execution\n    :on(\"beforeExecute\", function(value)\n        print(\"Executing:\", value)\n    end)\n    -- After execution\n    :on(\"afterExecute\", function(value, result)\n        print(\"Result:\", result)\n    end)\n    -- On error\n    :on(\"error\", function(type, err)\n        print(\"Error in\", type .. \":\", err)\n    end)\n    :when(\"test\", function()\n        return \"test ok\"\n    end)\n\ndebugSwitch:execute(\"test\")\n```\n\n### Automatic Caching\nThe system automatically caches results with intelligent memory management using pairs.\n\n```lua\nlocal expensiveSwitch = Switch(\"expensive\")\n    :when(\"calc\", function()\n        -- Expensive operation\n        local result = 0\n        for i = 1, 1000000 do\n            result = result + i\n        end\n        return result\n    end)\n\n-- First call: calculates\nprint(expensiveSwitch:execute(\"calc\"))\n-- Second call: uses cache\nprint(expensiveSwitch:execute(\"calc\"))\n```\n\n## ⚙️ Configuration\n\n```lua\nlocal switch = Switch(\"config\", {\n    maxCases = 1000  -- Case limit (default: 100)\n})\n```\n\n## 📋 Available Events\n\n- `beforeExecute`: Before execution\n- `afterExecute`: After execution\n- `error`: On error\n- `cacheHit`: Cache found\n- `cacheMiss`: Cache not found\n- `middlewareStart`: Middleware start\n- `middlewareEnd`: Middleware end\n- `noMatch`: No match found\n\n## 🎮 Complete Example (State Machine)\n\n```lua\nlocal gameState = {\n    score = 0,\n    lives = 3\n}\n\nlocal gameSwitch = Switch(\"game\")\n    -- Middleware for logging\n    :use(function(action)\n        print(\"Game action:\", action)\n        return action\n    end)\n    -- Events for debug\n    :on(\"beforeExecute\", function(action)\n        print(\"Current state - Score:\", gameState.score, \"Lives:\", gameState.lives)\n    end)\n    -- Game actions\n    :when(\"start\", function()\n        gameState.score = 0\n        gameState.lives = 3\n        return \"PLAYING\"\n    end)\n    :when(\"hit\", function()\n        gameState.lives = gameState.lives - 1\n        return gameState.lives \u003e 0 and \"PLAYING\" or \"GAME_OVER\"\n    end)\n    :when(\"score\", function()\n        gameState.score = gameState.score + 100\n        return \"PLAYING\"\n    end)\n    :default(function(action)\n        return \"UNKNOWN_ACTION\"\n    end)\n\n-- Game simulation\nprint(gameSwitch:execute(\"start\"))    -- Reset and start\nprint(gameSwitch:execute(\"score\"))    -- Score +100\nprint(gameSwitch:execute(\"hit\"))      -- Lose a life\n```\n\n## 🔧 Performance\n\nThe system uses several optimizations:\n- Cache with pairs for automatic memory management\n- Middleware compilation\n- Lookup minimization\n- Fluent method chaining\n\n## 🤝 Contributing\n\nContributions are welcome! Feel free to open an issue or submit a pull request.\n\n## 📄 License\n\n[MIT License](LICENSE.md)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkamionn%2Feasyswitchlua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkamionn%2Feasyswitchlua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkamionn%2Feasyswitchlua/lists"}