dev
Documentation icon Module documentation
[view] [edit] [history] [purge]

Docbunto generic types test case

Documentation

Package items

text.trim(str) (function)
Docbunto string trim case
Parameter: str String to trim whitespace from. (string)
Returns: Trimmed value of str. (string)
text.split(str, delimiter, isPattern) (function)
Docbunto split test case
Parameters:
  • str String to split. (string)
  • delimiter The substring used by the split operation. (string)
  • isPattern Whether the delimiter is a pattern. (boolean; optional)
Returns: List of strings split by delimiter. (sequence<string>)

-- <nowiki>
--------------------------------------------------------------------------------
-- Docbunto generic types test case
--
-- @script text
-- @author [[User:ExE Boss|ExE Boss]]
--------------------------------------------------------------------------------
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType

local text = {}

--------------------------------------------------------------------------------
-- Docbunto string trim case
--
-- @function    text.trim
-- @param       {string} str String to trim whitespace from.
-- @return      {string} Trimmed value of @{text.trim~str|str}.
--------------------------------------------------------------------------------
function text.trim(str)
   local index1, index2 = find(str, '^%s*')
   if index2 >= index1 then
      str = sub(str, index2 + 1)
   end
   local i1, i2 = find(str,'%s*$')
   if index2 >= index1 then
      str = sub(str, 1, index1 - 1)
   end
   return str
end

--------------------------------------------------------------------------------
-- Docbunto split test case
--
-- @function    text.split
-- @param       {string} str String to split.
-- @param       {string} delimiter The substring used by the split operation.
-- @param       {boolean} [isPattern] Whether the delimiter is a pattern.
-- @return      {sequence<string>} List of strings split by @{text.split~delimiter|delimiter}.
--------------------------------------------------------------------------------
function text.split(str, delimiter, isPattern)
	checkType('split', 1, str, 'string')
	checkType('split', 2, delimiter, 'string', true)
	checkType('split', 3, isPattern, 'boolean', true)

	delimiter = delimiter or ','
	local isPlain = not isPattern

	local result = {}
	local prevIndex = 1
	local startIndex, endIndex = mw.ustring.find(str, delimiter, nil, isPlain)

	while startIndex ~= nil do
		table.insert(result, mw.ustring.sub(str, prevIndex, startIndex - 1))
		prevIndex = endIndex + 1
		startIndex, endIndex = mw.ustring.find(str, delimiter, prevIndex, isPlain)
	end

	table.insert(result, mw.ustring.sub(str, prevIndex))
	return result
end

return text