Module:Distribute

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 end

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 end end return table.concat(result, ", ") end

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