Module:Utilities: Difference between revisionsGive feedback
Gammaton32 (talk | contribs) created new function to remove hashtags |
No edit summary |
||
| (8 intermediate revisions by 3 users not shown) | |||
| Line 17: | Line 17: | ||
result = result:gsub("%s+", "", 1) -- Removes the postfixed space | result = result:gsub("%s+", "", 1) -- Removes the postfixed space | ||
end | end | ||
-- Trim any extra whitespace | -- Trim any extra whitespace | ||
result = result:gsub("^%s*(.-)%s*$", "%1") | result = result:gsub("^%s*(.-)%s*$", "%1") | ||
return result | return result | ||
end | end | ||
--round_to_significant_figures(12345.6789, 3) -- Output: 12300 | --round_to_significant_figures(12345.6789, 3) -- Output: 12300 | ||
--round_to_significant_figures(0.0012345, 2) -- Output: 0.0012 | --round_to_significant_figures(0.0012345, 2) -- Output: 0.0012 | ||
--round_to_significant_figures(-98765, 4) -- Output: -98760 | --round_to_significant_figures("-98765", 4) -- Output: -98760 | ||
function p.round_to_sig_fig( | --round_to_significant_figures("foobar", 4) -- Output: foobar | ||
function p.round_to_sig_fig(input_value, n) | |||
num = tonumber(input_value) | |||
if num == nil then | |||
return input_value | |||
end | |||
if num == 0 then | if num == 0 then | ||
return 0 | return 0 | ||
| Line 112: | Line 111: | ||
local slot_colors = { | local slot_colors = { | ||
["Weapon"] = { | ["Weapon"] = { | ||
hex = " | hex = "c8761c", | ||
rgb = " | rgb = "200, 118, 28", | ||
hsl = " | hsl = "31, 75%, 45%", | ||
cmyk = "0%, | cmyk = "0%, 41%, 86%, 22%" | ||
}, | }, | ||
["Armor"] = { | ["Armor"] = { | ||
hex = " | hex = "6f8724", | ||
rgb = " | rgb = "198, 248, 114", | ||
hsl = "82, | hsl = "82, 91%, 71%", | ||
cmyk = " | cmyk = "20%, 0%, 54%, 3%" | ||
}, | }, | ||
["Tech"] = { | ["Tech"] = { | ||
hex = " | hex = "ba5ca6", | ||
rgb = " | rgb = "186, 92, 166", | ||
hsl = " | hsl = "313, 41%, 55%", | ||
cmyk = " | cmyk = "0%, 51%, 11%, 27%" | ||
} | } | ||
| Line 217: | Line 216: | ||
if value then | if value then | ||
-- Keep only digits (0-9), plus (+), and minus (-) | -- Keep only digits (0-9), plus (+), and minus (-) | ||
value = value:gsub("[^0-9+-]", "") | value = value:gsub("[^0-9+.-]", "") | ||
-- Return empty string if nothing left, otherwise return the filtered value | -- Return empty string if nothing left, otherwise return the filtered value | ||
return value ~= "" and value or variable_name | return value ~= "" and value or variable_name | ||
| Line 226: | Line 225: | ||
return processed_text | return processed_text | ||
end | |||
function p.contains(tbl, str) | |||
for _, v in ipairs(tbl) do | |||
if v == str then | |||
return true | |||
end | |||
end | |||
return false | |||
end | |||
-- Expands multiple of the same template in a single preprocess call | |||
-- Example: | |||
-- expand_teampltes(frame, 'HeroIcon', {hero_atlas: {'Abrams'}, hero_bebop: {'Bebop'}) | |||
-- Returns {hero_atlas: <abrams_icon_template>, hero_bebop: <bebop_icon_template>, ...} | |||
function p.expand_templates(frame, template_name, args_map) | |||
local keys = {} | |||
local strings = {} | |||
for key, args in pairs(args_map) do | |||
keys[#keys + 1] = key | |||
end | |||
for i, key in ipairs(keys) do | |||
local args = args_map[key] | |||
local parts = {"{{", template_name, "|"} | |||
for j, arg in ipairs(args) do | |||
if j > 1 then parts[#parts + 1] = "|" end | |||
parts[#parts + 1] = arg | |||
end | |||
parts[#parts + 1] = "}}" | |||
strings[i] = table.concat(parts) | |||
end | |||
local expanded = mw.text.split( | |||
frame:preprocess(table.concat(strings, "\x01")), "\x01" | |||
) | |||
local result = {} | |||
for i, key in ipairs(keys) do | |||
result[key] = expanded[i] | |||
end | |||
return result | |||
end | |||
-- Helper function | |||
-- Returns true or false depending if the page exist | |||
function p.page_exists(title_text) | |||
local title_obj = mw.title.new(title_text) | |||
return title_obj and title_obj.exists or false | |||
end | end | ||
return p | return p | ||