Advertisement
eniallator

Deep Copy

Sep 20th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.01 KB | None | 0 0
  1. local function validator(args)
  2.     for i = 1, #args do
  3.         if type(args[i]) ~= 'table' then
  4.             return false
  5.         end
  6.     end
  7.     return true
  8. end
  9.  
  10. local function getInputTbls(args, key)
  11.     local inputTbls = {}
  12.  
  13.     for i=1, #args do
  14.         local nextVal = args[i][key]
  15.         if nextVal and type(nextVal) == 'table' then
  16.             table.insert(inputTbls, nextVal)
  17.         end
  18.     end
  19.  
  20.     return inputTbls
  21. end
  22.  
  23. local function deepCopy(...)
  24.     local args = {...}
  25.     if not validator(args) then
  26.         return {}
  27.     end
  28.  
  29.     local newTbl = {}
  30.  
  31.     for i = #args, 1, -1 do
  32.         for key, val in pairs(args[i]) do
  33.             if not newTbl[key] then
  34.                 local inputTbls = getInputTbls(args, key)
  35.                 if type(val) == 'table' then
  36.                     newTbl[key] = deepCopy(unpack(inputTbls))
  37.                 else
  38.                     newTbl[key] = val
  39.                 end
  40.             end
  41.         end
  42.     end
  43.  
  44.     return newTbl
  45. end
  46.  
  47. return deepCopy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement