forked from dmccuskey/Ghost-vs-Monsters---OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmc_utils.lua
205 lines (155 loc) · 5.07 KB
/
dmc_utils.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
--====================================================================--
-- dmc_utils.lua
--
-- by David McCuskey
-- Documentation: http://docs.davidmccuskey.com/display/docs/dmc_utils.lua
--====================================================================--
--[[
Copyright (C) 2011 David McCuskey. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
--]]
local Utils = {}
-- extend()
-- Copy key/values from one table to another
-- Will deep copy any value from first table which is itself a table.
--
-- @param fromTable the table (object) from which to take key/value pairs
-- @param toTable the table (object) in which to copy key/value pairs
-- @return table the table (object) that received the copied items
--
function Utils.extend( fromTable, toTable )
function _extend( fT, tT )
for k,v in pairs( fT ) do
if type( fT[ k ] ) == "table" and
type( tT[ k ] ) == "table" then
tT[ k ] = _extend( fT[ k ], tT[ k ] )
elseif type( fT[ k ] ) == "table" then
tT[ k ] = _extend( fT[ k ], {} )
else
tT[ k ] = v
end
end
return tT
end
return _extend( fromTable, toTable )
end
-- hasOwnProperty()
-- Find a property directly on an object (not via inheritance prototype)
--
-- @param table the table (object) to search
-- @param prop the name (string) of the property to find
-- @return true if property is found, false otherwise
--
function Utils.hasOwnProperty( table, property )
if rawget( table, property ) ~= nil then
return true
end
return false
end
-- propertyIn()
-- Determines whether a property is within a list of items in a table (acting as an array)
--
-- @param table the table with *list* of properties
-- @param property the name of the property to search for
--
function Utils.propertyIn( table, property )
for _, v in pairs( table ) do
if v == property then return true end
end
return false
end
-- destroy()
-- Deletes all of the items in a table structure.
-- This is intended for generic tables and structures, *not* objects
--
-- @param table the table from which to delete contents
--
function Utils.destroy( table )
if type( table ) ~= "table" then return end
function _destroy( t )
for k,v in pairs( t ) do
if type( t[ k ] ) == "table" then
_destroy( t[ k ] )
end
t[ k ] = nil
end
end
-- start destruction process
_destroy( table )
end
-- createObjectCallback()
-- Creates a closure used to bind a method to an object. Useful for creating a custom callback.
--
-- @param object the object which has the method
-- @param method the method to call
--
function Utils.createObjectCallback( object, method )
return function( ... )
method( object, ... )
end
end
-- print()
-- print out the keys contained within a table.
-- by default, does not process items with underscore '_'
--
-- @param table the table (object) to print
-- @param include a list of names to include
-- @param exclude a list of names to exclude
--
function Utils.print( table, include, exclude, params )
local indent = ""
local step = 0
local include = include or {}
local exclude = exclude or {}
local params = params or {}
local options = {
limit = 10,
}
opts = Utils.extend( params, options )
--print("Printing object table =============================")
function _print( t, ind, s )
-- limit number of rounds
if s > options.limit then return end
for k, v in pairs( t ) do
local ok_to_process = true
if Utils.propertyIn( include, k ) then
ok_to_process = true
elseif type( t[k] ) == "function" or
Utils.propertyIn( exclude, k ) or
type( k ) == "string" and k:sub(1,1) == '_' then
ok_to_process = false
end
if ok_to_process then
if type( t[ k ] ) == "table" then
local o = t[ k ]
local address = tostring( o )
local items = #o
print ( ind .. k .. " --> " .. address .. " w " .. items .. " items" )
_print( t[ k ], ( ind .. " " ), ( s + 1 ) )
else
if type( v ) == "string" then
print ( ind .. k .. " = '" .. v .. "'" )
else
print ( ind .. k .. " = " .. tostring( v ) )
end
end
end
end
end
-- start printing process
_print( table, indent, step + 1 )
end
return Utils