コンテンツにスキップ

モジュール:Util

提供: Undertale Wiki

このモジュールについての説明文ページを モジュール:Util/doc に作成できます

--- Utility methods used across other modules.
--  @module             util
--  @author             [[User:KockaAdmiralac|KockaAdmiralac]]
--  <nowiki>
require('strict')
local util = {}

--  Package variables.
local title = mw.title.getCurrentTitle()

--- Adds a category into a specified string (represented as a table).
--  Only works if the current page is in the main namespace.
--  @function           util.category
--  @param              {table} str String to insert the category in
--  @param              {table} category Category to insert
--  @param              {string|nil} sortkey The sort key for this category
function util.addCategory(str, category, sortkey)
	if sortkey == nil and title.text == category then
		sortkey = '*'
	end
	if title.namespace == 0 and title.text ~= 'Items' then
		table.insert(str, '[[Category:')
		table.insert(str, category)
		if sortkey ~= nil and sortkey ~= '' then
			table.insert(str, '|')
			table.insert(str, sortkey)
		end
		table.insert(str, ']]')
	end
end

--- Parses a comma-separated list into individual list items.
--  @function           util.parseCommaList
--  @param              {string} list Comma-separated list of items
--  @returns            {table} Array of individual items, whitespace trimmed
function util.parseCommaList(list)
	local parsedList = {}
	for _, item in ipairs(mw.text.split(list, ',', true)) do
		table.insert(parsedList, mw.text.trim(item))
	end
	return parsedList
end

--- Formats a list of lines as either a bullet point list or a single item.
--  @function           util.formatBulletList
--  @param              {table} lines Lines without bullet points
--  @returns            {string} Lines with bullet points
function util.formatBulletList(lines)
	if #lines <= 1 then
		return table.concat(lines)
	end
	return '* ' .. table.concat(lines, '\n* ')
end

return util