Module:UtilsString

local p = {} local h = {}

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

function p.notEmpty(str) return not p.isEmpty(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.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

-- Source: https://phabricator.wikimedia.org/diffusion/ELUA/browse/master/includes/engines/LuaCommon/lualib/mw.text.lua function p.split(text, pattern, plain) local ret = {} for m in h.gsplit( text, pattern, plain ) do		ret[#ret+1] = m	end return ret end function h.gsplit(text, pattern, plain) if not pattern then pattern = '%s*,%s*' end local s, l = 1, text:len return function if s then local e, n = text:find( pattern, s, plain ) local ret if not e then ret = text:sub( s ) s = nil elseif n < e then -- Empty separator! ret = text:sub( s, e ) if e < l then s = e + 1 else s = nil end else ret = e > s and text:sub( s, e - 1 ) or '' s = n + 1 end return ret end end, nil, nil end

-- Source: https://phabricator.wikimedia.org/diffusion/ELUA/browse/master/includes/engines/LuaCommon/lualib/mw.text.lua function p.trim(s, charset) return p._trim(charset)(s) end function p._trim(charset) charset = charset or '\t\r\n\f ' return function(s) s = s:gsub( '^[' .. charset .. ']*(.-)[' .. charset .. ']*$', '%1' ) return s	end 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 = { 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", },	},	trim = { pattern = { type = "string", },		str = { type = "string", required = true, },	},	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 = "notEmpty", params = {"str"}, returns = " if and only if   is neither nil nor an empty string.", cases = { {				args = {" "}, expect = true, },			{				args = {""}, expect = false, },			{				args = {nil}, 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 = "trim", desc = "Equivalent of except that it does not operate on Unicode strings.", params = {"str", "pattern"}, _params = {{"pattern"}, {"str"}}, returns = "The trimmed string.", cases = { outputOnly = true, {				args = {" foo"}, expect = "foo", },			{				args = {":Category:Link", ":"}, expect = "Category:Link", },		},	},	{		name = "split", desc = "Equivalent of except that it does not operate on Unicode strings.", 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 = "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