被永久保护的模块

模組:PeriodicTable/utils

维基百科,自由的百科全书
跳转到导航 跳转到搜索
local p = {}

p.element_lib = require( 'Module:Element' )
p.element_data = mw.loadData( 'Module:Element/data' )
p.p_data = require( 'Module:PeriodicTable/data' )
p.table_models = require( 'Module:PeriodicTable/models' )

function p.getHalflifeStyle(element)
	if element.stability then
		local stability = element.stability
		if stability.stableCount and stability.stableCount > 0 then
			for v, stables in ipairs(p.p_data.halflifestyle.stables) do
				if ( stability.stableCount >= stables.stables) then
					return stables.styleclass
				end
			end
		end
		if stability.halflife then
			local halflife = tonumber(stability.halflife)
			for v, halflifes in ipairs(p.p_data.halflifestyle.halflifes) do
				if ( halflife >= halflifes.halflife) then
					return halflifes.styleclass
				end
			end
		end
	end
	return '';
end

function p.getTimeDesc(seconds)
	string.format( "%.2f", 0.2453435 )
	if seconds < 1e-3 then
		local digs = math.floor(math.log10( seconds ))
		local first_dig = seconds / math.pow( 10, digs )
		return string.format("%.2f",first_dig) .. '×10^' .. string.format( "%d",digs) .. '秒'
	elseif seconds < 0.1 then --ms
		local val = seconds / (1e-3)
		return string.format( "%.2f", val) .. '毫秒'
	elseif seconds < 60 then --seconds
		return string.format( "%.2f", seconds) .. '秒'
	elseif seconds < 3600 then --min
		local val = seconds / 60.0
		return string.format( "%.2f", val) .. '分鐘'
	elseif seconds < 86400 then --hour
		local val = seconds / 3600.0
		return string.format( "%.2f", val) .. '小時'
	elseif seconds < 2.592e6 then --day
		local val = seconds / 86400.0
		return string.format( "%.2f", val) .. '天'
	elseif seconds < 3.1536e7 then --month
		local val = seconds / 2.592e6
		return string.format( "%.2f", val) .. '個月'
	elseif seconds < 3.1536e11 then --years
		local val = seconds / 3.1536e7
		return string.format( "%.2f", val) .. '年'
	elseif seconds < 3.1536e15 then --ten_KYear
		local val = seconds / 3.1536e11
		return string.format( "%.2f", val) .. '萬年'
	elseif seconds < 3.1536e19 then --ten_MYear
		local val = seconds / 3.1536e15
		return string.format( "%.2f", val) .. '億年'
	else
		local years = seconds / 3.1536e7
		local digs = math.floor(math.log10( years ))
		local first_dig = years / math.pow( 10, digs )
		return string.format( "%.2f", first_dig) .. '×10^' .. string.format( "%d",digs) .. '年'
	end
end

function p.getSeriesData(input_name)
	for v, x in ipairs(p.p_data.series_data) do                                
		if (x.page == input_name) then
			return p.p_data.series_data[v]
		end
		for v1, x1 in ipairs(x.name) do
			if (x1 == input_name) then
				return p.p_data.series_data[v]
			end
		end
	end
end
function p.compareSeriesList(left_list, right_list)
	for left_index, left_value in ipairs(left_list) do
		for right_index, right_value in ipairs(right_list) do
			if left_value.name and right_value.name then
				if left_value.name[1] == right_value.name[1] then return true end
			end
		end
	end
	return false
end
function p.getSeriesDataByString(input_str)
	local series_list = {}; local series_list_n = 0
	local input_list = {}
	if string.find( input_str, ',') then
		input_list=mw.text.split(input_str, ',')
	else input_list={input_str}
	end
	for v, x in ipairs(input_list) do
		local result_series = p.getSeriesData(x)
		if result_series then
			series_list[series_list_n + 1] = result_series
			series_list_n = series_list_n + 1
		else
			local ele_check = p.element_lib.getListID(x)
			local predicted = false
			if ele_check > 0 then
				local ele1 = p.element_data[ele_check]
				if ele1.series then
					if (ele1.predictedSeries and ele1.predictedSeries ~= '') then predicted=true end
					for v_ele, x_ele in ipairs(ele1.series) do
						result_series = p.getSeriesData(x_ele)
						if result_series.name and result_series.name[1] ~= "金屬" then 
							if predicted then result_series.predicted = true end
							result_series.fromElement = ele1.name
							if result_series then
								series_list[series_list_n + 1] = result_series
								series_list_n = series_list_n + 1
							end
						end
					end
				end
			end
		end
	end
	return series_list
