Difference between revisions of "Module:Test/lib/util"

From RimWorld Wiki
Jump to navigation Jump to search
(Created page with "local util = { _DESCRIPTION = 'an assortment of useful things', } --ref: https://gist.github.com/ripter/4270799 function util.tprint(tbl, indent) if not indent then inden...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local util = {
+
if mw then
  _DESCRIPTION = 'an assortment of useful things',
+
  Search = Search or require("Module:Test/lib/search")
}
+
else
 +
  Search = Search or require "lib/search"
 +
end
 +
 
 +
local util = {}
 +
 
 +
------------------------
 +
-- table manipulation --
 +
------------------------
  
--ref: https://gist.github.com/ripter/4270799
+
util.table = {}
function util.tprint(tbl, indent)
 
  if not indent then indent = 0 end
 
  
   if type(tbl) ~= "table" then
+
function util.table.check(tbl, ...)
    print(tbl)
+
  local inside = tbl
     return 0
+
   for i,v in pairs(arg) do
 +
    if i ~= 'n' then
 +
      inside = Search.find(v, inside or {})
 +
     end
 
   end
 
   end
 +
  return inside and true
 +
end
  
   for k, v in pairs(tbl) do
+
function util.table.checkMultiple(tbl, filters)
     formatting = string.rep("  ", indent) .. k .. ": "
+
  local checked = false
    if type(v) == "table" then
+
   for _,filter in ipairs(filters) do
      print(formatting)
+
     if util.table.check(tbl, unpack(filter)) then
      util.tprint(v, indent+1)
+
       checked = true
    elseif type(v) == 'boolean' then
 
       print(formatting .. tostring(v))
 
 
     else
 
     else
       print(formatting .. v)
+
       return false
 +
    end
 +
  end
 +
  return checked
 +
end
 +
 
 +
function util.table.isIn(var, table)
 +
  assert(type(var) == 'nil', "isIn: Empty argument #1")
 +
  assert(type(table) == 'table', string.format("isIn: Bad argument #2 ('table' expected, got '%s'", type(table)))
 +
  for k,v in pairs(table) do
 +
    if var == k or var == v then
 +
      return true
 
     end
 
     end
 
   end
 
   end
 +
  return false
 
end
 
end
  
--ref: http://lua-users.org/wiki/CopyTable
+
-- procedure
function util.shallowcopy(original_table)
+
function util.table.overwrite(dest, source, ignoreKeys)
     local orig_type = type(original_table)
+
  ignoreKeys = ignoreKeys or {}
     local copy
+
 
     if orig_type == 'table' then
+
  for sK,sV in pairs(source) do
         copy = {}
+
     local ignore = false
        for orig_key, orig_value in pairs(original_table) do
+
 
            copy[orig_key] = orig_value
+
    for _,ignoredK in ipairs(ignoreKeys) do
 +
      if sK == ignoredK then
 +
        ignore = true
 +
        break
 +
      end
 +
     end
 +
 
 +
     if not ignore then
 +
      if type(sV) == "table" then
 +
         if type(dest[sK]) == "table" then
 +
          util.table.overwrite(dest[sK], sV, ignoreKeys)
 +
        else
 +
          dest[sK] = {}
 +
          util.table.overwrite(dest[sK], sV, ignoreKeys)
 
         end
 
         end
    else -- number, string, boolean, etc
+
      else
         copy = original_table
+
         dest[sK] = sV
 +
      end
 
     end
 
     end
    return copy
+
  end
 
end
 
end
  
 
--ref: https://gist.github.com/balaam/3122129
 
--ref: https://gist.github.com/balaam/3122129
function util.reverse_numeric_table(tbl)
+
function util.table.reverse(tbl)
    local reversed_table = {}
+
  local reversed_table = {}
    local length = #tbl
+
  local length = #tbl
    for i,v in ipairs(tbl) do
+
 
        reversed_table[length + 1 - i] = v
+
  for i,v in ipairs(tbl) do
 +
      reversed_table[length + 1 - i] = v
 +
  end
 +
 
 +
  for k,v in pairs(tbl) do
 +
    if type(k) ~= 'number' then
 +
      reversed_table[k] = v
 
     end
 
     end
    return reversed_table
+
  end
 +
 
 +
  return reversed_table
 
end
 
end
  
---procedure
+
--ref: http://lua-users.org/wiki/CopyTable
function util.hl(title, width)
+
function util.table.shallowcopy(original_table)
   width = width or 80
