Module:UtilsString

local p = {}

function p.isEmpty(str) return str == nil or str == "" end

function p.isBlank(str) return str == nil or mw.text.trim(str) == "" end

function p.nilIfEmpty(str) if str and str ~= "" then return str end end

function p.format(formatString) return function(...) return string.format(formatString, ...) end end

function p.startsWith(pattern, str) return str:sub(1, #pattern) == pattern end

function p.endsWith(pattern, str) return str.match(str, pattern, #str - #pattern + 1) == pattern end

function p.split(text, pattern, plain) pattern = pattern or "%s*,%s*" return mw.text.split(text, pattern, plain) end

-- By http://lua-users.org/wiki/RiciLake function p.interpolate(formatStr, tab) return (formatStr:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end)) end

p.Schemas = { isEmpty = { str = { type = "string" }, },	isBlank = { str = { type = "string" }, },	nilIfEmpty = { str = { type = "string" }, },	startsWith = { pattern = { type = "string", required = true }, str = { type = "string", required = true }, },	endsWith = { pattern = { type = "string", required = true }, str = { type = "string", required = true }, },	split = { str = { type = "string", required = true, },		pattern = { type = "string", default = "%s*,%s*", },		plain = { type = "boolean", },	},	format = { formatStr = { type = "string", required = true, },		["..."] = {			type = "array", items = { type = "string", },			required = true, },	},	interpolate = { formatStr = { type = "string", required = true, },		args = { type = "map", required = true, keys = { type = "string" }, values = { type = "string" }, },	}, }

p.Documentation = { {		name = "isEmpty", params = {"str"}, returns = ' if and only if the value is   or  ', cases = { {				args = {nil}, expect = true, },			{				args = {""}, expect = true, },			{				args = {" "}, expect = false, },		},	},	{		name = "isBlank", params = {"str"}, returns = " if and only if   is nil, blank, or whitespace.", cases = { {				args = {" "}, expect = true, },			{				args = {"\n\n\n"}, expect = true, },			{				args = {nil}, expect = true, },			{				args = {"foo"}, expect = false, },		},	},	{		name = "nilIfEmpty", params = {"str"}, returns = " if value is nil or empty string, otherwise returns the given value.", cases = { outputOnly = true, {				args = {""}, expect = nil, },			{				args = {nil}, expect = nil, },			{				args = {" "}, expect = " ", },		},	},	{		name = "startsWith", params = {"pattern", "str"}, returns = " if and only if   starts with  .", cases = { {				args = {"Foo", "Fooloo Limpah"}, expect = true, },			{				args = {"foo", "Fooloo Limpah"}, expect = false, },			{				args = {"", "Fooloo Limpah"}, expect = true, },		},	},	{		name = "endsWith", params = {"pattern", "str"}, returns = " if and only if   ends with  .", cases = { {				args = {"Limpah", "Fooloo Limpah"}, expect = true, },			{				args = {"limpah", "Fooloo Limpah"}, expect = false, },			{				args = {"", "Fooloo Limpah"}, expect = true, },		},	},	{		name = "split", desc = ", but by default splits by comma+whitespace.", params = {"str", "pattern", "plain"}, returns = "A  of the split strings.", cases = { {				args = {" foo,   bar,baz "}, expect = {" foo", "bar", "baz "}, },			{				args = {"foo bar baz", " "}, expect = {"foo", "bar", "baz"}, },		},	},	{		name = "format", order = 2, params = {{"formatStr"}, {"..."}}, returns = "The formatted string.", cases = { outputOnly = true, {				args = {{"Tingle Tingle %s %s!"}, {"Kooloo", "Limpah"}}, expect = "Tingle Tingle Kooloo Limpah!", }		}	},	{		name = "interpolate", desc = "Approximation of string interpolation", params = {"formatStr", "args"}, returns = "The formatted string.", cases = { outputOnly = true, {				args = {"${wiki} is a ${franchise} encyclopedia that anyone can edit.", { wiki = "Zelda Wiki", franchise = "Zelda", }},				expect = "Zelda Wiki is a Zelda encyclopedia that anyone can edit." }		}	} }

return p