Merge 0.10->trunk
[prosody.git] / util / multitable.lua
index 299eecb13f73b15b1ab25881bd72a798239b6f03..e4321d3d34e120e8740b1db80278cabdbbe93e3d 100644 (file)
@@ -1,16 +1,17 @@
 -- Prosody IM
 -- Copyright (C) 2008-2010 Matthew Wild
 -- Copyright (C) 2008-2010 Waqas Hussain
--- 
+--
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
 --
 
 local select = select;
 local t_insert = table.insert;
-local unpack, pairs, next, type = unpack, pairs, next, type;
+local pairs, next, type = pairs, next, type;
+local unpack = table.unpack or unpack; --luacheck: ignore 113
 
-module "multitable"
+local _ENV = nil;
 
 local function get(self, ...)
        local t = self.data;
@@ -126,7 +127,7 @@ local function search_add(self, results, ...)
        return results;
 end
 
-function iter(self, ...)
+local function iter(self, ...)
        local query = { ... };
        local maxdepth = select("#", ...);
        local stack = { self.data };
@@ -144,23 +145,24 @@ function iter(self, ...)
                        keys[depth] = key;
                end
                local value = stack[depth][key];
-               if depth == maxdepth then -- Result
-                       local result = {}; -- Collect keys forming path to result
-                       for i = 1, depth do
-                               result[i] = keys[i];
-                       end
-                       return unpack(result, 1, depth);
-               else
-                       if (query[depth] == nil or key == query[depth]) and type(value) == "table" then -- Descend
-                               t_insert(stack, value);
+               if query[depth] == nil or key == query[depth] then
+                       if depth == maxdepth then -- Result
+                               local result = {}; -- Collect keys forming path to result
+                               for i = 1, depth do
+                                       result[i] = keys[i];
+                               end
+                               result[depth+1] = value;
+                               return unpack(result, 1, depth+1);
+                       elseif type(value) == "table" then
+                               t_insert(stack, value); -- Descend
                        end
-                       return it(self);
                end
+               return it(self);
        end;
        return it, self;
 end
 
-function new()
+local function new()
        return {
                data = {};
                get = get;
@@ -173,4 +175,7 @@ function new()
        };
 end
 
-return _M;
+return {
+       iter = iter;
+       new = new;
+};