+
   local orig_type = type(original_table)
 
+
  local copy
   if type(title) == "string" then
+
   if orig_type == 'table' then
    title = " " .. title .. " "
+
      copy = {}
    local before = math.floor((width - #title) / 2)
+
      for orig_key, orig_value in pairs(original_table) do
    local after = width - before - #title
+
          copy[orig_key] = orig_value
    print(string.rep("-", before) .. title .. string.rep("-", after))
+
      end
  else
+
  else -- number, string, boolean, etc
    print(string.rep("-", width))
+
      copy = original_table
 
   end
 
   end
 +
  return copy
 
end
 
end
  
function util.count(tbl, key_type)
+
-- this is not as strict as # so might bug out in extreme situations
 +
-- needs love
 +
function util.table.count(tbl, key_type)
 
   local length = 0;
 
   local length = 0;
 
   for k,v in pairs(tbl) do
 
   for k,v in pairs(tbl) do
Line 78: Line 124:
 
end
 
end
  
-- "delimiter" must be a single character or the removal of the final one won't work
+
-- procedure
function util.table_to_csv_string(simple_table, delimiter)
+
-- ref: https://gist.github.com/ripter/4270799
   local f_name = "string_csv"
+
function util.table.tprint(tbl, indent)
  delimiter = tostring(delimiter) or ","
+
   if not indent then indent = 0 end
  assert(#delimiter == 1, string.format("bad argument #2 to '%s' (single character expected)", f_name))
 
  
   local list = ""
+
   if type(tbl) ~= "table" then
 +
    print(tbl)
 +
    return 0
 +
  end
  
   for k,v in pairs(simple_table) do
+
   for k, v in pairs(tbl) do
     list = tostring(list) .. v .. delimiter
+
     local formatting = string.rep("  ", indent) .. k .. ": "
 +
    if type(v) == "table" then
 +
      print(formatting)
 +
      util.tprint(v, indent+1)
 +
    elseif type(v) == 'boolean' then
 +
      print(formatting .. tostring(v))
 +
    else
 +
      print(formatting .. v)
 +
    end
 
   end
 
   end
  list = string.sub(list, 1, -2)
 
  return list
 
 
end
 
end
  
---procedure
+
-- delimiter must be a single character
-- this could be implemented with metatable events
+
-- only for strings and numbers
-- ignore_keys: {"foo", "bar", ... }
+
function util.table.toCSVstring(tbl, delimiter)
function util.overwrite_first_table_with_second(first_table, second_table, ignore_keys)
+
   delimiter = delimiter or ","
   ignore_keys = ignore_keys or {}
+
  assert(#delimiter == 1 and type(delimiter) == 'toCSVstring', "toCSVstring: bad argument #2 (single character expected)")
  
   for k,v in pairs(second_table) do
+
   local csv = ""
    local ignore = false
 
  
    for _, ignored_key in ipairs(ignore_keys) do
+
  for k,v in pairs(tbl) do
      if k == ignored_key then ignore = true end
+
    if type(v) == 'string' or type(v) == 'number' then
 +
      csv = csv .. v .. delimiter
 +
    else
 +
      assert(false, "toCSVstring: can only handle numbers and strings")
 
     end
 
     end
 +
  end
  
    if not ignore then
+
  csv = string.sub(csv, 1, -2) -- remove final delimiter (works only for a single char)
      if type(v) == "table" then
+
 
        if type(first_table[k]) == "table" then
+
  return csv
          util.overwrite_first_table_with_second(first_table[k], v)
+
end
        else
+
 
          first_table[k] = {}
+
----------
          util.overwrite_first_table_with_second(first_table[k], v)
+
-- misc --
        end
+
----------
      else
+
 
        first_table[k] = v
+
-- procedure
      end
+
function util.hl(title, width)
     end
+
  width = width or 80
 +
 
 +
  if type(title) == "string" then
 +
    title = " " .. title .. " "
 +
    local before = math.floor((width - #title) / 2)
 +
    local after = width - before - #title
 +
    print(string.rep("-", before) .. title .. string.rep("-", after))
 +
  else
 +
     print(string.rep("-", width))
 
   end
 
   end
 
end
 
end
  
return util
+
function util.round(num, numDecimalPlaces)
 +
  return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
 +
end
 +
 
 +
return util -- return module

Latest revision as of 21:00, 17 May 2021


if mw then
  Search = Search or require("Module:Test/lib/search")
else
  Search = Search or require "lib/search"
end

local util = {}

------------------------
-- table manipulation --
------------------------

util.table = {}

function util.table.check(tbl, ...)
  local inside = tbl
  for i,v in pairs(arg) do
    if i ~= 'n' then
      inside = Search.find(v, inside or {})
    end
  end
  return inside and true
end

function util.table.checkMultiple(tbl, filters)
  local checked = false
  for _,filter in ipairs(filters) do
    if util.table.check(tbl, unpack(filter)) then
      checked = true
    else
      return false
    end
  end
  return checked
end

function util.table.isIn(var, table)
  assert(type(var) == 'nil', "isIn: Empty argument #1")
  assert(type(table) == 'table', string.format("isIn: Bad argument #2 ('table' expected, got '%s'", type(table)))
  for k,v in pairs(table) do
    if var == k or var == v then
      return true
    end
  end
  return false
end

-- procedure
function util.table.overwrite(dest, source, ignoreKeys)
  ignoreKeys = ignoreKeys or {}

  for sK,sV in pairs(source) do
    local ignore = false

    for _,ignoredK in ipairs(ignoreKeys) do
      if sK == ignoredK then
        ignore = true
        break
      end
    end

    if not ignore then
      if type(sV) == "table" then
        if type(dest[sK]) == "table" then
          util.table.overwrite(dest[sK], sV, ignoreKeys)
        else
          dest[sK] = {}
          util.table.overwrite(dest[sK], sV, ignoreKeys)
        end
      else
        dest[sK] = sV
      end
    end
  end
end

--ref: https://gist.github.com/balaam/3122129
function util.table.reverse(tbl)
  local reversed_table = {}
  local length = #tbl

  for i,v in ipairs(tbl) do
      reversed_table[length + 1 - i] = v
  end

  for k,v in pairs(tbl) do
    if type(k) ~= 'number' then
      reversed_table[k] = v
    end
  end

  return reversed_table
end

--ref: http://lua-users.org/wiki/CopyTable
function util.table.shallowcopy(original_table)
  local orig_type = type(original_table)
  local copy
  if orig_type == 'table' then
      copy = {}
      for orig_key, orig_value in pairs(original_table) do
          copy[orig_key] = orig_value
      end
  else -- number, string, boolean, etc
      copy = original_table
  end
  return copy
end

-- this is not as strict as # so might bug out in extreme situations
-- needs love
function util.table.count(tbl, key_type)
  local length = 0;
  for k,v in pairs(tbl) do
    if key_type then
      if type(k) == key_type then
        length = length + 1
      end
    else
      length = length + 1
    end
  end
  return length
end

-- procedure
-- ref: https://gist.github.com/ripter/4270799
function util.table.tprint(tbl, indent)
  if not indent then indent = 0 end

  if type(tbl) ~= "table" then
    print(tbl)
    return 0
  end

  for k, v in pairs(tbl) do
    local formatting = string.rep("  ", indent) .. k .. ": "
    if type(v) == "table" then
      print(formatting)
      util.tprint(v, indent+1)
    elseif type(v) == 'boolean' then
      print(formatting .. tostring(v))
    else
      print(formatting .. v)
    end
  end
end

-- delimiter must be a single character
-- only for strings and numbers
function util.table.toCSVstring(tbl, delimiter)
  delimiter = delimiter or ","
  assert(#delimiter == 1 and type(delimiter) == 'toCSVstring', "toCSVstring: bad argument #2 (single character expected)")

  local csv = ""

  for k,v in pairs(tbl) do
    if type(v) == 'string' or type(v) == 'number' then
      csv = csv .. v .. delimiter
    else
      assert(false, "toCSVstring: can only handle numbers and strings")
    end
  end

  csv = string.sub(csv, 1, -2) -- remove final delimiter (works only for a single char)

  return csv
end

----------
-- misc --
----------

-- procedure
function util.hl(title, width)
  width = width or 80

  if type(title) == "string" then
    title = " " .. title .. " "
    local before = math.floor((width - #title) / 2)
    local after = width - before - #title
    print(string.rep("-", before) .. title .. string.rep("-", after))
  else
    print(string.rep("-", width))
  end
end

function util.round(num, numDecimalPlaces)
  return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

return util -- return module