-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_utils.lua
170 lines (136 loc) · 2.95 KB
/
table_utils.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
local M = {}
---@param self table
---@param value any
---@return boolean
function M.contains(self, value)
if M.is_empty(self) then
return false
end
for _, _value in pairs(self) do
if _value == value then
return true
end
end
return false
end
---@param self table
---@return boolean
function M.is_empty(self)
return #self == 0
end
---@param value any
---@param self table
---@return integer?
function M.get_index(self, value)
local index = 1
for _, _value in pairs(self) do
if _value == value then
return index
end
index = index + 1
end
return nil
end
---@param self table
---@param other_table table
---@return boolean
function M.is_equal(self, other_table)
if #self ~= #other_table then
return false
end
for key, value in pairs(self) do
if other_table[key] ~= value then
return false
end
end
return true
end
---@deprecated Use table.concat() instead
---@param self table
---@param other_table table
---@param element_condition? fun(any):boolean
---@return table
function M.concat_tables(self, other_table, element_condition)
local always_true_fun = function()
return true
end
element_condition = element_condition or always_true_fun
local final_result = {}
for _, value in pairs(self) do
if element_condition(value) then
table.insert(final_result, value)
end
end
for _, value in pairs(other_table) do
if element_condition(value) then
table.insert(final_result, value)
end
end
return final_result
end
---@param lhs table
---@param rhs table
---@return table
function M.concat(lhs, rhs)
local result = lhs
for k, v in pairs(rhs) do
result[k] = v
end
return result
end
---@generic T
---@param self table
---@param callback fun(element: T):boolean
---@return table
function M.filter(self, callback)
local result = {}
for _, value in pairs(self) do
if callback(value) then
table.insert(result, value)
end
end
return result
end
---@generic T, U
---@param self table
---@param callback fun(item: T): U
---@return table
function M.map(self, callback)
local result = {}
for _, value in pairs(self) do
table.insert(result, callback(value))
end
return result
end
---@generic K, V, U
---@param self table
---@param callback fun(key: K, value: V): U
---@return table
function M.mapkv(self, callback)
local t = {}
for key, value in pairs(self) do
table.insert(t, callback(key, value))
end
return t
end
---@generic T
---@param size integer
---@param defaultValue T
---@return table<T>
function M.alloc(size, defaultValue)
local t = {}
for i = 1, size do
t[i] = defaultValue
end
return t
end
---@param self table
---@return string
function M.toString(self)
local json = require("cluautils.json")
return json.encode(self, {
pretty = true,
indent = " ",
})
end
return M