forked from roblox-ts/object-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
125 lines (104 loc) · 2.29 KB
/
init.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
local HttpService = game:GetService("HttpService")
local Object = {}
function Object.keys(object)
local result = table.create(#object)
for key in pairs(object) do
result[#result + 1] = key
end
return result
end
function Object.values(object)
local result = table.create(#object)
for _, value in pairs(object) do
result[#result + 1] = value
end
return result
end
function Object.entries(object)
local result = table.create(#object)
for key, value in pairs(object) do
result[#result + 1] = { key, value }
end
return result
end
function Object.assign(toObj, ...)
for i = 1, select("#", ...) do
local arg = select(i, ...)
if type(arg) == "table" then
for key, value in pairs(arg) do
toObj[key] = value
end
end
end
return toObj
end
function Object.copy(object)
local result = table.create(#object)
for k, v in pairs(object) do
result[k] = v
end
return result
end
local function deepCopyHelper(object, encountered)
local result = table.create(#object)
encountered[object] = result
for k, v in pairs(object) do
if type(k) == "table" then
k = encountered[k] or deepCopyHelper(k, encountered)
end
if type(v) == "table" then
v = encountered[v] or deepCopyHelper(v, encountered)
end
result[k] = v
end
if type(object) == "table" then
local refMetatable = getmetatable(object)
if refMetatable then
setmetatable(result, refMetatable)
end
end
return result
end
function Object.deepCopy(object)
return deepCopyHelper(object, {})
end
function Object.deepEquals(a, b)
-- a[k] == b[k]
for k in pairs(a) do
local av = a[k]
local bv = b[k]
if type(av) == "table" and type(bv) == "table" then
local result = Object.deepEquals(av, bv)
if not result then
return false
end
elseif av ~= bv then
return false
end
end
-- extra keys in b
for k in pairs(b) do
if a[k] == nil then
return false
end
end
return true
end
function Object.toString(data)
return HttpService:JSONEncode(data)
end
function Object.isEmpty(object)
return next(object) == nil
end
function Object.fromEntries(entries)
local entriesLen = #entries
local result = table.create(entriesLen)
if entries then
for i = 1, entriesLen do
local pair = entries[i]
result[pair[1]] = pair[2]
end
end
return result
end
return Object