915
edits
(Issue finally found...) |
mNo edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 52: | Line 52: | ||
-- Function to sort a dictionary-like structure where items are added like tbl['key'] = value | -- 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. | -- We need to turn this structure into a table first, in order to sort it. | ||
function p.sortDictionary(dict, comparer) | function p.sortDictionary(dict, comparer, factory) | ||
local sortedTable = {} | local sortedTable = {} | ||
for k, v in pairs(dict) do | 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 | end | ||
Line 87: | 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 107: | 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 477: | Line 540: | ||
frame.args[2] or '', | frame.args[2] or '', | ||
frame.args[3] 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