fork/m-overlay

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

blackops7799I lost track of what I'm even doing lola7d6bcc

GUI
4.8 KiB219 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 function val_to_str(v, stack, scope)
36		if type(v) == "string" then
37			v = string.gsub(v, "\n", "\\n" )
38			if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
39				return "'" .. v .. "'"
40			end
41			return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
42		else
43			return type(v) == "table" and table.tostring(v, stack, scope) or tostring(v)
44		end
45	end
46
47	local function key_to_str(k, stack, scope)
48		if type(k) == "string" and string.match(k, "^[_%a][_%a%d]*$") then
49			return k
50		else
51			return "[" .. val_to_str(k, stack, scope) .. "]"
52		end
53	end
54
55	function table.tostring(tbl, stack, scope)
56		stack = stack or {}
57		scope = scope or 0
58
59		if stack[tbl] then return error("circular reference") end
60
61		stack[tbl] = true
62		scope = scope + 1
63
64		local result = "{\n"
65
66		for k, v in pairs(tbl) do
67			local tabs = string.rep("\t", scope)
68			if type(v) == "table" then
69				result = result .. tabs .. key_to_str(k, stack, scope) .. " = " .. table.tostring(v, stack, scope) .. "\n"
70			else
71				result = result .. tabs .. key_to_str(k, stack, scope) .. " = " .. val_to_str(v, stack, scope) .. "\n"
72			end
73		end
74
75		scope = scope - 1
76		stack[tbl] = nil
77
78		return result .. string.rep("\t", scope) .. "}"
79	end
80end
81
82--[[local test = {
83	1, 2, 5,
84	["Kappa 1 2 3 \n lul"] = true,
85	data = {
86		1337.84239,
87		test = {
88			7, 8, "NINE"
89		}
90	}
91}
92
93print(table.tostring(love))]]
94
95function table.copy(object)
96	local lookup_table = {}
97	local function _copy(object)
98		if type(object) ~= "table" then
99			return object
100		elseif lookup_table[object] then
101			return lookup_table[object]
102		end
103		local new_table = {}
104		lookup_table[object] = new_table
105		for index, value in pairs(object) do
106			new_table[_copy(index)] = _copy(value)
107		end
108		return setmetatable(new_table, getmetatable(object))
109	end
110	return _copy(object)
111end
112
113function table.concatNumberedList(tbl, sep, oxford)
114	if type(tbl) ~= "table" then
115		return tostring(tbl)
116	end
117
118	local str = ""
119	local num = #tbl
120	for i=1,num do
121		if i < num then
122			-- If we aren't at the end of the list, use commas
123			-- If we're one before the end of the list, use an oxford comma if we want it
124			str = str .. i .. ". " .. tbl[i] .. ((oxford or i < num - 1) and ", " or " ")
125		elseif i > 1 then
126			-- If we're the last item and we have more than 1 item already..
127			-- Seperate using our defined seperator word
128			str = str .. (sep or "and") .. " " .. i .. ". " .. tbl[i]
129		else
130			-- Single item..
131			str = i .. ". " .. tbl[i]
132		end
133	end
134	return str
135end
136
137function table.concatList(tbl, sep, oxford)
138	if type(tbl) ~= "table" then
139		return tostring(tbl)
140	end
141
142	local str = ""
143	local num = #tbl
144	for i=1,num do
145		if i < num then
146			-- If we aren't at the end of the list, use commas
147			-- If we're one before the end of the list, use an oxford comma if we want it
148			str = str .. tbl[i] .. ((oxford or i < num - 1) and ", " or " ")
149		elseif i > 1 then
150			-- If we're the last item and we have more than 1 item already..
151			-- Seperate using our defined seperator word
152			str = str .. (sep or "and") .. " " .. tbl[i]
153		else
154			-- Single item..
155			-- Don't even bother concating the string
156			str = tbl[i]
157		end
158	end
159	return str
160end
161
162function table.max(tbl)
163	local max_key = nil
164	local max_value = -math.huge
165	for key, value in pairs(tbl) do
166		value = assert(tonumber(value), "expected a table full of numbers")
167		if value > max_value then
168			max_key = key
169			max_value = value
170		end
171	end
172	return max_key, max_value
173end
174
175function table.min(tbl)
176	local min_key = nil
177	local min_value = math.huge
178	for key, value in pairs(tbl) do
179		value = assert(tonumber(value), "expected a table full of numbers")
180		if value < min_value then
181			min_key = key
182			min_value = value
183		end
184	end
185	return min_key, min_value
186end
187
188function table.avg(tbl)
189	local num = 0
190	local total = 0
191	for key, value in pairs(tbl) do
192		value = assert(tonumber(value), "expected a table full of numbers")
193		total = total + value
194		num = num + 1
195	end
196	return total / num
197end
198
199function table.sum(tbl)
200	local total = 0
201	for key, value in pairs(tbl) do
202		value = assert(tonumber(value), "expected a table full of numbers")
203		total = total + value
204	end
205	return total
206end
207
208function table.random(tbl)
209	return tbl[math.random(1, #tbl)]
210end
211
212function table.randomkeyvalue(tbl)
213	local keys = {}
214	for k,v in pairs(tbl) do
215		table.insert(keys, k)
216	end
217	local key = keys[math.random(1, #keys)]
218	return key, tbl[key]
219end