Module:Manufacturer

Revision as of 15:45, 17 May 2023 by Fury>Alistair3149
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.find( string.lower( value ), string.lower( s ) ) then
    			return manufacturer
    		end
    	end
	end
	return nil
end

function p.manufacturer( frame )
	mArguments = require( 'Module:Arguments' )
	return p._manufacturer( mArguments.getArgs( frame ) )
end

function p._manufacturer( args )
	local s = args[1]
	local type = args['type'] or 'table'
	checkType('_manufacturer', 1, s, 'string')
	checkType('_manufacturer', 2, type, 'string')

	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
	elseif type == 'table' then
		return manufacturer
	-- Return string from matched manufacturer
	else
		return manufacturer[type]
	end
end

return p