Module:Manufacturer: Difference between revisions

Fury>Alistair3149
No edit summary
 
m (1 revision imported: starcitizen tools)
 
(2 intermediate revisions by 2 users not shown)
Line 10: Line 10:
for i, manufacturer in ipairs( data ) do
for i, manufacturer in ipairs( data ) do
     for j, value in pairs( manufacturer ) do
     for j, value in pairs( manufacturer ) do
     if string.find( string.lower( value ), string.lower( s ) ) then
     if string.match( string.lower( value ), '^' .. string.lower( s ) .. '$' ) then
     return manufacturer
     return manufacturer
     end
     end
Line 20: Line 20:
function p.manufacturer( frame )
function p.manufacturer( frame )
mArguments = require( 'Module:Arguments' )
mArguments = require( 'Module:Arguments' )
return p._manufacturer( mArguments.getArgs( frame ) )
local args = mArguments.getArgs( frame )
local s = args[1]
-- Default to name key
local type = args['type'] or 'name'
if not s then
return '<span class="error">No text specified.</span>'
end
return p._manufacturer( s, type )
end
end


function p._manufacturer( args )
function p._manufacturer( s, type )
local s = args[1]
-- Return nil for Lua
local type = args['type'] or 'table'
if s == nil then return end
checkType('_manufacturer', 1, s, 'string')
-- Return table for Lua
checkType('_manufacturer', 2, type, 'string')
if type == nil then
type = 'table'
end


local manufacturer = matchManufacturer( s )
local manufacturer = matchManufacturer( s )
if manufacturer == nil or manufacturer[type] == nil then
return '<span class="error">No ' .. type .. ' found for ' .. s .. '.</span>'
-- Used for other Lua modules
-- Used for other Lua modules
elseif type == 'table' then
if type == 'table' then
return manufacturer
return manufacturer
-- Return error message
elseif manufacturer == nil or manufacturer[type] == nil then
return '<span class="error">No ' .. type .. ' found for ' .. s .. '.</span>'
-- Return wiki page name
elseif type == 'page' then
return manufacturer['page'] or manufacturer['name']
-- Return string from matched manufacturer
-- Return string from matched manufacturer
else
else

Latest revision as of 18:13, 7 November 2023

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

local mArguments --initialize lazily
local checkType = require('libraryUtil').checkType
local p = {}

--- Return matched manufacturer from string
--- @param string s Match string
--- @return table Manufacturer
local function matchManufacturer( s )
	local data = mw.loadJsonData( 'Module:Manufacturer/data.json' )
	for i, manufacturer in ipairs( data ) do
    	for j, value in pairs( manufacturer ) do
    		if string.match( string.lower( value ),  '^' .. string.lower( s ) .. '$' ) then
    			return manufacturer
    		end
    	end
	end
	return nil
end

function p.manufacturer( frame )
	mArguments = require( 'Module:Arguments' )
	local args = mArguments.getArgs( frame )
	local s = args[1]
	-- Default to name key
	local type = args['type'] or 'name'
	if not s then
		return '<span class="error">No text specified.</span>'
	end
	return p._manufacturer( s, type )
end

function p._manufacturer( s, type )
	-- Return nil for Lua
	if s == nil then return end
	-- Return table for Lua
	if type == nil then
		type = 'table'
	end

	local manufacturer = matchManufacturer( s )
	-- Used for other Lua modules
	if type == 'table' then
		return manufacturer
	-- Return error message
	elseif manufacturer == nil or manufacturer[type] == nil then
		return '<span class="error">No ' .. type .. ' found for ' .. s .. '.</span>'
	-- Return wiki page name
	elseif type == 'page' then
		return manufacturer['page'] or manufacturer['name']
	-- Return string from matched manufacturer
	else
		return manufacturer[type]
	end
end

return p