Module:Sandbox/Lakelimbo/Utils

Documentation for this module may be created at Module:Sandbox/Lakelimbo/Utils/doc

-- (c) 2024 Lakelimbo <[email protected]>
-- This code is licensed under Creative Commons BY-NC-SA 2.5
--[[
    This file will contain a bunch of utility functions (among other things)
    that is not specific to any module.
]]

local util = {}

--- Takes a set of items and checks if any of them match the comparator.
--- @param comparator string | number | table
--- @param items table
--- @return boolean
function util.is_any(comparator, items)
    for item in pairs(items) do
        if items[item] == comparator then
            return true
        end
    end

    return false
end

--- Trims and lowercases a string
--- @param str string
--- @return string
function util.trim_lower(str)
    if str then
        return mw.text.trim(mw.ustring.lower(str))
    end

    return str
end

--- Common fractions
--- @param number number
--- @return string | number
function util.fraction(number, frame)
    if frame then
        number = frame:getParent().args[1]
    end

    local fractions = {
        [0.75] = "¾",
        [0.5] = "½",
        [0.25] = "¼",
    }

    if fractions[number] then
        return fractions[number]
    end

    return number
end

return util