Модуль:IanComradeBot/prototypes/fills/Item.json/data: различия между версиями

Материал из Space Station 14 Вики
Нет описания правки
Метка: ручная отмена
мНет описания правки
Строка 1: Строка 1:
local title = mw.title.new("Участник:IanComradeBot/prototypes/fills/Item.json")
local title = mw.title.new("Участник:IanComradeBot/prototypes/fills/Item.json")
local jsonData = title and title:getContent() or ""
local jsonData = title and title:getContent() or ""
local success, data = pcall(mw.text.jsonDecode, jsonData)
local success, data = pcall(mw.text.jsonDecode, jsonData)
return (success and type(data) == "table") and data or {}
 
if not success or type(data) ~= "table" then
    return {}
end
 
local function normalizeData(item)
    if type(item) ~= "table" then return item end
   
    if item.id and not next(item, "id") then
        for _, fullItem in ipairs(data) do
            if fullItem.id == item.id then
                return fullItem
            end
        end
    end
 
    for k, v in pairs(item) do
        item[k] = normalizeData(v)
    end
   
    return item
end
 
for i, item in ipairs(data) do
    data[i] = normalizeData(item)
end
 
return data

Версия от 19:39, 4 февраля 2025

Для документации этого модуля может быть создана страница Модуль:IanComradeBot/prototypes/fills/Item.json/data/doc

local title = mw.title.new("Участник:IanComradeBot/prototypes/fills/Item.json")
local jsonData = title and title:getContent() or ""

local success, data = pcall(mw.text.jsonDecode, jsonData)

if not success or type(data) ~= "table" then
    return {}
end

local function normalizeData(item)
    if type(item) ~= "table" then return item end
    
    if item.id and not next(item, "id") then
        for _, fullItem in ipairs(data) do
            if fullItem.id == item.id then
                return fullItem
            end
        end
    end

    for k, v in pairs(item) do
        item[k] = normalizeData(v)
    end
    
    return item
end

for i, item in ipairs(data) do
    data[i] = normalizeData(item)
end

return data