Difference between revisions of "Module:Database"

From Cassette Beasts
Line 3: Line 3:
 
local db = mw.loadData("Module:Database/data")
 
local db = mw.loadData("Module:Database/data")
  
function p.listSpecies(frame)
+
-- Private helpers
  local result = ""
+
 
  for index, species in pairs(db.species.by_index) do
+
function get_species(id)
    if result ~= "" then
+
if tonumber(id) ~= nil then
      result = result .. "\n\n"
+
id = tonumber(id)
    end
+
end
    result = result .. string.format("%s #%03d", species.name, index)
+
if type(id) == "number" then
  end
+
return db.species.by_index[id]
  return result
+
else
 +
return db.species.by_name[id]
 +
end
 +
end
 +
 
 +
function subscript(value, path, path_start)
 +
while value ~= nil and path[path_start] ~= nil do
 +
local key = path[path_start]
 +
if tonumber(key) ~= nil then
 +
value = value[tonumber(key)]
 +
else
 +
value = value[key]
 +
end
 +
path_start = path_start + 1
 +
end
 +
return value
 +
end
 +
 
 +
function textify(frame, value)
 +
if value == true and frame.args.if_true ~= null then
 +
value = frame.args.if_true
 +
elseif value == false and frame.args.if_false ~= nil then
 +
value = frame.args.if_false
 +
elseif type(value) == "number" and frame.args.num_format ~= nil then
 +
value = frame.args.num_format:format(value)
 +
elseif type(value) == "string" and frame.args["if_" .. value] ~= nil then
 +
value = frame.args["if_" .. value]
 +
elseif type(value) == "table" and frame.args["foreach"] ~= nil then
 +
local delim = frame.args.delim or ""
 +
local result = ""
 +
for i,elem in ipairs(value) do
 +
if i > 1 then
 +
result = result .. delim
 +
end
 +
result = result .. frame:expandTemplate{title = frame.args["foreach"], args = { elem }}
 +
end
 +
value = result
 +
end
 +
if type(value) ~= "string" and type(value) ~= "number" and type(value) ~= "boolean" then
 +
return nil
 +
else
 +
return value
 +
end
 +
end
 +
 
 +
-- Public functions for use with #invoke
 +
 
 +
function p.get_species(frame)
 +
local species = get_species(frame.args[1])
 +
return textify(frame, subscript(species, frame.args, 2))
 
end
 
end
  
 
return p
 
return p

Revision as of 19:54, 21 May 2022

Documentation for this module may be created at Module:Database/doc

local p = {}

local db = mw.loadData("Module:Database/data")

-- Private helpers

function get_species(id)
	if tonumber(id) ~= nil then
		id = tonumber(id)
	end
	if type(id) == "number" then
		return db.species.by_index[id]
	else
		return db.species.by_name[id]
	end
end

function subscript(value, path, path_start)
	while value ~= nil and path[path_start] ~= nil do
		local key = path[path_start]
		if tonumber(key) ~= nil then
			value = value[tonumber(key)]
		else
			value = value[key]
		end
		path_start = path_start + 1
	end
	return value
end

function textify(frame, value)
	if value == true and frame.args.if_true ~= null then
		value = frame.args.if_true
	elseif value == false and frame.args.if_false ~= nil then
		value = frame.args.if_false
	elseif type(value) == "number" and frame.args.num_format ~= nil then
		value = frame.args.num_format:format(value)
	elseif type(value) == "string" and frame.args["if_" .. value] ~= nil then
		value = frame.args["if_" .. value]
	elseif type(value) == "table" and frame.args["foreach"] ~= nil then
		local delim = frame.args.delim or ""
		local result = ""
		for i,elem in ipairs(value) do
			if i > 1 then
				result = result .. delim
			end
			result = result .. frame:expandTemplate{title = frame.args["foreach"], args = { elem }}
		end
		value = result
	end
	if type(value) ~= "string" and type(value) ~= "number" and type(value) ~= "boolean" then
		return nil
	else
		return value
	end
end

-- Public functions for use with #invoke

function p.get_species(frame)
	local species = get_species(frame.args[1])
	return textify(frame, subscript(species, frame.args, 2))
end

return p