Difference between revisions of "Module:LexiSorting"

From Istaria Lexica

 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[
 
--[[
LexiSorting is a set of functions which can be used to sort stuff.
+
  A set of functions which can be used to sort stuff.
 
]]
 
]]
  
Line 7: Line 7:
 
local function anyTypesComparator( a, b )
 
local function anyTypesComparator( a, b )
 
     local type1, type2 = type(a),    type(b)
 
     local type1, type2 = type(a),    type(b)
     local num1, num2  = tonumber(a), tonumber(b)
+
     local num1, num2  = tonumber(a), tonumber(b)
 
      
 
      
 
     if ( num1 ~= nil) and (num2 ~= nil) then    -- Number or numeric string
 
     if ( num1 ~= nil) and (num2 ~= nil) then    -- Number or numeric string
Line 24: Line 24:
  
 
--[[
 
--[[
Can be used to sort a string of values. You can provide a delimiter (defaults to whitespace)
+
  Basic trimming function. Removes spaces from start and end of a string
 +
]]
 +
function p.trim(s)
 +
  return (s:gsub("^%s*(.-)%s*$", "%1"))
 +
end
  
frame = the MediaWiki's frame table
+
--[[
 +
  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
  
Returns the sorted string or an empty one if no string was provided
+
  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.sortnatural( frame )
+
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.
Line 36: Line 56:
 
   local input = frame.args[1]
 
   local input = frame.args[1]
 
   local delimiter = frame.args[2]
 
   local delimiter = frame.args[2]
 +
  local replaceDelimiterWith = frame.args[3]
 +
 
 
   local t={}
 
   local t={}
 
   local s=""
 
   local s=""
 
    
 
    
   if input == nil or input == "" then return "" end -- nothing to do
+
  --let's verify if we got all needed arguments
 
+
   if input == nil or input == "" then return "" end
   if delimiter==nil then
+
   if delimiter == nil or delimiter == "" then return "" end
    delimiter="%s"
+
   if replaceDelimiterWith == nil or replaceDelimiterWith == "" then return "" end
   end
+
 
 
 
 
   for str in string.gmatch(input,"([^" ..delimiter.. "]+)") do
 
   for str in string.gmatch(input,"([^" ..delimiter.. "]+)") do
     table.insert(t, str)
+
     table.insert(t, p.trim(str))
 
   end
 
   end
 
    
 
    
Line 53: Line 74:
 
   for i,v in ipairs(t) do
 
   for i,v in ipairs(t) do
 
     if s ~= "" then
 
     if s ~= "" then
      if(delimiter=="%s") then delimiter=" " end
+
       s=s..replaceDelimiterWith..v
       s=s..delimiter..v
 
 
     else
 
     else
 
       s=v
 
       s=v
Line 60: Line 80:
 
   end
 
   end
 
   
 
   
 +
  return p.trim(s)
 +
end
 +
 +
--[[
 +
  Checks a separated list of string to be unique and removes duplicates. Keep in mind that strings will be trimmed!
 +
 
 +
  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 delimiter to split up the new values
 +
 
 +
  Returns:
 +
  Incase of a script error or invalid call, returns an empty string. Else the parsed string.
 +
]]
 +
function p.unique( frame )
 +
  local s = ""
 +
 
 +
  if frame == nil or frame == "" then return s end --nothing to do
 +
 
 +
  local input = frame.args[1]
 +
  local delimiter = frame.args[2]
 +
  local replaceDelimiterWith  = frame.args[3]
 +
  local t = {}
 +
 
 +
  if input == nil or input == "" then return s end
 +
  if delimiter == nil or delimiter == "" then return s end
 +
  if replaceDelimiterWith == nil or replaceDelimiterWith == "" then return s end
 +
   
 +
  for str in string.gmatch(input,"([^" ..delimiter.. "]+)") do
 +
    t[p.trim(str)] = true
 +
  end
 +
 
 +
  for i,v in pairs(t) do
 +
      s=s..replaceDelimiterWith..i
 +
  end
 +
 
   return s
 
   return s
 
end
 
end
  
 
return p
 
return p

Latest revision as of 12:52, 29 March 2024

Documentation for this module may be created at Module:LexiSorting/doc

--[[
  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 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
      s=s..replaceDelimiterWith..v
    else
      s=v
    end
  end
 
  return p.trim(s)
end

--[[
  Checks a separated list of string to be unique and removes duplicates. Keep in mind that strings will be trimmed!
  
  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 delimiter to split up the new values
  
  Returns:
  Incase of a script error or invalid call, returns an empty string. Else the parsed string.
]]
function p.unique( frame )
  local s = ""
  
  if frame == nil or frame == "" then return s end --nothing to do
  
  local input = frame.args[1]
  local delimiter = frame.args[2]
  local replaceDelimiterWith  = frame.args[3]
  local t = {}
  
  if input == nil or input == "" then return s end
  if delimiter == nil or delimiter == "" then return s end
  if replaceDelimiterWith == nil or replaceDelimiterWith == "" then return s end
    
  for str in string.gmatch(input,"([^" ..delimiter.. "]+)") do
    t[p.trim(str)] = true
  end
  
  for i,v in pairs(t) do
      s=s..replaceDelimiterWith..i
  end

  return s
end

return p