fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

BkacjiosMerge branch 'tmp' into settings-overhaulf7324a4

settings-overhaul
5.4 KiB263 linesraw
1function table.merge(a,b)
2	for k,v in pairs(b) do
3		if not a[k] then a[k]=v end
4	end
5	return a
6end
7
8function table.clone(a)
9	local ret = {}
10	for k,v in pairs(a) do
11		ret[k] = type(v) == "table" and table.clone(v) or v
12	end
13	return ret
14end
15
16local function reversedipairsiter(t, i)
17    i = i - 1
18    if i ~= 0 then
19        return i, t[i]
20    end
21end
22
23function reversedipairs(t)
24    return reversedipairsiter, t, #t + 1
25end
26
27function table.hasValue(tbl, val)
28	for key, value in pairs(tbl) do
29		if (value == val) then return true, key end
30	end
31	return false
32end
33
34do
35	local recursiveTableString
36	local stack = {}
37	local cache = {}
38
39	local KEY_TYPE_DOT = 1
40	local KEY_TYPE_BRACKET = 2
41
42	local function val_to_str(v, name)
43		if type(v) == "string" then
44			v = string.gsub(v, "\n", "\\n" )
45			if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
46				return "'" .. v .. "'"
47			end
48			return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
49		elseif type(v) == "table" then
50			return recursiveTableString(v, name)
51		else
52			return tostring(v)
53		end
54	end
55
56	local function key_type(k)
57		if type(k) == "string" and string.match(k, "^[_%a][_%a%d]*$") then
58			return KEY_TYPE_DOT
59		else
60			return KEY_TYPE_BRACKET
61		end
62	end
63
64	local function key_to_str(k, name)
65		if key_type(k) == KEY_TYPE_DOT then
66			return k
67		else
68			return "[" .. val_to_str(k, name) .. "]"
69		end
70	end
71
72	local function getStackName()
73		local result = ""
74		for pos, name in ipairs(stack) do
75			if key_type(name) == KEY_TYPE_DOT and pos > 1 then
76				result = result .. "." .. key_to_str(name)
77			else
78				result = result .. key_to_str(name)
79			end
80		end
81		return result
82	end
83
84	local function getTabs()
85		return string.rep("\t", #stack)
86	end
87
88	recursiveTableString = function (tbl, name)
89		name = name or "self"
90
91		local result = "{\n"
92
93		if cache[tbl] then
94			return cache[tbl]
95		end
96
97		table.insert(stack, name)
98		cache[tbl] = getStackName()
99
100		for k, v in pairs(tbl) do
101			result = result .. getTabs() .. key_to_str(k, k) .. " = " .. val_to_str(v, k) .. "\n"
102		end
103		table.remove(stack, #stack)
104
105		return result .. getTabs(stack) .. "}"
106	end
107
108	function table.tostring(tbl)
109		stack = {}
110		cache = {}
111		return recursiveTableString(tbl)
112	end
113end
114
115--[[local test = {
116	1, 2, 5,
117	["Kappa 1 2 3 \n lul"] = true,
118	data = {
119		1337.84239,
120		test = {
121			7, 8, "NINE"
122		}
123	}
124}
125
126table.insert(test.data, test)
127test.data.test[3] = test
128test[58] = test.data.test
129
130print(table.tostring(test))]]
131
132function table.copy(object)
133	local lookup_table = {}
134	local function _copy(object)
135		if type(object) ~= "table" then
136			return object
137		elseif lookup_table[object] then
138			return lookup_table[object]
139		end
140		local new_table = {}
141		lookup_table[object] = new_table
142		for index, value in pairs(object) do
143			new_table[_copy(index)] = _copy(value)
144		end
145		return setmetatable(new_table, getmetatable(object))
146	end
147	return _copy(object)
148end
149
150function table.concatNumberedList(tbl, sep, oxford)
151	if type(tbl) ~= "table" then
152		return tostring(tbl)
153	end
154
155	local str = ""
156	local num = #tbl
157	for i=1,num do
158		if i < num then
159			-- If we aren't at the end of the list, use commas
160			-- If we're one before the end of the list, use an oxford comma if we want it
161			str = str .. i .. ". " .. tbl[i] .. ((oxford or i < num - 1) and ", " or " ")
162		elseif i > 1 then
163			-- If we're the last item and we have more than 1 item already..
164			-- Seperate using our defined seperator word
165			str = str .. (sep or "and") .. " " .. i .. ". " .. tbl[i]
166		else
167			-- Single item..
168			str = i .. ". " .. tbl[i]
169		end
170	end
171	return str
172end
173
174function table.concatList(tbl, sep, oxford)
175	if type(tbl) ~= "table" then
176		return tostring(tbl)
177	end
178
179	local str = ""
180	local num = #tbl
181	for i=1,num do
182		if i < num then
183			-- If we aren't at the end of the list, use commas
184			-- If we're one before the end of the list, use an oxford comma if we want it
185			str = str .. tbl[i] .. ((oxford or i < num - 1) and ", " or " ")
186		elseif i > 1 then
187			-- If we're the last item and we have more than 1 item already..
188			-- Seperate using our defined seperator word
189			str = str .. (sep or "and") .. " " .. tbl[i]
190		else
191			-- Single item..
192			-- Don't even bother concating the string
193			str = tbl[i]
194		end
195	end
196	return str
197end
198
199function table.max(tbl)
200	local max_key = nil
201	local max_value = -math.huge
202	for key, value in pairs(tbl) do
203		value = assert(tonumber(value), "expected a table full of numbers")
204		if value > max_value then
205			max_key = key
206			max_value = value
207		end
208	end
209	return max_key, max_value
210end
211
212function table.min(tbl)
213	local min_key = nil
214	local min_value = math.huge
215	for key, value in pairs(tbl) do
216		value = assert(tonumber(value), "expected a table full of numbers")
217		if value < min_value then
218			min_key = key
219			min_value = value
220		end
221	end
222	return min_key, min_value
223end
224
225function table.avg(tbl)
226	local num = 0
227	local total = 0
228	for key, value in pairs(tbl) do
229		value = assert(tonumber(value), "expected a table full of numbers")
230		total = total + value
231		num = num + 1
232	end
233	return total / num
234end
235
236function table.sum(tbl)
237	local total = 0
238	for key, value in pairs(tbl) do
239		value = assert(tonumber(value), "expected a table full of numbers")
240		total = total + value
241	end
242	return total
243end
244
245function table.random(tbl)
246	return tbl[math.random(1, #tbl)]
247end
248
249function table.randomkeyvalue(tbl)
250	local keys = {}
251	for k,v in pairs(tbl) do
252		table.insert(keys, k)
253	end
254	local key = keys[math.random(1, #keys)]
255	return key, tbl[key]
256end
257
258function table.shuffle(tbl)
259	for i = #tbl, 2, -1 do
260		local j = math.random(i)
261		tbl[i], tbl[j] = tbl[j], tbl[i]
262	end
263end