{"id":20994859,"url":"https://github.com/xiedacon/lua-utility","last_synced_at":"2025-08-21T20:18:21.995Z","repository":{"id":54474598,"uuid":"151814373","full_name":"xiedacon/lua-utility","owner":"xiedacon","description":"A collection of useful utilities while writing Lua.","archived":false,"fork":false,"pushed_at":"2025-03-12T16:28:43.000Z","size":38,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T23:24:36.494Z","etag":null,"topics":["lua","lua-utils","luajit","luarocks","opm"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xiedacon.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":"2018-10-06T07:10:01.000Z","updated_at":"2025-03-12T16:43:05.000Z","dependencies_parsed_at":"2025-03-12T17:35:53.730Z","dependency_job_id":null,"html_url":"https://github.com/xiedacon/lua-utility","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiedacon%2Flua-utility","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiedacon%2Flua-utility/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiedacon%2Flua-utility/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xiedacon%2Flua-utility/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xiedacon","download_url":"https://codeload.github.com/xiedacon/lua-utility/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254230798,"owners_count":22036244,"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":["lua","lua-utils","luajit","luarocks","opm"],"created_at":"2024-11-19T07:20:08.451Z","updated_at":"2025-05-14T21:30:53.918Z","avatar_url":"https://github.com/xiedacon.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lua-utility\n\n[![MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/xiedacon/lua-utility/blob/master/LICENSE)\n\n## Requirements\n\n- cjson\n\n## Usage\n\n- [Base](#Base)\n  - try\n  - noop\n  - once\n- [Array](#Array)\n  - concat\n  - diff\n  - each\n  - every\n  - fill\n  - filter\n  - find\n  - findIndex\n  - from\n  - includes\n  - indexOf\n  - join\n  - lastIndexOf\n  - map\n  - pop\n  - push\n  - reduce\n  - reduceRight\n  - reverse\n  - shift\n  - slice\n  - some\n  - sort\n  - splice\n  - toString\n  - union\n  - unshift\n- [Function](#Function)\n  - apply\n  - bind\n  - call\n- [Object](#Object)\n  - assign\n  - entries\n  - freeze\n  - get\n  - keys\n  - omit\n  - pick\n  - set\n  - values\n- [String](#String)\n  - endsWith\n  - replace\n  - slice\n  - split\n  - startsWith\n  - trim\n  - trimLeft\n  - trimRight\n\n#### Base\n\n```lua\nlocal resty_redis = require \"resty.redis\"\nlocal utility = require \"utility\"\n\nfunction get_redis()\n    local redis, err = resty_redis:new()\n    if not redis then return nil, err end\n\n    local ok, err = redis:connect(\"127.0.0.1\", 6379)\n\n    if ok then\n        return redis\n    else\n        return nil, err\n    end\nend\n\nfunction close_redis(res)\n    local redis = res[0]\n    local data = res[1]\n\n    redis:close()\n\n    return data\nend\n\nlocal res, err = utility.try({\n    get_redis\n    function(red)\n        local data, err = redis:get(\"something\")\n\n        return { redis, data }, err\n    end,\n    close_redis\n}, function(err)\n    return nil, \"some error happened\"\nend)\n\nutility.noop() -- nothing happend\nutility.once(function()\n    -- only call once\nend)\n```\n\n#### Array\n\nThis api is similar to [js Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\n\n```lua\nlocal Array = require \"utility.array\"\n\nlocal arr = Array({}) -- {}\nlocal arr1 = Array({ 1, 2, 3 }) -- { 1, 2, 3 }\nlocal arr2 = Array.from({ 1, 2, 3 }) -- { 1, 2, 3 }\n\narr:concat(arr1)\narr:each(function(value, index, arr) end)\narr:every(function(value, index, arr) end)\narr:fill(1)\narr:filter(function(value, index, arr) end)\narr:find(function(value, index, arr) end)\narr:findIndex(function(value, index, arr) end)\narr:forEach(function(value, index, arr) end)\narr:includes(1)\narr:indexOf(1)\narr:join('-')\narr:lastIndexOf(1)\narr:map(function(value, index, arr) end)\narr:pop()\narr:push(1)\narr:reduce(function(result, value, index, arr) end, 0)\narr:reduceRight(function(result, value, index, arr) end, 0)\narr:reverse()\narr:shift()\narr:slice(0, 1)\narr:some(function(value, index, arr) end)\narr:sort(function(value1, value2) end)\narr:splice(0, 1)\narr:toString()\narr:unshift(1)\narr:diff(arr1) -- arr_only, arr1_only\narr:union(arr1, arr2) -- arr U arr1 U arr2\n```\n\n#### Function\n\n```lua\nlocal Function require \"utility.function\"\n\nlocal func = Function(function(arg1, arg2, arg3)\n    print(arg1 .. \" \" .. arg2 .. \" \" .. arg3)\nend)\n\n-- 1 2 3\nfunc(1, 2, 3)\n-- 1 2 3\nfunc:apply({ 1, 2, 3 })\n-- 1 2 4\nfunc:bind(1, 2)(2, 3, 4)\n-- 1 2 2 3 4\nfunc:call(1, 2, 3)\n```\n\n#### Object\n\n```lua\nlocal Object = require \"utility.array\"\n\n-- { a = 2, b = 3, c = 2, d = 3 }\nObject.assign({\n    a = 1,\n    b = 1\n}, {\n    a = 2,\n    c = 2\n}, {\n    b = 3,\n    d = 3\n})\n\n-- { a = 1, b = 1 }\nObject.pick({\n    a = 1,\n    b = 1,\n    c = 1\n}, { \"a\", \"b\" })\n\n-- { c = 1 }\nObject.omit({\n    a = 1,\n    b = 1,\n    c = 1\n}, { \"a\", \"b\" })\n\n-- 1\nObject.get({\n    a = {\n        a = 1\n    },\n    b = 1,\n    c = 1\n}, \"a.a\") -- or { \"a\", \"a\" }\n\n-- 0\nObject.get({\n    a = 1,\n    b = 1,\n    c = 1\n}, \"a.a\", 0)\n\n-- { a = { a = 1 } }\nObject.set({}, \"a.a\", 1)\n\n-- { a = { a = 1 } }\nObject.set({}, { \"a\", \"a\" }, 1)\n\n-- { \"a\", \"b\", \"c\" }\nObject.keys({\n    a = 1,\n    b = 2,\n    c = 3\n})\n\n-- { 1, 2, 3 }\nObject.values({\n    a = 1,\n    b = 2,\n    c = 3\n})\n\n-- { { \"a\", 1 }, { \"b\", 2 }, { \"c\", 3 } }\nObject.entries({\n    a = 1,\n    b = 2,\n    c = 3\n})\n\nlocal obj = { a = \"a\" }\nObject.freeze(obj) -- after this, obj is not editable\n\nobj.a = \"b\" -- { a = \"a\" }\nobj.b = \"b\" -- { a = \"a\" }\n```\n\n#### String\n\n```lua\nlocal String = require \"utility.string\"\n\nString.endsWith(\"test.lua\", \".lua\") -- true\nString.endsWith(\"test.lua\", \".lu\") -- false\nString.replace(\"test.lua\", \".\", \"-\") -- \"test-lua\"\nString.replace(\"$a-$b\", { a = \"aaa\", b = \"bbb\" }) -- \"aaa-bbb\"\nString.replace(\"${a}-${b}\", { a = \"aaa\", b = \"bbb\" }, function(k) return \"${\" .. k .. \"}\" end) -- \"aaa-bbb\"\nString.slice(\"test.lua\", 1, 5) -- test\nString.split(\"test.lua\", \".\") -- { \"test\", \"lua\" }\nString.startsWith(\"test.lua\", \"test\") -- true\nString.startsWith(\"test.lua\", \"est\") -- false\nString.trim(\"  test.lua  \") -- \"test.lua\"\nString.trimLeft(\"  test.lua  \") -- \"test.lua  \"\nString.trimRight(\"  test.lua  \") -- \"  test.lua\"\n```\n\n## Tests\n\n```shell\nexport LUA_PATH=\"/path/to/lua-utility/lib/?.lua;;\"\nexport LUA_CPATH=\"/path/to/lua-cjson/?.so;;\"\n\nlua test.lua\n```\n\n## License\n\n[MIT License](https://github.com/xiedacon/lua-utility/blob/master/LICENSE)\n\nCopyright (c) 2018 xiedacon\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiedacon%2Flua-utility","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxiedacon%2Flua-utility","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxiedacon%2Flua-utility/lists"}