fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
94c74ee
v2.1.2
1function table . merge ( a , b ) 2for k , v in pairs ( b ) do 3if not a [ k ] then a [ k ] =v end 4end 5return a 6end 7 8function table . clone ( a ) 9local ret ={} 10for k , v in pairs ( a ) do 11ret [ k ] =type ( v ) == "table" and table . clone ( v ) or v 12end 13return ret 14end 15 16local function reversedipairsiter ( t , i ) 17i =i - 1 18if i ~= 0 then 19return i , t [ i ] 20end 21end 22 23function reversedipairs ( t ) 24return reversedipairsiter , t , # t + 1 25end 26 27function table . hasValue ( tbl , val ) 28for key , value in pairs ( tbl ) do 29if ( value == val ) then return true , key end 30end 31return false 32end 33 34do 35local recursiveTableString 36local stack ={} 37local cache ={} 38 39local KEY_TYPE_DOT =1 40local KEY_TYPE_BRACKET =2 41 42local function val_to_str ( v , name ) 43if type ( v ) == "string" then 44v =string . gsub ( v , "\n" , "\\n" ) 45if string . match ( string . gsub ( v , "[^'\"]" , "" ), '^"+$' ) then 46return "'" .. v .. "'" 47end 48return '"' .. string . gsub ( v , '"' , '\\"' ) .. '"' 49elseif type ( v ) == "table" then 50return recursiveTableString ( v , name ) 51else 52return tostring ( v ) 53end 54end 55 56local function key_type ( k ) 57if type ( k ) == "string" and string . match ( k , "^[_%a][_%a%d]*$" ) then 58return KEY_TYPE_DOT 59else 60return KEY_TYPE_BRACKET 61end 62end 63 64local function key_to_str ( k , name ) 65if key_type ( k ) == KEY_TYPE_DOT then 66return k 67else 68return "[" .. val_to_str ( k , name ) .. "]" 69end 70end 71 72local function getStackName () 73local result ="" 74for pos , name in ipairs ( stack ) do 75if key_type ( name ) == KEY_TYPE_DOT and pos > 1 then 76result =result .. "." .. key_to_str ( name ) 77else 78result =result .. key_to_str ( name ) 79end 80end 81return result 82end 83 84local function getTabs () 85return string . rep ( "\t" , # stack ) 86end 87 88recursiveTableString =function ( tbl , name ) 89name =name or "self" 90 91local result ="{\n" 92 93if cache [ tbl ] then 94return cache [ tbl ] 95end 96 97table . insert ( stack , name ) 98cache [ tbl ] =getStackName () 99 100for k , v in pairs ( tbl ) do 101result =result .. getTabs () .. key_to_str ( k , k ) .. " = " .. val_to_str ( v , k ) .. "\n" 102end 103table . remove ( stack , # stack ) 104 105return result .. getTabs ( stack ) .. "}" 106end 107 108function table . tostring ( tbl ) 109stack ={} 110cache ={} 111return recursiveTableString ( tbl ) 112end 113 114function table . ( tbl ) 115( table . tostring ( tbl )) 116end 117end 118 119--[[local test = { 1201, 2, 5, 121["Kappa 1 2 3 \n lul"] = true, 122data = { 1231337.84239, 124test = { 1257, 8, "NINE" 126} 127} 128} 129 130table.insert(test.data, test) 131test.data.test[3] = test 132test[58] = test.data.test 133 134print(table.tostring(test))]] 135 136function table . copy ( object ) 137local lookup_table ={} 138local function _copy ( object ) 139if type ( object ) ~= "table" then 140return object 141elseif lookup_table [ object ] then 142return lookup_table [ object ] 143end 144local new_table ={} 145lookup_table [ object ] =new_table 146for index , value in pairs ( object ) do 147new_table [ _copy ( index )] =_copy ( value ) 148end 149return setmetatable ( new_table , getmetatable ( object )) 150end 151return _copy ( object ) 152end 153 154function table . concatNumberedList ( tbl , sep , oxford ) 155if type ( tbl ) ~= "table" then 156return tostring ( tbl ) 157end 158 159local str ="" 160local num =# tbl 161for i =1 , num do 162if i < num then 163-- If we aren't at the end of the list, use commas 164-- If we're one before the end of the list, use an oxford comma if we want it 165str =str .. i .. ". " .. tbl [ i ] .. (( oxford or i < num - 1 ) and ", " or " " ) 166elseif i > 1 then 167-- If we're the last item and we have more than 1 item already.. 168-- Seperate using our defined seperator word 169str =str .. ( sep or "and" ) .. " " .. i .. ". " .. tbl [ i ] 170else 171-- Single item.. 172str =i .. ". " .. tbl [ i ] 173end 174end 175return str 176end 177 178function table . concatList ( tbl , sep , oxford ) 179if type ( tbl ) ~= "table" then 180return tostring ( tbl ) 181end 182 183local str ="" 184local num =# tbl 185for i =1 , num do 186if i < num then 187-- If we aren't at the end of the list, use commas 188-- If we're one before the end of the list, use an oxford comma if we want it 189str =str .. tbl [ i ] .. (( oxford or i < num - 1 ) and ", " or " " ) 190elseif i > 1 then 191-- If we're the last item and we have more than 1 item already.. 192-- Seperate using our defined seperator word 193str =str .. ( sep or "and" ) .. " " .. tbl [ i ] 194else 195-- Single item.. 196-- Don't even bother concating the string 197str =tbl [ i ] 198end 199end 200return str 201end 202 203function table . max ( tbl ) 204local max_key =nil 205local max_value =- math . huge 206for key , value in pairs ( tbl ) do 207value =assert ( tonumber ( value ), "expected a table full of numbers" ) 208if value > max_value then 209max_key =key 210max_value =value 211end 212end 213return max_key , max_value 214end 215 216function table . min ( tbl ) 217local min_key =nil 218local min_value =math . huge 219for key , value in pairs ( tbl ) do 220value =assert ( tonumber ( value ), "expected a table full of numbers" ) 221if value < min_value then 222min_key =key 223min_value =value 224end 225end 226return min_key , min_value 227end 228 229function table . avg ( tbl ) 230local num =0 231local total =0 232for key , value in pairs ( tbl ) do 233value =assert ( tonumber ( value ), "expected a table full of numbers" ) 234total =total + value 235num =num + 1 236end 237return total / num 238end 239 240function table . sum ( tbl ) 241local total =0 242for key , value in pairs ( tbl ) do 243value =assert ( tonumber ( value ), "expected a table full of numbers" ) 244total =total + value 245end 246return total 247end 248 249function table . keys ( tbl ) 250local keys ={} 251for k , v in pairs ( tbl ) do 252table . insert ( keys , k ) 253end 254return keys 255end 256 257function table . random ( tbl ) 258return tbl [ math . random ( 1 , # tbl )] 259end 260 261function table . randomkey ( tbl ) 262local keys =table . keys ( tbl ) 263return keys [ math . random ( 1 , # keys )] 264end 265 266function table . randomkeyvalue ( tbl ) 267local key =table . randomkey ( tbl ) 268return key , tbl [ key ] 269end 270 271function table . shuffle ( tbl ) 272for i =# tbl , 2 , - 1 do 273local j =math . random ( i ) 274tbl [ i ], tbl [ j ] =tbl [ j ], tbl [ i ] 275end 276end