Modul:Hêlkêşan

Ji Wîkîpediya, ensîklopediya azad.
Belgekirina modulê[biafirîne]
local z = {}

function z.tostring(tabla, identacion)
    identacion = identacion or '\n'
    local resultado = ''
    
    if not tabla then
    	return
    end
 
    for k,v in pairs(tabla) do
    	if type(k) == 'string' then
    		k2='"' .. k .. '"'
    	else
    		k2=k
    	end
        if type(v)=='table' then
            resultado = resultado .. identacion .. k2 .. ': {' .. z.tostring(v,identacion .. '  ') .. identacion .. '}'
        elseif type(v)=='string' then
        	resultado = resultado .. identacion .. k2 .. ': "' .. v .. '"'
        else
        	resultado = resultado .. identacion .. k2 .. ': ' .. tostring(v)
        end
    end
 
    return resultado
end

function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6, indice7)
	local resultado
	
	if not tabla or not indice1 then
		return
	end
	
	resultado = tabla[indice1]
	
	if not indice2 or not resultado then
		return resultado
	end
	
	resultado = resultado[indice2]

	if not indice3 or not resultado then
		return resultado
	end
	
	resultado = resultado[indice3]
	
	if not indice4 or not resultado then
		return resultado
	end
	
	resultado = resultado[indice4]
	
	if not indice5 or not resultado then
		return resultado
	end
	
	resultado = resultado[indice5]
	
	if not indice6 or not resultado then
		return resultado
	end
	
	resultado = resultado[indice6]
	
	if not indice7 or not resultado then
		return resultado
	end
	
	resultado = resultado[indice7]
	
	return resultado
end

function z.en(tabla, elemento)
    if not elemento then
        return
    end
    for k,v in pairs( tabla ) do
        if v == elemento then
            return k
        end
    end
end

function z.copiarElementosConValor(original)
	local copia= {}
	
    for k,v in pairs(original) do
        if v~='' then
            copia[k] = original[k]
        end
    end	
    
    return copia
end

function z.insertar(tabla, elemento)
	if not elemento then
		return false
	end

	if not z.en(tabla, elemento) then
		table.insert(tabla, elemento)	
	end
	
	return true
end

function z.insertarElementosConValor(origen, destino)
    for k,v in pairs(origen) do
        if v~='' then
            table.insert(destino, v)
        end
    end	
    
    return copia
end

function z.sonIguales(tabla1, tabla2)
	if not tabla1 or not tabla2 then
		return false
	end
	
	if tabla1 == tabla2 then
		return true
	end
	
	for k,v in pairs(tabla1) do
		if tabla2[k] ~= v then
			return false
		end
	end

	for k,v in pairs(tabla2) do
		if not tabla1[k] then
			return false
		end
	end

	return true
end

function z.ordenarFuncion(tabla, funcion)
	local funcionInestable = funcion
	
	-- Añadir a la tabla un campo con el orden
	for i,n in ipairs(tabla) do tabla[i].orden = i end
	
	table.sort(tabla, 
		function(a,b) 
		   if     funcionInestable(a, b) then return true -- a < b
		   elseif funcionInestable(b, a) then return false -- b < a
		   elseif a.orden <= b.orden     then return true -- a = b y a aparece antes que b
		   else                               return false -- a = b y b aparece antes que a
		   end
	    end)
end

function z.ordenar(tabla, criterio)
	if type(criterio) == 'table' then
		z.ordenarFuncion(tabla,
			function(a,b)
				for i,campo in ipairs(criterio) do
					if a[campo] < b[campo] then return true
					elseif a[campo] > b[campo] then return false 
					end
				end	
		   		return false -- Todos los valores son iguales
			end
		)	
	else
		z.ordenarFuncion(tabla, criterio)
	end
end
 
return z