File size: 3,410 Bytes
d7c5875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- This file is part of SA MoonLoader package.
-- Licensed under the MIT License.
-- Copyright (c) 2016, BlastHack Team <blast.hk>

local util = require 'extensions-lite.core.util'
local tableex = {}

 -- http://lua-users.org/wiki/CopyTable
function tableex.deepcopy(object, mt)
   util.checkType("table", object)
   local lookup_table = {}
   mt = mt or false
   local function _copy(object)
         if type(object) ~= "table" then
            return object
         elseif lookup_table[object] then
            return lookup_table[object]
         end
         local new_table = {}
         lookup_table[object] = new_table
         for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
         end
         return mt and setmetatable(new_table, getmetatable(object)) or new_table
   end
   return _copy(object)
end

function tableex.copy(object, mt)
   util.checkType("table", object)
   mt = mt or false
   local newt = {}
   for k, v in pairs(object) do
      newt[k] = v
   end
   return mt and setmetatable(newt, getmetatable(object)) or newt
end

function tableex.contains(object, value)
   util.checkType("table", object, "string", value)
   for k, v in pairs(object) do
      if v == value then
         return true
      end
   end
   return false
end

function tableex.merge(...)
   local len = select('#', ...)
   assert(len > 1, "impossible to merge less than two tables")
   util.checkGenType("table", ...)
   local newTable = {}
   for i = 1, len do
      local t = select(i, ...)
      for k, v in pairs(t) do
         table.insert(newTable, v)
      end
   end
   return newTable
end

function tableex.assocMerge(...)
   local len = select('#', ...)
   assert(len > 1, "impossible to merge less than two tables")
   util.checkGenType("table", ...)
   local newTable = {}
   for i = 1, len do
      local t = select(i, ...)
      for k, v in pairs(t) do
         newTable[k] = v
      end
   end
   return newTable
end

function tableex.map(object, func) -- lume
   util.checkType("table", object, "function", func)
   local newTable = {}
   for k, v in pairs(object) do
      if type(v) == "table" then
         newTable[k] = tableex.map(v, func)
      else
         newTable[k] = func(v)
      end
   end
   return newTable
end

function tableex.transform(object, func)
   util.checkType("table", object, "function", func)
   for k, v in pairs(object) do
      if type(v) == "table" then
         object[k] = tableex.transform(v, func)
      else
         object[k] = func(v)
      end
   end
   return object
end

function tableex.invert(object) -- lume
   util.checkType("table", object)
   local newTable = {}
   for k, v in pairs(object) do
      newTable[v] = k
   end
   return newTable
 end

 function tableex.keys(object) -- lume
   util.checkType("table", object)
   local newTable = {}
   local i = 0
   for k in pairs(object) do
      i = i + 1
      newTable[i] = k
   end
   return newTable
 end

 function tableex.getIndexOf(object, value)
   util.checkType("table", object, "string", value)
   for k, v in pairs(object) do
      if v == value then
         return k
      end
   end
   return nil
end

function tableex.removeByValue(object, value)
   util.checkType("table", object)
   local getIndexOf = tableex.getIndexOf(object, value)
   if getIndexOf then
      object[getIndexOf] = nil
   end
   return getIndexOf
end

return tableex