Module:Utilities: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Sur (talk | contribs)
m url_encode initial
Sur (talk | contribs)
m url_encode fix, str instead of input
Line 74: Line 74:
function p.url_encode(str)
function p.url_encode(str)
-- Just replaces spaces with %20
-- Just replaces spaces with %20
return string.gsub(input, " ", "%%20")
return string.gsub(str, " ", "%%20")
end
end
return p
return p

Revision as of 03:10, 12 October 2024

Overview

Utility functions that serve any miscellaneous purpose

Functions

get_slot_color

Retrieve's the color of a certain slot/category in any color format (default hex), formerly known in english as Weapon, Vitality, and Spirit, though their unlocalized names are Weapon, Armor, and Tech. Utilized by Template:SlotColor

Parameters

  • slot - slot key, options are Weapon, Armor, and Tech
  • color_format - color format - Defaults to hex. Valid options are hex, rgb, hsl, cmyk
  • no_wrap - (OPTIONAL) - Defaults to false. Set to true to remove prefixes and postfixes of '#' from hex, or 'rgb()', 'hsl()', and 'cmyk()' from the other formats. May be useful for altering the values dynamically
  • debug_mode - (OPTIONAL) - Defaults to false. Used mostly for documentation purposes

Examples

See examples at Template:SlotColor


local p = {};
	
	-- "Default %hero_name% Build" --> "Default  Build", or "Default Build" with -1 or +1 for spaces
	function p.remove_var(input, spaces)
		-- spaces = 
		-- -1: also remove prefixed character
		-- 0 or nil: no character
		-- 1: postfixed character
		-- Remove text between % and % including the %
	    local result = input:gsub("%%.-%%", "")
	    
	    -- If spaces == -1, remove the character before the removed section
	    if spaces ~= nil and spaces == -1 then
	        result = result:gsub("%s+%s*", "", 1) -- Removes the preceding space or character
	    -- If spaces == 1, remove the character after the removed section
	    elseif spaces ~= nil and spaces == 1 then
	        result = result:gsub("%s+", "", 1) -- Removes the postfixed space
	    end
	    
	    -- Trim any extra whitespace
	    result = result:gsub("^%s*(.-)%s*$", "%1")
	    
	    return result
	end

	--round_to_significant_figures(12345.6789, 3)  -- Output: 12300
	--round_to_significant_figures(0.0012345, 2)   -- Output: 0.0012
	--round_to_significant_figures(-98765, 4)      -- Output: -98760
	function p.round_to_sig_fig(num, n)
	    if num == 0 then
	        return 0
	    end
	
	    -- Calculate the order of magnitude (log10 returns the logarithm base 10)
	    local order_of_magnitude = math.floor(math.log10(math.abs(num)))
	    
	    -- Scale the number to move the significant digits into the integer part
	    local scale = math.pow(10, n - 1 - order_of_magnitude)
	    
	    -- Round the scaled number and then scale it back
	    local rounded_num = math.floor(num * scale + 0.5) / scale
	    
	    return rounded_num
	end
	
	-- If the image exists exists, return it back enclosed in brackets, else return a blank string
	function p.get_image_file(image_file_name, px, link)
		if (px == nil) then px = 15 end --default
		if (link == nil) then link = "" end --default
		
		image_file = mw.title.new(image_file_name)
		if image_file and image_file.exists then
		    image_file_name = "[[" .. image_file_name .. "|" .. px .. "px|link=" .. link .. "]]"
		else
		    image_file_name = ''
		end
		
		return image_file_name
	end
	
	function p.string_endswith(str, ending)
	    return ending == "" or str:sub(-#ending) == ending
	end
	
	-- Add a space before each capital letter that is not the first character
	-- i.e. BulletVelocity > Bullet Velocity
	-- when a string doesn't have localization, it can outputted as add_space_before_cap(unlocalized_key)
	function p.add_space_before_cap(str)
	    local result = str:gsub("(%l)(%u)", "%1 %2")
	    return result:gsub("(%u)(%u%l)", " %1%2")
	end
	
	--Much room for expansion here
	function p.url_encode(str)
		-- Just replaces spaces with %20
		return string.gsub(str, " ", "%%20")
	end
return p