Difference between revisions of "Module:LexiSorting"
From Istaria Lexica
Line 23: | Line 23: | ||
end | end | ||
+ | --[[ | ||
+ | Basic trimming function. Removes spaces from the start and end of a string | ||
+ | ]] | ||
function p.trim(s) | function p.trim(s) | ||
return (s:gsub("^%s*(.-)%s*$", "%1")) | return (s:gsub("^%s*(.-)%s*$", "%1")) | ||
Line 28: | Line 31: | ||
--[[ | --[[ | ||
− | + | Sorts a token separated list. | |
− | + | ||
− | + | Keep in mind that the values are trimmed. | |
+ | Example: | ||
+ | Jungle Crawler Breeder Abdomen, Insect Ichor, Insect Jaw, Jungle Crawler Thorax, Spider Silk, Venom Gland | ||
+ | ..ends up in.. | ||
+ | Insect Ichor,Insect Jaw,Jungle Crawler Breeder Abdomen,Jungle Crawler Thorax,Spider Silk,Venom Gland | ||
− | + | Arguments: | |
+ | frame = the MediaWiki's frame table | ||
+ | |||
+ | MediaWiki arguments: | ||
+ | arg1: The string to be parsed | ||
+ | arg2: The token to split up the values | ||
+ | arg3: The token to be entered to the values after sort is done | ||
+ | |||
+ | Returns: | ||
+ | Incase of a script error or invalid call, returns an empty string. Else the parsed string. | ||
]] | ]] | ||
− | function p. | + | function p.sortseparatedstring( frame ) |
if frame == nil or frame == "" then return "" end -- frame is not given, this is an error. Return a blank string to avoid script errors. | if frame == nil or frame == "" then return "" end -- frame is not given, this is an error. Return a blank string to avoid script errors. |
Revision as of 04:10, 16 March 2024
Documentation for this module may be created at Module:LexiSorting/doc
--[[ LexiSorting is a set of functions which can be used to sort stuff. ]] local p={} local function anyTypesComparator( a, b ) local type1, type2 = type(a), type(b) local num1, num2 = tonumber(a), tonumber(b) if ( num1 ~= nil) and (num2 ~= nil) then -- Number or numeric string return num1 < num2 -- Numeric compare elseif type1 ~= type2 then -- Different types return type1 < type2 -- From here on, types are known to match elseif type1 == "string" then -- Non-numeric string return string.lower(a)< string.lower(b) -- Case-insensitive compare elseif type1 == "boolean" then return a and not b -- Booleans else -- What's left: function, table, thread, userdata, ? return string.lower(tostring(a)) < string.lower(tostring(b)) -- String representation compare, case-insensitive end end --[[ Basic trimming function. Removes spaces from the start and end of a string ]] function p.trim(s) return (s:gsub("^%s*(.-)%s*$", "%1")) end --[[ Sorts a token separated list. Keep in mind that the values are trimmed. Example: Jungle Crawler Breeder Abdomen, Insect Ichor, Insect Jaw, Jungle Crawler Thorax, Spider Silk, Venom Gland ..ends up in.. Insect Ichor,Insect Jaw,Jungle Crawler Breeder Abdomen,Jungle Crawler Thorax,Spider Silk,Venom Gland Arguments: frame = the MediaWiki's frame table MediaWiki arguments: arg1: The string to be parsed arg2: The token to split up the values arg3: The token to be entered to the values after sort is done Returns: Incase of a script error or invalid call, returns an empty string. Else the parsed string. ]] function p.sortseparatedstring( frame ) if frame == nil or frame == "" then return "" end -- frame is not given, this is an error. Return a blank string to avoid script errors. local input = frame.args[1] local delimiter = frame.args[2] local replaceDelimiterWith = frame.args[3] local t={} local s="" --let's verify if we got all needed arguments if input == nil or input == "" then return "" end if delimiter == nil or delimiter == "" then return "" end if replaceDelimiterWith == nil or replaceDelimiterWith == "" then return "" end for str in string.gmatch(input,"([^" ..delimiter.. "]+)") do table.insert(t, p.trim(str)) end table.sort(t,anyTypesComparator) for i,v in ipairs(t) do if s ~= "" then if(replaceDelimiterWith=="%s") then replaceDelimiterWith=" " end s=s..replaceDelimiterWith..v else s=v end end return p.trim(s) end return p