prosody-modules/mod_mix/helpers.lib.lua

24 lines
653 B
Lua

-- Helper functions for mod_mix
local function find(array, f)
-- Searches for an element for which f returns true. The first element
-- and its index are returned. If none are found, then -1, nil is returned.
--
-- f is a function that takes the value and returns either true or false.
for i, v in pairs(array) do
if f(v) then return i, v; end
end
return -1, nil;
end
local function find_str(array, str)
-- Returns the index of str in array. -1 if array does not contain str
local i, _ = find(array, function(v) return v == str end);
return i;
end
return {
find_str = find_str,
find = find,
};