915
edits
m (titleCase: Better describe function) |
mNo edit summary |
||
(15 intermediate revisions by the same user not shown) | |||
Line 50: | 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, factory) | |||
local sortedTable = {} | |||
for k, v in pairs(dict) do | |||
local newValue = nil | |||
if factory then | |||
newValue = factory(k, v) | |||
end | |||
newValue = newValue or {key = k, value = v} | |||
table.insert(sortedTable, newValue) | |||
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 74: | Line 92: | ||
end | end | ||
end | end | ||
end | |||
-- Makes a deep copy of a table or otherwise object. | |||
-- Yoinked from http://lua-users.org/wiki/CopyTable | |||
function p.deepcopy(orig) | |||
local copy | |||
if type(orig) == 'table' then | |||
copy = {} | |||
for orig_key, orig_value in next, orig, nil do | |||
copy[deepcopy(orig_key)] = deepcopy(orig_value) | |||
end | |||
setmetatable(copy, deepcopy(getmetatable(orig))) | |||
else -- number, string, boolean, etc | |||
copy = orig | |||
end | |||
return copy | |||
end | end | ||
Line 94: | Line 128: | ||
return string.upper(head) .. string.lower(tail) | return string.upper(head) .. string.lower(tail) | ||
end | end | ||
end | |||
-- Converts an input string into TitleCase. | |||
-- Every first letter of every word becomes uppercase | |||
-- With the exception of 'of', 'the', 'and', but only if these | |||
-- appear anywhere in the sentence besides the first occurence. | |||
-- Examples: | |||
-- specialTitleCase('ALL CAPS') = 'All Caps' | |||
-- specialTitleCase('all lowercase') = 'All Lowercase' | |||
-- specialTitleCase('A MiXTUre') = 'A Mixture' | |||
-- specialTitleCase('the bones') = 'The Bones | |||
-- specialTitleCase('of the world') = 'Of the World' | |||
-- specialTitleCase('amulet Of Fishing') = 'Amulet of Fishing' | |||
function p.specialTitleCase(sentence) | |||
if sentence == nil or sentence:match("^%s*$") ~= nil then return nil end | |||
-- List of words that are excluded from TitleCase | |||
local excludedWords = { | |||
["of"] = true, | |||
["and"] = true, | |||
["the"] = true | |||
} | |||
-- Split all words and add them as lower case to the table. | |||
local words = {} | |||
for word in sentence:gmatch("%S+") do | |||
table.insert(words, word:lower()) | |||
end | |||
-- Capitalize the first word | |||
words[1] = words[1]:gsub("^%l", string.upper) | |||
-- Title-case the remaining words, excluding certain words based on position | |||
for i = 2, #words do | |||
local curWord = words[i] | |||
if excludedWords[curWord] == true then | |||
else | |||
words[i] = curWord:gsub("^%l", string.upper) | |||
end | |||
end | |||
return table.concat(words, " ") | |||
end | end | ||
Line 155: | Line 231: | ||
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 163: | Line 242: | ||
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 430: | Line 512: | ||
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() | ||
Line 435: | Line 520: | ||
return left == right | return left == right | ||
end | 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 | |||
function p.addOrUpdate(tbl, key, func) | |||
local val = tbl[key] | |||
if val ~= nil then | |||
tbl[key] = func(val) | |||
else | |||
tbl[key] = func(nil) | |||
end | |||
return tbl[key] | |||
end | end | ||
return p | return p |
edits