Skip to content

Instantly share code, notes, and snippets.

@insyri
Created April 3, 2022 02:18
Show Gist options
  • Save insyri/fa03c4507a27afe9dcd75f2f4b5ede74 to your computer and use it in GitHub Desktop.
Save insyri/fa03c4507a27afe9dcd75f2f4b5ede74 to your computer and use it in GitHub Desktop.
-- use like this:
local err, res = parseToURLArgs()
-- thanks golang for this idea
function parseToURLArgs(tb)
function Err(err) return err, nil end
function Ok(res) return nil, res end
-- err checking
if not tb then return Err('got nothing') end
if type(tb) ~= 'table' then return Err('expected table, got '..type(tb)) end
local str = '?'
local index = 1
for key, value in pairs(tb) do
if index == 1 then
str = str..key..'='..t(value)
else
str = str..'&'..key..'='..t(value)
end
index = index + 1
end
return Ok(str)
end
@dowoge
Copy link

dowoge commented Apr 3, 2022

will return a string that looks like: ?hello=world&bool=true&page=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment