Module:User:Vitalik/inflection-tools

Hello, you have come here looking for the meaning of the word Module:User:Vitalik/inflection-tools. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:User:Vitalik/inflection-tools, but we will also tell you about its etymology, its characteristics and you will know how to say Module:User:Vitalik/inflection-tools in singular and plural. Everything you need to know about the word Module:User:Vitalik/inflection-tools you have here. The definition of the word Module:User:Vitalik/inflection-tools will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:User:Vitalik/inflection-tools, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.


local export = {}

local u = require('Module:utils')

local function trim_stress(str)
	-- remove space after stress (we put it in the code just to make visually readable sources)
	local result, count = mw.ustring.gsub(str, '́ ', '́')
	return result
end

export.stash = {}

function export.clear_stash()
	export.stash = {}
end

function export.add_stash(name, value)
	export.stash = value
end

function export.apply_stash(str)
	for name, value in pairs(export.stash) do
		str = mw.ustring.gsub(str, u.escape(name), value)
	end
	return str
end

function export.replace(str, pattern, replace_to)
	pattern = export.apply_stash(pattern)
	return mw.ustring.gsub(str, trim_stress(pattern), trim_stress(replace_to))
end

function export.extract(str, pattern)
	pattern = export.apply_stash(pattern)
	return mw.ustring.match(str, trim_stress(pattern))
end

function export.check(str, values, checker)
	if type(values) == 'string' then
		values = {values}
	end
	for i, value in pairs(values) do
		value = trim_stress(export.apply_stash(value))
		local ok = 
			   checker == 'equals'           and value == str
			or checker == 'endswith'         and mw.ustring.match(str, value .. "$") ~= nil
			or checker == 'penultimate'      and mw.ustring.match(str, value .. ".$") ~= nil
			or checker == 'contains'         and mw.ustring.match(str, value) ~= nil 
			or checker == 'contains_once'    and table.getn(mw.text.split(str, value)) == 2
			or checker == 'contains_several' and table.getn(mw.text.split(str, value)) > 2
		if ok then
			return true
		end
	end
	return false	
end

function export.equals(str, values)
	return export.check(str, values, 'equals')
end

function export.endswith(str, values)
	return export.check(str, values, 'endswith')
end

function export.penultimate(str, values)
	return export.check(str, values, 'penultimate')
end

function export.contains(str, values)
	return export.check(str, values, 'contains')
end

function export.contains_once(str, values)
	return export.check(str, values, 'contains_once')
end

function export.contains_several(str, values)
	return export.check(str, values, 'contains_several')
end

return export