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...")
 
Line 1: Line 1:
local util = {
+
local util = {}
  _DESCRIPTION = 'an assortment of useful things',
 
}
 
  
--ref: https://gist.github.com/ripter/4270799
+
------------------------
function util.tprint(tbl, indent)
+
-- table manipulation --
   if not indent then indent = 0 end
+
------------------------
 +
 
 +
util.table = {}
 +
 
 +
-- procedure
 +
function util.table.overwrite(dest, source, ignoreKeys)
 +
   ignoreKeys = ignoreKeys or {}
  
   if type(tbl) ~= "table" then
+
   for sK,sV in pairs(source) do
     print(tbl)
+
     local ignore = false
    return 0
 
  end
 
  
  for k, v in pairs(tbl) do
+
    for _,ignoredK in ipairs(ignoreKeys) do
    formatting = string.rep("  ", indent) .. k .. ": "
+
      if sK == ignoredK then
    if type(v) == "table" then
+
        ignore = true
      print(formatting)
+
        break
      util.tprint(v, indent+1)
+
       end
    elseif type(v) == 'boolean' then
 
      print(formatting .. tostring(v))
 
    else
 
       print(formatting .. v)
 
 
     end
 
     end
  end
 
end
 
  
--ref: http://lua-users.org/wiki/CopyTable
+
    if not ignore then
function util.shallowcopy(original_table)
+
      if type(sV) == "table" then
    local orig_type = type(original_table)
+
        if type(dest[sK]) == "table" then
    local copy
+
          util.table.overwrite(dest[sK], sV, ignoreKeys)
    if orig_type == 'table' then
+
         else
         copy = {}
+
          dest[sK] = {}
        for orig_key, orig_value in pairs(original_table) do
+
          util.table.overwrite(dest[sK], sV, ignoreKeys)
            copy[orig_key] = orig_value
 
 
         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 85:
 
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
+
     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
+
return util -- return module

Revision as of 14:24, 9 May 2021


local util = {}

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

util.table = {}

-- 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
    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

return util -- return module