From Dead Cells Wiki
Jump to: navigation, search

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

local text = require("Module:Text")

local p = {}
local h = {}

-- Checks if a string is a single decimal digit and returns it as a number.
-- If the string is anything else, throws an error.
function h.checkDigit(input)
   local number = assert(tonumber(input), "input does not contain a number")
   -- Checking if it's an integer
   assert(math.floor(number) == number, "input contains a non-integer number")
   assert(number >= 0 and number <= 9, "input contains an out-of-range integer")
   return number

function p._process(args)
   local str = args[1]
   local group = h.checkDigit(args[2])
   local result = {}
   local resultIndex = 1
   for part in text.gsplit(str, '%s*,%s*') do
   	  local digitMin, digitMax, rest = string.match(part, "^(%d)(%d)(.+)$")
   	  assert(digitMin and digitMax and rest, "incomplete part, needs to have two digits followed by text")
   	  digitMin = h.checkDigit(digitMin)
   	  digitMax = h.checkDigit(digitMax)
   	  if group >= digitMin and group <= digitMax then
   	  	 result[resultIndex] = rest
   	  	 resultIndex = resultIndex + 1
   return table.concat(result, ", ")

function p.process(frame)
   local args = require("Module:Arguments").getArgs(frame)
   if not args[1] then
   	  return ""
   return p._process(args)
return p