Warframe Wiki
Advertisement

Die Dokumentation für dieses Modul kann unter Modul:Shared/Doku erstellt werden

--So there are a handful of functions that I'm using in a lot of places
--So rather than continue to copy across the same handful of functions to every single new module
--I'm just going to unify them here
--Here's how you use this:
-- 1. When you're making a new module, add this near the top:
--        local Shared = require( "Module:Shared" )
-- 2. When you need to make a call to one of these functions,
--    just preface it with "Shared."
--    So for example you could call tableCount like so:
--        Shared.tableCount(data)
--This is being initially pulled together by User:Falterfire, but...
--not all these functions are mine.
--I've cited original author where I can trace it

local p = {}

-- iterator sorted by keys
-- For example, if you had a table that looked something like
-- data = {["Cat"] = 5,
--         ["Bat"] = 4,
--         ["Hat"] = 7}
-- You could do
--  for k, v in skpairs(data) do...
-- And your loop would start with k="Bat", v=4 then go to k="Cat", v=5, 
--		 and finally to k="Hat", v=7
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
function p.skpairs(t)
	local keys = {}
	for k in pairs(t) do
		keys[#keys + 1] = k
	end
	table.sort(keys)
	local i = 0
	return function()
		i = i + 1
		local key = keys[i]
		if key then
			return key, t[key]
		else
			return nil
		end
	end
end

--Loops through all the Relic types
--Comes up a surprising amount
function p.relicLoop()
	local tier = ""
	local new_vars = {
		[""] = "Lith",
		Lith = "Meso",
		Meso = "Neo",
		Neo = "Axi",
	}
	return function()
		tier = new_vars[tier] or nil
		return tier
	end
end

function p.consoleLoop()
	local console = ""
	local new_vars = {
		[""] = "PC",
		PC = "PS4",
		PS4 = "XB1",
	}
	return function()
		console = new_vars[console] or nil
		return console
	end
end
 
-- conveniently shifts BLAH to Blah
-- Handy when formatting data in ALL CAPS or all lower case
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
function p.titleCase(head, tail)
	if not tail then
		--Split into two lines because don't want the other return from gsub
		local result = string.gsub(head, "(%a)([%w_']*)", p.titleCase)
		return result
	else
		return string.upper(head) .. string.lower(tail)
	end
end

--Returns the number of rows in a table 
--Originally snagged this from Module:VoidByReward written by User:NoBrainz
function p.tableCount(t)
	local count = 0
	for _ in pairs(t) do
		count = count + 1
	end
	return count
end

--Sorts theTable based on the listed column
function p.tableSort(theTable, sortCol, ascend)
	local function sorter(r1, r2)
		if ascend then
			return r1[sortCol] < r2[sortCol]
		else
			return r1[sortCol] > r2[sortCol]
		end
	end
	table.sort(theTable, sorter)
end

--Splits a string based on a sent in separating character
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"}
function p.splitString(inputstr, sep)
	if sep == nil then
		sep = "%s"
	end
	local t = {}
	for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
		table.insert(t, str)
	end
	return t
end

--Returns 'true' if a string starts with something
--For example calling startsWith ("Lith V1 Relic", "Lith") would return true
function p.startsWith(string1, start)
	return string.sub(string1, 1, #start) == start
end

--Stolen from Stack Overflow
--Adds commas
function p.formatnum(number)
  local _, _, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')

  -- reverse the int-string and append a comma to all blocks of 3 digits
  int = int:reverse():gsub("(%d%d%d)", "%1%.")

  -- reverse the int-string back remove an optional comma and put the 
  -- optional minus and fractional part back
  return minus .. int:reverse():gsub("^%.", "") .. fraction
end

function p.round(val, maxDigits, minDigits)
	if not val then
		return nil
	end
	if type(maxDigits) == "table" then
		minDigits = maxDigits[2]
		maxDigits = maxDigits[1]
	end

	local result = val .. ""
	local decimals = string.find(result, "%.")
	if decimals then
		decimals = #result - decimals
	else
		decimals = 0
	end

	if maxDigits and decimals > maxDigits then
		result = tonumber(string.format("%." .. maxDigits .. "f", result))
	elseif minDigits and decimals < minDigits then
		result = string.format("%." .. minDigits .. "f", result)
	end

	return result
end

function p.contains(List, Item)
	if not List or not Item then
		return false
	end

	if type(List) == "table" then
		for _, listI in pairs(List) do
			if listI == Item then
				return true
			end
		end
	else
		local start = string.find(List, Item)
		return start ~= nil
	end
	return false
end

return p
Advertisement