Some more suggestions:
Lua Code:
local MAJOR, MINOR = "LibConstantMapper-1.0", 1
local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end
local mappingsTable = {
["ACTION_TYPE"] = { exclude = "DEPRECATED" },
["ATTRIBUTE"] ={ exclude = "ATTRIBUTE_.+_" },
["CHAT_CATEGORY"] = { exclude = "^CHAT_CATEGORY_HEADER" },
["CURRENCY_CHANGE_REASON"] = {},
["DAMAGE_TYPE" = {},
["ITEM_QUALITY"] = {},
["ITEM_TRAIT_TYPE"] = {},
["ITEMSTYLE"] = { exclude = "DEPRECATED" },
["ITEMTYPE"] = {},
["LINK_TYPE"] = { pattern = "_LINK_TYPE$" }
["STAT"] = { exclude = {"^STAT_BONUS_OPTION_", "^STAT_SOFT_CAP_OPTION", "^STAT_VALUE_COLOR"} }
}
--do not overwrite existing data
lib.data = lib.data or {}
--internal functions
function lib:AddData(mapping, pattern, exclude)
for name, value in zo_insecurePairs(_G) do
if type(mapping) == "table" then
for mappingName, mappingTable in pairs(mapping) do
lib:SaveDataEntry(name, value, mappingName, mappingTable.pattern, mappingTable.exclude)
end
elseif type(mapping) == "string" then
lib:SaveDataEntry(name, value, mapping, pattern, exclude)
end
end
end
function lib:SaveDataEntry(name, value, mapping, pattern, exclude)
if (name):find(pattern or "^"..mapping.."_") == nil then return end
local skip = false
if exclude then
if type(exclude) == "table" then
for i,v in ipairs(exclude) do
if (name):find(v) then
skip = true
break
end
end
else
skip = (name):find(exclude) ~= nil
end
end
if not skip then
lib.data[mapping] = data[mapping] or {}
lib.data[mapping][name] = value
end
end
if oldminor then
--library update, add only new/changed items?
local newItems = {}
for name, value in pairs(mappingsTable) do
if value ~= lib.mappings[name] then
newItems[name] = value
lib.mappings = value
end
end
lib:AddData(newItems)
else
lib.mappings = mappingsTable
lib:AddData(mappingsTable)
end
--external functions
function lib:AddMapping(mapping, pattern, exclude)
--add or replace enxisting entry
if type(mapping) == "table" then
for name, value in pairs(mapping) do
lib.mappings[name] = value
end
else
lib.mappings[mapping] = { ["pattern"] = pattern, ["exclude"] = exclude }
end
lib:AddData(mapping, pattern, exclude)
end
function lib:GetConstants(mapping)
return lib.data[mapping]
end
1) Mapping can be part of the name and pattern is defined only in special cases
2) Do not create a new global table ConstantMapperMappings, just make it accessible using the library reference
3) As merlight said, it would be nice to have direct [value] => "key" mapping, so I changed it that way