Module:US elections imagemap/utils

< Module:US elections imagemap
Revision as of 07:44, 16 July 2021 by Zoran (talk | contribs) (Pywikibot 6.4.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Module rating

Usage

String utilities for Module:US elections imagemap.

  • stripspaces(str) strips spaces from str.
  • split(str, sep) split str into a table of strings at each sep.

Unlike many other string utilities on Wikipedia, these are designed to be called from another Lua script easily — not to be called directly with {{#invoke}}.


local p = {}

function p.stripspaces(str)
	return str:gsub("^%s*(.-)%s*$", "%1") -- trim spaces
end

function p.split(str, sep) -- split string by separator and return table
  sep = sep or "%s"
  local parts = {}
  for word in string.gmatch(str, "[^" .. sep .. "]+") do parts[#parts+1] = p.stripspaces(word) end
  return parts
end

return p