Skip to content

Instantly share code, notes, and snippets.

@Fizzadar

Fizzadar/table_to_file.lua

Last active Jan 20, 2016
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@jdjasperson jdjasperson commented Jan 20, 2016

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