-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.lua
More file actions
62 lines (57 loc) · 1.25 KB
/
Copy pathstringify.lua
File metadata and controls
62 lines (57 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local function explicit(value)
local type = type(value)
if type == "string" then
return '"' .. value .. '"'
elseif type == "number" then
return value
elseif type == "boolean" then
return tostring(value)
elseif type == "nil" then
return type
end
return '"' .. tostring(value) .. '"'
end
return function(root)
if type(root) ~= "table" then
return explicit(root)
end
local image = {}
local counter = 0
local insert = function(value)
counter = counter + 1
image[counter] = value
end
local remove = function()
image[counter] = nil
counter = counter - 1
end
local mapper = {}
local function traverse(root, indent, prefix)
if mapper[root] then
insert("[=[" .. mapper[root] .. "]=]")
return
end
mapper[root] = prefix or "[=[[nil]]=]"
local empty = true
insert('{')
insert('\n')
for key, value in pairs(root) do
empty = false
insert(indent .. "\t[" .. explicit(key) .. "] = ")
if type(value) ~= "table" then
insert(explicit(value))
else
local prefix = (prefix or "") .. '[' .. explicit(key) .. ']'
traverse(value, indent .. '\t', prefix)
end
insert(",\n")
end
remove()
if not empty then
insert('\n' .. indent)
end
insert('}')
end
traverse(root, "")
return table.concat(image)
end