Anonymous

Module:Shared: Difference between revisions

From Melvor Idle
m
Add splitString param to trim whitespace
m (Add string compare method)
m (Add splitString param to trim whitespace)
(2 intermediate revisions by 2 users not shown)
Line 76: Line 76:
end
end


-- conveniently shifts BLAH to Blah
-- Takes an input string and returns the same string with title case-like
-- Handy when formatting data in ALL CAPS or all lower case
-- 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 148: Line 155:
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 156: Line 166:
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 423: Line 436:
if left == nil or right == nil then return false end
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
if ignoreCase == true then
return left:upper() == right:upper()
return left:upper() == right:upper()
914

edits