Module:Str endswith

From CasperTech Wiki
Revision as of 20:08, 9 December 2018 by D1cd5b71-6209-4595-9bf0-771bf689ce00 (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

-- This module implements {{str endswith}}.

local TRUE_STRING = 'yes'
local FALSE_STRING = ''

local p = {}

local function trim(s)
	return s:match('^%s*(.-)%s*$')
end

function p.main(frame)
	local args = frame:getParent().args
	local s = args[1]
	local pattern = args[2]
	if not s or not pattern then
		-- TRUE_STRING is not the natural choice here, but is needed for
		-- backwards compatibility.
		return TRUE_STRING
	end
	s = trim(s)
	pattern = trim(pattern)
	if pattern == '' then
		-- All strings end with the empty string.
		return TRUE_STRING
	end
	if mw.ustring.sub(s, 0 - mw.ustring.len(pattern), -1) == pattern then
		return TRUE_STRING
	else
		return FALSE_STRING
	end
end

return p