Module:Func

From Secrets of Grindea Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Func/doc

local p = {}

function p.has_value(tab, val)
  if tab == nil then
    return false
  end
  for index, value in pairs(tab) do
    if value == val then
      return true
    end
  end
  return false
end

function p.has_value_ipairs(tab, val)
  if tab == nil then
    return false
  end
  for index, value in ipairs(tab) do
    if value == val then
      return true
    end
  end
  return false
end

function p.is_empty(s)
  if s == nil then
    return true
  elseif mw.text.trim(s) == "" then
    return true
  else
    return false
  end
end

function p.nil_if_empty(s)
  if s == nil then
    return nil
  elseif mw.text.trim(s) == "" then
    return nil
  else
    return s
  end
end

function p.limit_range(left, right, num)
  if left ~= nil and num < left then
    num = left
  elseif right ~= nil and num > right then
    num = right
  end
  return num
end

function p.choice(cond, valTrue, valFalse)
  if cond then
    return valTrue
  else
    return valFalse
  end
end

return p