Module:Util

From Official Factorio Wiki
Revision as of 11:58, 25 October 2024 by Bilka (talk | contribs) (Moved util methods to separate module from Module:Infobox)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local util = {}

-- @param arg string The argument to check
-- @param default string The fallback argument
-- @return boolean @The argument if it's non-empty, otherwise the default
function util._arg_or(arg, default)
  if util._empty_arg(arg) then
    return default
  else
    return arg
  end
end

-- @param arg string The argument to check
-- @return boolean @Whether the argument is a string containing only whitespace or empty string or nil
function util._empty_arg(arg)
  if type(arg) == "string" then
    return not string.find(arg, "%S")
  else
    return not arg
  end
end

-- @param inputstr string A string to be split
-- @param separator string The separator
-- @return string[] @The separated strings, without the separator
function util._split(inputstr, separator)
  local result = {}

  for str in string.gmatch(inputstr, "([^"..separator.."]+)") do
    table.insert(result, mw.text.trim(str))
  end

  return result
end

-- @param page_title string The title of the page
-- @return boolean
function util._page_exists(page_title)
  if page_title and page_title ~= "" then
    return mw.title.new(page_title).exists
  else
    return false
  end
end

return util