Toggle preferences menu
Открыть персональное меню
Вы не представились системе
Your IP address will be publicly visible if you make any edits.
⁣ ⁣ ⁣Господь есть Дух; а где Дух Господень, там свобода. 2Кор 3:17
Cтать автором⌋ ⌈Войти в систему⁣⌋ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣

Избранная статья: Святая месса

Версия от 15:28, 2 марта 2025; SystemAdmin (обсуждение | вклад) (Новая страница: «local libraryUtil = require('libraryUtil') local checkType = libraryUtil.checkType local checkTypeMulti = libraryUtil.checkTypeMulti local p = {} --- Escape pattern for regex --- @param s string string to escape --- @return string local function escapePattern(s) return s:gsub("%W", "%%%1") end --- Check if table contains value, return key if match --- --- @param t table - Table to search --- @param val any - Value to match --- @return number|nil funct...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

Сopyright Content is available under Creative Commons Attribution-ShareAlike 4.0 License unless otherwise noted. https://tolkiengateway.net/wiki/Module:Common


local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti

local p = {}


--- Escape pattern for regex

--- @param s string string to escape
--- @return string
local function escapePattern(s)
	return s:gsub("%W", "%%%1")
end


--- Check if table contains value, return key if match
---
--- @param t table - Table to search
--- @param val any - Value to match
--- @return number|nil
function p.tableContains(t, val)
	checkType('Module:Common.tableContains', 1, t, 'table')
	for k, v in pairs(t) do
		if v == val then
			return k
		end
	end
end


--- Check if table contains key, return value if match
---
--- @param t table - Table to search
--- @param key string|number - Key to match
--- @return number|nil
function p.tableContainsKey(t, key)
	checkType('Module:Common.tableContainsKey', 1, t, 'table')
	checkTypeMulti('Module:Common.tableContainsKey', 2, key, {'string', 'number'})
	for k, v in pairs(t) do
		if k == key then
			return v
		end
	end
end


--- Split string by delimiter and return the table of string parts
---
--- @param str string String to split
--- @param delimiter string Delimiter used to split the string, default to %s
--- @return table
function p.splitStringIntoTable( str, delimiter )
        if delimiter == nil then
                delimiter = "%s"
        end
        local t = {}
        local pattern = '[^' .. escapePattern( delimiter ) .. ']+'
        for s in string.gmatch( str, pattern ) do
                table.insert( t, s )
        end
        return t
end


--- Returns a table containing the numbers of the arguments that exist
--- for the specified prefix. For example, if the prefix was 'data', and
--- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
---
--- @param prefix string Prefix of the argument name
--- @param args table Table of arguments
--- @return table Table of argument numbers
function p.getArgNums(prefix, args)
	local nums = {}
	for k, v in pairs(args) do
		local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
		if num then table.insert(nums, tonumber(num)) end
	end
	table.sort(nums)
	return nums
end

return p