Modul:SK/Helper: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
mNema sažetka uređivanja
mNema sažetka uređivanja
Redak 3: Redak 3:
-- Pomoćna funkcija za čišćenje unosa
-- Pomoćna funkcija za čišćenje unosa
local function clean(val)
local function clean(val)
     if type(val) == 'table' then val = val[1] end -- OVO rješava "got table" grešku
     if type(val) == 'string' then val = val:match("^%s*(.-)%s*$") end -- RUČNI TRIM
     if type(val) == 'string' then val = val:gsub('%s+', '') end
     if type(val) == 'table' then val = val[1] end
     return tonumber(val)
     -- ostatak koda...
end
end



Inačica od 26. ožujak 2026. u 12:42

Script error: The function "nonexistent" does not exist.

local h = {}

-- Pomoćna funkcija za čišćenje unosa
local function clean(val)
    if type(val) == 'string' then val = val:match("^%s*(.-)%s*$") end -- RUČNI TRIM
    if type(val) == 'table' then val = val[1] end
    -- ostatak koda...
end


-- Nova funkcija za obradu naziva/teksta
function h.text(t)
    if type(t) == 'table' then t = t[1] or t.v or "" end -- Izvlači string iz tablice
    return tostring(t)
end

-- Format broja s tisućicama (razmak)
function h.num(n)
    local num = clean(n)
    if not num then return "" end
    
    local s = tostring(math.floor(num))
    local i, j, minus, int, fraction = s:find('^([-+]?)(%d+)([.]?%d*)$')
    int = int:reverse():gsub('(%d%d%d)', '%1 '):reverse():gsub('^ ', '')
    return minus .. int .. fraction
end

-- Format površine
function h.area(a)
    local num = clean(a)
    if not num then return "" end
    return string.format("%.2f", num):gsub("%.", ",") .. " km²"
end

-- Format gustoće
function h.density(d)
    local num = clean(d)
    if not num then return "" end
    return string.format("%.1f", num):gsub("%.", ",") .. " st./km²"
end

-- Format stanovništva
function h.pop(p)
    return h.num(p)
end

return h