Модуль:Feature


local lib = {}

function lib.mergeFrames(frame, parent)
    local args = {}
    if frame then
        for k,v in pairs(frame.args) do
            args[k] = v
        end
    end
    if parent then
        for k,v in pairs(parent.args) do
            args[k] = v
        end
    end
    return args
end

function lib.arguments(origArgs)
    local args = {}
    for k, v in pairs(origArgs) do
        if type(v) == 'string' then
            v = mw.text.trim(v)
        end
        if v ~= '' then
            args[k] = v
        end
    end
    return args
end

function lib.getSortedKeys(tab)
    local keys = {}
    for k,_ in pairs(tab) do
        keys[#keys+1] = k
    end
    table.sort(keys)
    return keys
end

function lib.pairsByAlphabeticalKeys(t, f)
    local a = {}
    for n in pairs(t) do table.insert(a, n) end
        table.sort(a, f)
        local i = 0
        local iter = function ()
            i = i + 1
            if a[i] == nil then return nil
            else return a[i], t[a[i]]
        end
    end
    return iter
end

function lib.predicate(tbl, predicate)
    local result = {}
    for k, v in pairs(tbl) do
        if(predicate(v)) then
            table.insert(result, k)
        end
    end
    return result
end

function lib.groupedArguments(args, numeric)
    if numeric == nil then
        numeric = true
    end
    numeric = not not numeric
 
    local base = {}
    local groups = {}
 
    for k, v in pairs(args) do
        v = mw.text.trim(v) or ''
        if v ~= '' then
            if type(k) == 'string' then
                k = mw.text.trim(k) or ''
                if k ~= '' then
                    local split = mw.text.split(k, ':')
                    if #split == 1 then
                        base[k] = v
                    else
                        local group = mw.text.trim(split[1]) or ''
                        local key = mw.text.trim(table.concat(split, ':', 2)) or ''
                        if key ~= '' and group ~= '' then
                            if numeric then
                                group = tonumber(group)
                                if group ~= nil then
                                    if groups[group] == nil then
                                        groups[group] = {}
                                    end
                                    groups[group][key] = v
                                else
                                    base[k] = v
                                end
                            else
                                if groups[group] == nil then
                                    groups[group] = {}
                                end
                                groups[group][key] = v
                            end
                        else
                            base[k] = v
                        end
                    end
                end
            elseif v ~= '' then
                base[k] = v
            end
        end
    end
    return base, groups
end

function lib.tbl_concat(frame)
    local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end
    
    local pre = args["pre"] or args["prepend"] or ""
    local app = args["app"] or args["append"] or ""
    local sep = args["sep"] or args["separator"] or ","
    local tbl = args["tbl"] or args[1]
    local index = args["index"]
    local s = ""
    local i = 0;
    
    if index ~= nil then
    	if lib.tbl_length(tbl) > 1 then
    		for _, v in pairs(tbl[tonumber(index)]) do
    			i = i + 1;
				if i ~= lib.tbl_length(tbl[tonumber(index)]) then
					s = s .. pre .. v .. app .. sep
				else
					s = s .. pre .. v .. app
				end
			end
		else
			s = s .. tbl[tonumber(index)]
		end
    else
        for _, v in pairs(tbl) do
        	i = i + 1;
			if i ~= lib.tbl_length(tbl) then
				s = s .. pre .. v .. app .. sep
			else
				s = s .. pre .. v .. app
			end
        end
    end

    return s
end

function lib.tbl_length(tbl)
	local count = 0
	for _ in pairs(tbl) do
		count = count + 1
	end
	return count
end

function lib.tbl_debug(tbl)
    return table.tostring(tbl)
end
 
function lib.ternary(cond, T, F)
    if cond then
        return T
    else
        return F
    end
end

function lib.comma_value(value)
	local left, num, right = string.match(value,'^([^%d]*%d)(%d*)(.-)$')
	return left..(num:reverse():gsub('(%d%d%d)','%1.'):reverse())..right
end

function lib.lookup(t, ...)
    for _, k in ipairs{...} do
        t = t[k]
        if not t then
            return nil
        end
    end
    return t
end

function lib.replace(text, old, new)
	local b, e = text:find(old, 1, true)
	if b == nil then
		return text
	else
		return text:sub(1, b - 1) .. new .. text:sub(e + 1)
	end
end

function lib.pairsByKeys (t, f)
    local a = {}
    for n in pairs(t) do table.insert(a, n) end
    table.sort(a, f)
    local i = 0
    local iter = function ()
        i = i + 1
        if a[i] == nil then return nil
        else return a[i], t[a[i]]
        end
    end
    return iter
end

function lib.reverseTable(t)
	local n = #t
	local i = 1
	while i < n do
		t[i],t[n] = t[n],t[i]
		i = i + 1
		n = n - 1
	end
end

return lib