-
Notifications
You must be signed in to change notification settings - Fork 0
/
tables.lua
209 lines (178 loc) · 4.08 KB
/
tables.lua
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local M = {}
local function is_array(o)
for key, _ in pairs(o) do
if type(key) ~= "number" then
return false
end
end
return true
end
local function array_contains(arr, value)
for _, val in pairs(arr) do
if val == value then
return true
end
end
return false
end
function M.clone(tbl, deep)
local target = {}
for k, v in pairs(tbl) do
if deep and type(v) == "table" then
target[k] = M.clone(v, deep)
else
target[k] = v
end
end
local meta = getmetatable(tbl)
setmetatable(target, not deep and meta or M.clone(meta, deep))
return target
end
function M.merge(src, dest, opts)
assert(type(dest) == "table", "destination must be a table (array ok)")
assert(src == nil or type(src) == "table", "source must be a table (nil ok)")
opts = opts or {}
if src ~= nil then
if is_array(src) and is_array(dest) then
if opts.unique then
local seen = {}
for _, value in ipairs(dest) do
seen[value] = true
end
for _, value in ipairs(src) do
if not seen[value] then
table.insert(dest, value)
seen[value] = true
end
end
else
for _, value in ipairs(src) do
table.insert(dest, value)
end
end
else
for key, value in pairs(src) do
if type(value) == "table" and type(dest[key]) == "table" then
M.merge(value, dest[key])
elseif dest[key] == nil or (opts.force == nil or opts.force) then
dest[key] = value
end
end
end
end
end
function M.contains(tbl, value)
if tbl ~= nil then
for _, v in ipairs(tbl) do
if v == value then
return true
end
end
end
return false
end
function M.is_empty(tbl)
return next(tbl) == nil
end
function M.make_keys(tbl, ...)
local current = tbl
for i = 1, select("#", ...) do
local key = select(i, ...)
if current[key] == nil then
current[key] = {}
end
current = current[key]
end
end
function M.has_keys(tbl, ...)
if tbl == nil then
return false
end
local current = tbl
for i = 1, select("#", ...) do
local key = select(i, ...)
if current[key] == nil then
return false
end
current = current[key]
end
return true
end
---@param items? any[] items to transform
---@param f? fun(a: any): any transformation to apply
function M.transform(items, f)
if items == nil or f == nil then
return
end
for k, v in pairs(items) do
items[k] = f(v)
end
end
local function overwrite(src, target)
for k, v in pairs(src) do
if type(v) == "table" then
target[k] = {}
overwrite(v, target[k])
else
target[k] = v
end
end
end
function M.overwrite(src, target)
for k, _ in pairs(target) do
target[k] = nil
end
overwrite(src, target)
end
local function _flat_append(dest, key, value)
if type(value) == "table" then
local keys = {}
for k, _ in pairs(value) do
table.insert(keys, k)
end
table.sort(keys)
for _, k in ipairs(keys) do
_flat_append(dest, k, value[k])
end
elseif key and type(key) ~= "number" then
dest[key] = value
elseif value ~= nil then
table.insert(dest, value)
end
end
function M.flattened(...)
local o = {}
for i = 1, select("#", ...) do
table.insert(o, select(i, ...))
end
local flat = {}
_flat_append(flat, nil, o)
return flat
end
function M.tostring(o)
local type_ = type(o)
if type_ == "string" then
return '"' .. tostring(o) .. '"'
elseif type_ ~= "table" then
return tostring(o)
end
local is_first = true
local is_array_ = is_array(o)
local result = is_array_ and "[" or "{"
for key, value in pairs(o) do
if not is_first then
result = result .. ", "
end
if not is_array_ then
if type(key) == "string" and key:find("%s") then
result = result .. '"' .. key .. '"' .. ": "
else
result = result .. tostring(key) .. ": "
end
end
result = result .. M.tostring(value)
is_first = false
end
return result .. (is_array_ and "]" or "}")
end
return M