-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.lua
More file actions
80 lines (76 loc) · 1.91 KB
/
Table.lua
File metadata and controls
80 lines (76 loc) · 1.91 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
--!strict
--!native
local Table = {}
Table.getn = function(tbl: {[any]: any}): number
local i: number = 0
for _ in tbl do
i += 1
end
return i
end
Table.isarray = function(tbl: {[any]: any}): boolean
return #tbl == Table.getn(tbl)
end
Table.remove = function(tbl: {[any]: any}, k: any): any?
local v: any? = nil
if Table.isarray(tbl) then
v = table.remove(tbl, k)
else
v = tbl[k]
tbl[k] = nil
end
return v
end
Table.clear = table.clear
Table.clone = function(tbl: {[any]: any}, isDeepClone: boolean): {[any]: any}
local newTbl = {}
for k, v in tbl do
newTbl[k] = if isDeepClone and typeof(v) == "table" then Table.clone(v, isDeepClone) else v
end
return newTbl
end
Table.concat = table.concat
Table.create = table.create
Table.filter = function(tbl: {[any]: any}, f: (any, any, {[any]: any}) -> boolean): {[any]: any}
local newTbl = Table.clone(tbl, true)
local hasRemovedSomething: boolean = true
repeat
hasRemovedSomething = false
for k, v in newTbl do
if f(v, k, newTbl) then
continue
end
Table.remove(newTbl, k)
hasRemovedSomething = true
break
end
until not hasRemovedSomething
return newTbl
end
Table.find = table.find
Table.foreach = function(tbl: {[any]: any}, f: (any, any, {[any]: any}) -> ()): ()
for k, v in tbl do
f(v, k, tbl)
end
end
Table.foreachi = function(tbl: {[any]: any}, f: (any, number, {[any]: any}) -> ()): ()
for i, v in ipairs(tbl) do
f(v, i, tbl)
end
end
Table.freeze = table.freeze
Table.insert = table.insert
Table.isfrozen = table.isfrozen
Table.map = function(tbl: {[any]: any}, f: (any, any, {[any]: any}) -> any): {[any]: any}
local newTbl = Table.clone(tbl, true)
for k, v in newTbl do
newTbl[k] = f(v, k, newTbl)
end
return newTbl
end
Table.maxn = table.maxn
Table.move = table.move
Table.pack = table.pack
Table.sort = table.sort
Table.unpack = table.unpack
return Table