end

function p.checkElementList(element, list)
	local check_list = mw.text.split(table.concat(element.othername or {},',') .. ',' ..
		element.name .. ',' .. (element.page or element.name) .. ',' .. element.Symbol, ',')
	if(list)then
		for vl, xl in ipairs(element.othername) do
			for vr, xr in ipairs(list) do
				if mw.text.trim(xl) == mw.text.trim(xr) then return true end
			end
		end
	end
	return false
end

function p.get_group_list(pmodel,render_period,lanthanideBelow,superactinidesBelow,transitionBelow,option)
	local group_list = {1,2,19,20,21,22,23,24,25,26,27,28,29,30,31,32,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
	if superactinidesBelow==false then 
		local max_g = 18 if pmodel == 'fricke' then max_g = 20 elseif pmodel == 'nefedov' then max_g = 22 end group_list = {1,2}
		pre_group_list = {19,20,21,22,23,24,25,26,27,28,29,30,31,32,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18} 
		for g_it = 1,max_g do group_list[#group_list+1] = 32 + g_it end
		for g_it = 1,#pre_group_list do group_list[#group_list+1] = pre_group_list[g_it] end
	end
	if lanthanideBelow==true or render_period < 6 then group_list = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18} end
	if render_period < 4 then group_list = {1,2,13,14,15,16,17,18} 
	elseif render_period < 2 then group_list = {1,18} end
	if transitionBelow==true and render_period < 6 and render_period > 3 then
		group_list = {1,2,3,13,14,15,16,17,18}
	end
	if option.groupList and type(option.groupList) == type('0') then
		local group_lists = mw.text.split(option.groupList,',')
		if type(group_lists) == type({'0'}) then
			local group_check_list = {}
			for _,group_num_str in ipairs(group_lists) do
				local group_num=tonumber(group_num_str)
				if group_num then group_check_list[#group_check_list+1] = tonumber(group_num) end
			end
			if #group_check_list > 0 then group_list = group_check_list end
		end
	end
	
	return group_list
end

function p.below_tweaking(iteratorElement,transitionBelow,lanthanideBelow,superactinidesBelow)
	local iteratorElement_temp = {}
	for v,k in pairs(iteratorElement) do iteratorElement_temp[v] = k end
	if transitionBelow then
		if iteratorElement_temp.Z == 21 or iteratorElement_temp.Z == 39 then
			iteratorElement_temp.name='過渡系'
			iteratorElement_temp.page='过渡金属'
		end 
		if iteratorElement_temp.Z == 21 then
			iteratorElement_temp.Symbol='第一'
			iteratorElement_temp.Z = "21-30"
		elseif iteratorElement_temp.Z == 39 then
			iteratorElement_temp.Symbol='第二'
			iteratorElement_temp.Z = "39-48"
		end
	end
	if lanthanideBelow then
		if iteratorElement_temp.Z == 71 or iteratorElement_temp.Z == 103 then
			iteratorElement_temp.name='元素'
			iteratorElement_temp.page='元素'
		end 
		if iteratorElement_temp.Z == 71 then
			iteratorElement_temp.Symbol='鑭系'
			iteratorElement_temp.page='镧系'..iteratorElement_temp.page
			iteratorElement_temp.Z = "57-71"
		elseif iteratorElement_temp.Z == 103 then
			iteratorElement_temp.Symbol='錒系'
			iteratorElement_temp.page='锕系'..iteratorElement_temp.page
			iteratorElement_temp.Z = "89-103"
		end
	end
	return iteratorElement_temp
end

return p