-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify_o1.lua
More file actions
62 lines (58 loc) · 1.38 KB
/
Copy pathstringify_o1.lua
File metadata and controls
62 lines (58 loc) · 1.38 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 mapper = {}
local function traverse(root, indent, prefix)
if mapper[root] then
counter = counter + 1
image[counter] = "[=[" .. mapper[root] .. "]=]"
return
end
mapper[root] = prefix or "[=[[nil]]=]"
local empty = true
counter = counter + 1
image[counter] = '{'
counter = counter + 1
image[counter] = '\n'
for key, value in pairs(root) do
empty = false
counter = counter + 1
image[counter] = indent .. '[' .. explicit(key) .. "] = "
if type(value) ~= "table" then
counter = counter + 1
image[counter] = explicit(value)
else
local prefix = (prefix or "") .. '[' .. explicit(key) .. ']'
traverse(value, indent .. '\t', prefix)
end
counter = counter + 1
image[counter] = ",\n"
end
image[counter] = nil
counter = counter - 1
if not empty then
counter = counter + 1
image[counter] = '\n' .. indent
end
counter = counter + 1
image[counter] = '}'
end
traverse(root, "")
return table.concat(image)
end