-
Notifications
You must be signed in to change notification settings - Fork 10
/
TableUtil.lua
177 lines (147 loc) · 4.19 KB
/
TableUtil.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
--[[----------------------------------------------------------------------------
LiteMount/TableUtil.lua
Copyright 2011 Mike Battersby
----------------------------------------------------------------------------]]--
local _, LM = ...
function LM.tMap(t, f, isIndexTable)
local out = {}
if isIndexTable then
for i, v in ipairs(t) do
out[i] = f(v)
end
else
for k, v in pairs(t) do
out[k] = f(v)
end
end
return out
end
function LM.tCopyShallow(t)
local out = {}
for k,v in pairs(t) do out[k] = v end
return out
end
function LM.tSlice(t, from, to)
return { unpack(t, from, to) }
end
local function tostringCompare(a, b)
return tostring(a) < tostring(b)
end
function LM.PairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f or tostringCompare)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil else return a[i], t[a[i]] end
end
return iter
end
function LM.tJoin(...)
local out = {}
for i = 1, select('#', ...) do
local t = select(i, ...)
for _,v in ipairs(t) do
table.insert(out, v)
end
end
return out
end
-- Really these are
-- LM.tUpdate = Mixin
-- LM.tMerge = CreateFromMixins
function LM.tUpdate(out, ...)
for i = 1, select('#', ...) do
local t = select(i, ...)
for k,v in pairs(t) do
out[k] = v
end
end
return out
end
function LM.tMerge(...)
return LM.tUpdate({}, ...)
end
-- This dumper is adapted from DevTools_Dump and is a lot less
-- capable BUT it never truncates anything, and it doesn't put
-- color codes into the output.
local function prepSimple(val)
local valType = type(val)
if valType == 'nil' then
return 'nil'
elseif valType == 'number' then
return val
elseif valType == 'boolean' then
if val then
return 'true'
else
return 'false'
end
elseif valType == 'string' then
return string.format('%q', val)
end
end
local function prepSimpleKey(val)
if (string.match(val, "^[a-zA-Z_][a-zA-Z0-9_]*$")) then
return val
else
return '[' .. prepSimple(val) .. ']'
end
end
local function keyComp(a, b)
if type(a) == type(b) then
return a < b
else
return tostring(a) < tostring(b)
end
end
local DumpValue
local function DumpTableContents(val, prefix, firstPrefix, context)
local oldDepth = context.depth
local oldKey = context.key
local iter = LM.PairsByKeys(val, keyComp)
local nextK, nextV = iter(val, nil)
while nextK do
local k, v = nextK, nextV
nextK, nextV = iter(val, k)
local prepKey = prepSimpleKey(k)
if oldKey == nil then
context.key = prepKey
elseif prepKey:sub(1,1) == '[' then
context.key = oldKey .. prepKey
else
context.key = oldKey .. '.' .. prepKey
end
context.depth = oldDepth + 1
local rp = string.format('%s%s = ', firstPrefix, prepKey)
firstPrefix = prefix
DumpValue(v, prefix, rp, (nextK and ',') or '', context)
end
context.key = oldKey
context.depth = oldDepth
end
function DumpValue(val, prefix, firstPrefix, suffix, context)
local valType = type(val)
if valType ~= 'table' then
table.insert(context.lines, string.format('%s%s%s', firstPrefix, prepSimple(val), suffix))
return
else
firstPrefix = firstPrefix .. '{'
local oldPrefix = prefix
prefix = prefix .. ' '
table.insert(context.lines, firstPrefix)
firstPrefix = prefix
DumpTableContents(val, prefix, firstPrefix, context)
table.insert(context.lines, oldPrefix .. '}' .. suffix)
end
end
function LM.TableToString(val)
local context = { depth = 0, lines = {} }
DumpTableContents(val, '', '', context)
return table.concat(context.lines, '\n') .. '\n'
end
local defaultIndexer = { __index = function (t, k) return t.DEFAULT end }
function LM.TableWithDefault(t)
return setmetatable(t, defaultIndexer)
end