Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 280: | Строка 280: | ||
if searchValue == "" or pagePath == "" or keyPath == "" then | if searchValue == "" or pagePath == "" or keyPath == "" then | ||
return "" | return "[]" | ||
end | end | ||
| Строка 289: | Строка 289: | ||
if not data then | if not data then | ||
local ok, loaded = pcall(mw.loadData, moduleName) | local ok, loaded = pcall(mw.loadData, moduleName) | ||
if not ok or not loaded then return "" end | if not ok or not loaded then return "[]" end | ||
data = loaded | data = loaded | ||
cache[moduleName] = data | cache[moduleName] = data | ||
| Строка 296: | Строка 296: | ||
local idsTable = data.id | local idsTable = data.id | ||
if type(idsTable) ~= "table" then | if type(idsTable) ~= "table" then | ||
return "" | return "[]" | ||
end | end | ||
local target = tostring(searchValue) | local target = tostring(searchValue) | ||
local matches = {} | |||
for idKey, entry in pairs(idsTable) do | for idKey, entry in pairs(idsTable) do | ||
| Строка 309: | Строка 310: | ||
for _, item in ipairs(v) do | for _, item in ipairs(v) do | ||
if tostring(item) == target then | if tostring(item) == target then | ||
matches[#matches + 1] = idKey | |||
break | |||
end | end | ||
end | end | ||
| Строка 315: | Строка 317: | ||
for _, item in pairs(v) do | for _, item in pairs(v) do | ||
if tostring(item) == target then | if tostring(item) == target then | ||
matches[#matches + 1] = idKey | |||
break | |||
end | end | ||
end | end | ||
| Строка 321: | Строка 324: | ||
else | else | ||
if tostring(v) == target then | if tostring(v) == target then | ||
matches[#matches + 1] = idKey | |||
end | end | ||
end | end | ||
| Строка 328: | Строка 331: | ||
end | end | ||
return "" | if #matches == 0 then | ||
return "[]" | |||
end | |||
local ok, json = pcall(mw.text.jsonEncode, matches) | |||
if ok and json then | |||
return json | |||
end | |||
return "[]" | |||
end | end | ||