Created
April 3, 2022 02:18
-
-
Save insyri/fa03c4507a27afe9dcd75f2f4b5ede74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
will return a string that looks like:
?hello=world&bool=true&page=1