This repository was archived by the owner on Jul 11, 2026. It is now read-only.
forked from darrellanderson/CrLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.ttslua
More file actions
120 lines (104 loc) · 3.77 KB
/
Copy pathTable.ttslua
File metadata and controls
120 lines (104 loc) · 3.77 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
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
-------------------------------------------------------------------------------
--- Lua table utility functions
-- @author Darrell
-------------------------------------------------------------------------------
local TAG = 'CrLua.Table'
CrLua = CrLua or {} -- global, <include> wraps in a do .. end block
CrLua.Table = assert(not CrLua.Table) and {
_require = {}
}
-------------------------------------------------------------------------------
--- Copy a table, including sub-tables.
-- Make a new table, copying key->value from source table.
-- @param table
-- @return new table
-------------------------------------------------------------------------------
function CrLua.Table.copy(table)
assert(type(table) == 'table')
local result = {}
for k, v in pairs(table) do
result[k] = type(v) == 'table' and CrLua.Table.copy(v) or v
end
return result
end
function CrLua.Table._testCopy()
local table = { a = 1, b = 2 }
local copy = CrLua.Table.copy(table)
assert(copy ~= table)
assert(copy.a == 1 and copy.b == 2)
end
-------------------------------------------------------------------------------
--- Merge two tables.
-- If tables have any keys in common, result value is from b.
-- @param a table
-- @param b table
-- @return table with both a and b key->value entries.
-------------------------------------------------------------------------------
function CrLua.Table.join(a, b)
assert(type(a) == 'table' and type(b) == 'table')
local result = {}
for k, v in pairs(a) do
result[k] = v
end
for k, v in pairs(b) do
result[k] = v
end
return result
end
function CrLua.Table._testJoin()
local a = { color = 'yellow', flavor = 'sour' }
local b = { color = 'red', scent = 'strawberry' }
local joined = CrLua.Table.join(a, b)
assert(joined.color == 'red')
assert(joined.flavor == 'sour')
assert(joined.scent == 'strawberry')
end
-------------------------------------------------------------------------------
--- Do the two tables contatain identical key/value pairs, including subtables?
-- @param a table.
-- @param b table.
-- @param path string : omit for first call, otherwise string path to depth.
-- @return boolean : true if equal, otherwise (false, path-to-mismatch) pair.
-- WARNING: cycles in a table cause infinite loops!
-------------------------------------------------------------------------------
function CrLua.Table.deepEquals(a, b, path)
assert(type(a) == 'table' and type(b) == 'table')
if a == b then
return true
end
local function appendToPath(k)
return (path and (path .. '.') or '') .. tostring(k)
end
local function errorMessage(k)
local pathPlusK = appendToPath(k)
local av = tostring(a[k] or '<nil>')
local bv = tostring(b[k] or '<nil>')
return TAG .. '.deepEquals: ' .. pathPlusK .. ' <' .. av .. '> ~= <' .. bv .. '>'
end
for k, _ in pairs(b) do
if type(a[k]) == 'nil' then
return false, errorMessage(k)
end
end
for k, av in pairs(a) do
local bv = b[k]
if type(av) == 'table' and type(bv) == 'table' then
local equals, message = CrLua.Table.deepEquals(av, bv, appendToPath(k))
if not equals then
return false, message
end
elseif (type(av) ~= type(bv)) or (av ~= bv) then
return false, errorMessage(k)
end
end
return true
end
function CrLua.Table._testDeepEquals()
local a = { one = 1, two = { alpha = 2 }}
local b = { one = 1, two = { alpha = 2 }}
assert(CrLua.Table.deepEquals(a, b))
local b = { one = 2, two = { alpha = 2 }}
assert(not CrLua.Table.deepEquals(a, b))
local b = { one = 1, two = { alpha = 3 }}
assert(not CrLua.Table.deepEquals(a, b))
end