Module:Var-array

From Moegirlpedia
Revision as of 14:18, 2 January 2021 by 桂の脌 (talk | contribs) (Transport form zh.moegirl.org.cn)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Template-info.svg Module Documentation  [Create] [Refresh]
local module = {}

function module.new(name, array)
	local frame = mw.getCurrentFrame()
	local idCount = frame:callParserFunction( '#var', 'array.idCount', 0)
	function foo(name, array)
		name = '@array-innerArrayIdentifier:'..name
		frame:callParserFunction( '#vardefine', name, name)
		for i, v in ipairs(array) do
			if type(v) == 'table' then
				idCount = idCount + 1
				local innerArrayName = name..'___'..idCount..'___'
				foo(innerArrayName, v)
				v = '@array-innerArrayIdentifier:'..innerArrayName
			end
			frame:callParserFunction( '#vardefine', name..i, v)		
		end
		frame:callParserFunction( '#vardefine', name..'.count', #array)
	end
	foo(name, array)
	
	frame:callParserFunction( '#vardefine', 'array.idCount', idCount)
	return name
end

function module.get(name)
	local frame = mw.getCurrentFrame()
	if type(name) == 'nil' then return nil end
	local time = 0
	function foo(name)
		local array = {}
		
		time = time + 1
		if time == 1 then
			name = '@array-innerArrayIdentifier:'..name
		end
		
		local count = frame:callParserFunction( '#var', name..'.count')		
		if count == '' then return false end
		for i=1, count do
			local val = frame:callParserFunction('#var', name..i)
			if string.find(val, '^@array%-innerArrayIdentifier:') then
				array[i] = foo(val)
			else
				array[i] = val
			end
		end
		return array
	end
	
	return foo(name)
end

return module