模块:CharacterCommon
来自星砂岛百科
更多操作
此模块的文档可以在模块:CharacterCommon/doc创建
local common = require('Module:Common')
local p = {}
local character_mapping_cache
local DEFAULT_MAPPING_PAGES = {
'数据:Character/character_mapping.json',
'数据:Character/character mapping.json',
}
function p.defaultMapping()
return {
name_to_id = {},
id_to_name = {},
aliases = {},
overrides = {
name_to_id = {},
aliases = {},
},
}
end
local function normalize_pages(pages)
if type(pages) == 'string' then
return { pages }
end
if type(pages) ~= 'table' then
return {}
end
return pages
end
local function find_mapped_key(data, mapping, normalized)
if type(data) ~= 'table' or type(mapping) ~= 'table' then
return ''
end
local function resolve_candidate(candidate)
local mapped_key = p.normalizeKey(candidate)
if mapped_key ~= '' and data[mapped_key] then
return mapped_key
end
return ''
end
for _, candidate in ipairs({
mapping.overrides and mapping.overrides.name_to_id and mapping.overrides.name_to_id[normalized] or false,
mapping.overrides and mapping.overrides.aliases and mapping.overrides.aliases[normalized] or false,
mapping.name_to_id and mapping.name_to_id[normalized] or false,
mapping.aliases and mapping.aliases[normalized] or false,
}) do
local mapped_key = resolve_candidate(candidate)
if mapped_key ~= '' then
return mapped_key
end
end
return ''
end
function p.trim(value)
return common.trim(value)
end
function p.normalizeKey(value)
return common.normalizeKey(value)
end
function p.stripNamespace(value)
local resolved = common.trim(value)
local namespace_stripped = mw.ustring.match(resolved, '^[^:]+:(.+)$')
if common.trim(namespace_stripped) ~= '' then
return common.trim(namespace_stripped)
end
return resolved
end
function p.resolveLookupKey(value, options)
local resolved = common.trim(value)
if resolved == '' then
resolved = common.getCurrentTitleText()
end
if not options or options.strip_namespace ~= false then
resolved = p.stripNamespace(resolved)
end
return resolved
end
function p.loadFirstJsonPage(pages)
for _, title in ipairs(normalize_pages(pages)) do
local data = common.loadJsonData(title)
if type(data) == 'table' then
return data
end
end
return nil
end
function p.loadCharacterMapping()
if character_mapping_cache then
return character_mapping_cache
end
character_mapping_cache = p.loadFirstJsonPage(DEFAULT_MAPPING_PAGES) or p.defaultMapping()
return character_mapping_cache
end
function p.loadDomainData(data_pages, mapping_pages)
local data = p.loadFirstJsonPage(data_pages) or {}
local mapping
if mapping_pages == false then
mapping = p.defaultMapping()
elseif mapping_pages == nil then
mapping = p.loadCharacterMapping()
else
mapping = p.loadFirstJsonPage(mapping_pages) or p.defaultMapping()
end
return data, mapping
end
function p.findRecordWithKey(data, mapping, key, options)
local resolved = p.resolveLookupKey(key, options)
local normalized = p.normalizeKey(resolved)
if normalized ~= '' and type(data) == 'table' and data[normalized] then
return data[normalized], normalized, normalized
end
local mapped_key = find_mapped_key(data, mapping or p.loadCharacterMapping(), normalized)
if mapped_key ~= '' then
return data[mapped_key], mapped_key, normalized
end
return nil, '', normalized
end
function p.findRecord(data, mapping, key, options)
local record = p.findRecordWithKey(data, mapping, key, options)
return record
end
function p.tableHasEntries(value)
if type(value) ~= 'table' then
return false
end
for _ in pairs(value) do
return true
end
return false
end
function p.orderedNumericValues(value)
if type(value) ~= 'table' then
return {}
end
local ordered_pairs = {}
for key, item in pairs(value) do
local numeric_key = tonumber(key)
if numeric_key ~= nil then
ordered_pairs[#ordered_pairs + 1] = {
key = numeric_key,
value = item,
}
end
end
table.sort(ordered_pairs, function(left, right)
return left.key < right.key
end)
local out = {}
for _, pair in ipairs(ordered_pairs) do
out[#out + 1] = pair.value
end
return out
end
return p