Skip to content

Instantly share code, notes, and snippets.

@Fizzadar
Last active January 20, 2016 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fizzadar/5076072 to your computer and use it in GitHub Desktop.
Save Fizzadar/5076072 to your computer and use it in GitHub Desktop.
Lua table => Lua Code
local testconf = {
test = 'hi',
test2 = {
hi = 'ho',
what = true,
fuck = function() return 'hello' end
}
}
function tableToLua( table )
local out = ''
for k, v in pairs( table ) do
if type( v ) == 'table' then
out = out .. k .. '={' .. tableToLua( v ) .. '},'
else
if type( v ) == 'function' then v = tostring( v() ) end
if type( v ) == 'string' then v = "'" .. v .. "'" end
if type( v ) == 'boolean' then v = tostring( v ) end
out = out .. k .. '=' .. v .. ','
end
end
out = out:sub( 0, out:len() - 1 )
return out
end
function tableToLuaFile( table )
return 'local _lua = {' .. tableToLua( table ) .. '} return _lua'
end
print( tableToLuaFile( testconf ) )
--output =
local _lua = {test='hi',test2={fuck='hello',hi='ho',what=true}} return _lua
@jdjasperson
Copy link

Love the test!! Of course, hopefully some errant mistake never results in fucking "unprofessional" language making its way into production code. Personally, I find that sprinkling funny fucking shit in my tests is the best way to keep my interest. Plus, the test output is fun too; nothing beats an obscenely profane test suite.

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