Module:Sandbox: Difference between revisionsGive feedback
Jump to navigation
Jump to search
test |
test |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local | local items_data = mw.loadJsonData("Data:ItemData.json") | ||
-- {{#invoke:Sandbox| | -- {{#invoke:Sandbox|get_item_key|item name}} | ||
function p. | function p.get_item_key(frame) | ||
local | local item_name_input = frame.args[1] -- Get the item name from the template argument | ||
if not | if not item_name_input then | ||
return "Error: | return "Error: Item name not specified." | ||
end | end | ||
item_name_input = item_name_input:lower() -- For case-independent search | |||
-- Go through all the | -- Go through all the items and look for a name match. | ||
for | for item_key, item_data in pairs(items_data) do | ||
if | if item_data and item_data["Name"] then | ||
local | local current_item_name = item_data["Name"]:lower() | ||
if | if current_item_name == item_name_input then | ||
return | return item_key -- Return the key if the name matches | ||
end | end | ||
end | end | ||
end | end | ||
return " | return "Item not found." -- If nothing is found | ||
end | end | ||
return p | return p | ||
Revision as of 14:40, 7 April 2025
Documentation for this module may be created at Module:Sandbox/doc
local p = {}
local items_data = mw.loadJsonData("Data:ItemData.json")
-- {{#invoke:Sandbox|get_item_key|item name}}
function p.get_item_key(frame)
local item_name_input = frame.args[1] -- Get the item name from the template argument
if not item_name_input then
return "Error: Item name not specified."
end
item_name_input = item_name_input:lower() -- For case-independent search
-- Go through all the items and look for a name match.
for item_key, item_data in pairs(items_data) do
if item_data and item_data["Name"] then
local current_item_name = item_data["Name"]:lower()
if current_item_name == item_name_input then
return item_key -- Return the key if the name matches
end
end
end
return "Item not found." -- If nothing is found
end
return p