915
edits
No edit summary |
(Issue finally found...) |
||
(16 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
-- Module that contains all functions regarding numeric formatting and calculations. | |||
-- TODO: Make modules that now call Shared call Number (for relevant functions) | |||
local numModule = require('Module:Number') | |||
--So there are a handful of functions that I'm using in a lot of places | --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 | --So rather than continue to copy across the same handful of functions to every single new module | ||
Line 46: | Line 50: | ||
end | end | ||
-- Function to sort a dictionary-like structure where items are added like tbl['key'] = value | |||
-- We need to turn this structure into a table first, in order to sort it. | |||
function p.sortDictionary(dict, comparer) | |||
local sortedTable = {} | |||
for k, v in pairs(dict) do | |||
table.insert(sortedTable, {key = k, value = v}) | |||
end | |||
table.sort(sortedTable, comparer) | |||
return sortedTable | |||
end | |||
--General purpose function for going through a table after sorting based on a custom sort order | --General purpose function for going through a table after sorting based on a custom sort order | ||
Line 72: | Line 89: | ||
end | end | ||
-- | -- Takes an input string and returns the same string with title case-like | ||
-- | -- formatting (that is, the first letter of every word becomes uppercase, | ||
-- while the remainder becomes lowercase) | |||
-- Examples: | |||
-- titleCase('ALL CAPS') = 'All Caps' | |||
-- titleCase('all lowercase') = 'All Lowercase' | |||
-- titleCase('A MiXTUre') = 'A Mixture' | |||
-- Note that non-alphanumeric characters are treated as a word boundary, so: | |||
-- titleCase('a!b(c)d') = 'A!B(C)D' (not 'A!b(c)d') | |||
--Originally snagged this from Module:VoidByReward written by User:NoBrainz | --Originally snagged this from Module:VoidByReward written by User:NoBrainz | ||
function p.titleCase(head, tail) | function p.titleCase(head, tail) | ||
Line 144: | Line 168: | ||
end | end | ||
--Splits a string based on a sent in separating character | --- Splits a string based on a sent in separating character | ||
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"} | --- For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"} | ||
function p.splitString(inputstr, sep) | -- @param inputstr (string) The input to separate. | ||
-- @param sep (string/char) The separation character. | |||
-- @param trim (boolean) TRUE to trim the leading/trailing whitespaces | |||
function p.splitString(inputstr, sep, trim) | |||
if sep == nil then | if sep == nil then | ||
sep = "%s" | sep = "%s" | ||
Line 152: | Line 179: | ||
local t = {} | local t = {} | ||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do | for str in string.gmatch(inputstr, "([^"..sep.."]+)") do | ||
if trim == true then | |||
str = str:gsub("^%s*(.-)%s*$", "%1") | |||
end | |||
table.insert(t, str) | table.insert(t, str) | ||
end | end | ||
Line 165: | Line 195: | ||
--Adds commas | --Adds commas | ||
function p.formatnum(number) | function p.formatnum(number) | ||
return numModule.formatnum(number) | |||
end | end | ||
Line 187: | Line 204: | ||
function p.round(val, maxDigits, minDigits) | function p.round(val, maxDigits, minDigits) | ||
return numModule.round(val, maxDigits, minDigits) | |||
end | end | ||
--From http://lua-users.org/wiki/SimpleRound | --From http://lua-users.org/wiki/SimpleRound | ||
function p.round2(num, numDecimalPlaces) | function p.round2(num, numDecimalPlaces) | ||
return numModule.round2(num, numDecimalPlaces) | |||
return | |||
end | end | ||
Line 293: | Line 286: | ||
local res = {} | local res = {} | ||
for k, v in pairs(obj) do res[p.clone(k)] = p.clone(v) end | for k, v in pairs(obj) do res[p.clone(k)] = p.clone(v) end | ||
return res | |||
end | |||
-- Shallow clone, desirable when operations such as sorting are to be performed | |||
-- on a table where it is not necessary to perform a deep clone of all data within | |||
-- the table's elements | |||
function p.shallowClone(obj) | |||
if type(obj) ~= 'table' then return obj end | |||
local res = {} | |||
for k, v in pairs(obj) do | |||
res[k] = v | |||
end | |||
return res | return res | ||
end | end | ||
Line 298: | Line 303: | ||
-- Euclidean Greatest Common Divisor algorithm | -- Euclidean Greatest Common Divisor algorithm | ||
function p.gcd(a, b) | function p.gcd(a, b) | ||
return numModule.gcd(a, b) | |||
end | end | ||
--Formats a pair of numbers as a reduced fraction | --Formats a pair of numbers as a reduced fraction | ||
function p.fraction(n, d) | function p.fraction(n, d) | ||
return numModule.fraction(n, d) | |||
end | end | ||
--Similar to p.fraction but returns the simplified numerator and denomerator separately without formatting | --Similar to p.fraction but returns the simplified numerator and denomerator separately without formatting | ||
function p.fractionpair(n, d) | function p.fractionpair(n, d) | ||
return numModule.fractionpair(n, d) | |||
end | end | ||
Line 394: | Line 393: | ||
--Returns a number including the sign, even if positive | --Returns a number including the sign, even if positive | ||
function p.numStrWithSign(number) | function p.numStrWithSign(number) | ||
return numModule.numStrWithSign(number) | |||
end | |||
-- Applies formatting to an error message for display on wiki pages. | |||
-- Also appends a category such that errors can be easily located | |||
function p.printError(message) | |||
-- Prevent message being interpreted as wikitext and handle non-string messages | |||
local messageString = mw.text.nowiki(type(message) == 'string' and message or mw.dumpObject(message)) | |||
return '[[Category:Pages with script errors]]<div class="text-negative">ERROR: ' .. messageString .. '</div>' | |||
end | end | ||
Line 412: | Line 415: | ||
end | end | ||
return resultDesc | return resultDesc | ||
end | |||
-- Given a namespace & local ID, returns a namespaced ID | |||
function p.getNamespacedID(namespace, localID) | |||
if string.find(localID, ':') == nil then | |||
return namespace .. ':' .. localID | |||
else | |||
-- ID already appears to be namespaced | |||
return localID | |||
end | |||
end | |||
-- Given a namespaced ID, returns both the namespace & local ID | |||
function p.getLocalID(ID) | |||
local namespace, localID = nil, nil | |||
local sepIdx = string.find(ID, ':') | |||
if sepIdx == nil then | |||
-- Provided ID doesn't appear to be namespaced | |||
localID = ID | |||
else | |||
namespace = string.sub(ID, 1, sepIdx - 1) | |||
localID = string.sub(ID, sepIdx + 1, string.len(ID)) | |||
end | |||
return namespace, localID | |||
end | |||
-- Compares two strings, optionally ignoring case | |||
function p.compareString(left, right, ignoreCase) | |||
-- Both are nil (equal) | |||
if left == nil and right == nil then return true end | |||
-- Only one is nil (not equal) | |||
if left == nil or right == nil then return false end | |||
-- Convert inputs to strings, just in case | |||
left = tostring(left) | |||
right = tostring(right) | |||
if ignoreCase == true then | |||
return left:upper() == right:upper() | |||
else | |||
return left == right | |||
end | |||
end | |||
function p._replace(str, searchTerm, replacementTerm) | |||
if str == nil then | |||
return str | |||
end | |||
local escapedSearch = searchTerm:gsub("[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%1") | |||
local result = str:gsub(escapedSearch, replacementTerm) | |||
return result | |||
end | |||
function p.replace(frame) | |||
local args = frame:getParent().args | |||
return p._replace( | |||
frame.args[1], | |||
frame.args[2] or '', | |||
frame.args[3] or '') | |||
end | end | ||
return p | return p |
edits