模块:Chemicals
此模块被引用于约27,000个页面。 为了避免造成大规模的影响,所有对此模块的编辑应先于沙盒或测试样例上测试。 测试后无误的版本可以一次性地加入此模块中,但是修改前请务必于讨论页发起讨论。 模板引用数量会自动更新。 |
Module:Chemicals(编辑 | 讨论 | 历史 | 链接 | 监视 | 日志)
本模组中包含一些与化学相关的函式
formula
本函式功能为查询Module:Element/data资料并产生化学式
Usage:
{{#invoke:Chemicals|formula|<template-argument>}}
Parameters:
- <template-argument>
- 要建构化学式的资料,必须使用模板引用传递参数
- 例如模板中写 :
{{#invoke:Chemicals|formula}}
- 叫用模板 :
{{模板名稱|H|2|O}}
→ H2O
- 例如模板中写 :
reaction
本函式功能为查询Module:Element/data资料并产生反应式
Usage:
{{#invoke:Chemicals|reaction|<template-argument>}}
Parameters:
- <template-argument>
- 要建构化学反应式的资料,必须使用模板引用传递参数
check_CAS
- 函数原型(以C语言表示):
boolean_string 模块::Chemicals::check_CAS ( const string cas_no );
本函式功能为查询检查CAS号的格式与校验码是否正确,仅能由其他模组呼叫。
Datatype:
- boolean_string
- 表示布林值的字串,其值仅会有
"true"
或"false"
Parameters:
- cas_no
- 要判断的CAS号字串
check_CAS_test
本函式功能为查询检查CAS号的格式与校验码是否正确,可由模板呼叫。
Usage:
{{#invoke:Chemicals|check_CAS_test|1=<CAS_Number>}}
Parameters:
- <CAS_Number>
- 要判断CAS号是否正确的CAS号字串
- 例如 :
{{#invoke:Chemicals|check_CAS_test|1=12-35-1}}
→ true- 通常会搭配
{{#ifeq:...|true}}
或{{#ifeq:...|false}}
使用
- 通常会搭配
- 例如 :
{{#ifeq:{{#invoke:Chemicals|check_CAS_test|1=12-35-4}}|true|正確|錯誤}}
→ 错误{{#ifeq:{{#invoke:Chemicals|check_CAS_test|1=12-35-1}}|false|{{Nay}}檢查碼校驗失敗|{{Aye}}檢查碼校驗成功}}
→ 检查码校验成功
- 例如 :
local p = {}
local origArgs
local error = require( 'Module:Error' )
local element = {}
local lib_arg = {}
local strings = require( 'Module:String' )
local element_data = {}
function p.formula(frame)
-- For calling from #invoke.
local args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame) --frame.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
end
return p._formula(args)
end
function p.reaction(frame)
-- For calling from #invoke.
local args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame) --frame.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
end
return p._reaction(args)
end
function p.check_CAS(cas_no)
arrs = strings.split(cas_no, '-', true, false)
split_num = tonumber(table.getn(arrs))
--avoid nil
if (split_num and split_num ~= '') then
if (split_num == 3) then
--cas no have 3 pattern
else
return 'false'
end
else
--if nil
return 'false'
end
cas_without_desh = arrs[1] .. arrs[2]
check_length = {} -- new array
for seq_it=1, 3 do
check_length[seq_it] = mw.ustring.len(arrs[seq_it])
end
if (check_length[1] < 2 or check_length[1] > 7) then return 'false' end
if (check_length[2] ~= 2) then return 'false' end
if (check_length[3] ~= 1) then return 'false' end
cas_check = tonumber(arrs[3])
if (cas_check and cas_check ~= '') then
else
return 'false'
end
cas_len = mw.ustring.len(cas_without_desh)
cas_sum = 0
for i = 1,cas_len do
index = cas_len + 1 -i
cas_symbol = tonumber(cas_without_desh:sub(index,index))
if (cas_symbol and cas_symbol ~= '') then
cas_sum = cas_sum + (cas_symbol*i)
else
return 'false'
end
end
if ((cas_sum % 10) == cas_check) then
return 'true'
else
return 'false'
end
return 'false'
end
function p.check_CAS_test(frame)
-- For calling from #invoke.
local load_args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
load_args = lib_arg.getArgs(frame) --frame.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
load_args = frame
end
args = {}
for k, v in pairs( load_args ) do
args[k] = v;
end
return p._check_CAS_test(args)
end
function p._check_CAS_test(args)
-- For calling from #invoke.
if (args[1] and args[1] ~= '') then
casid = args[1]
else
casid = "000-00-0"
end
return p.check_CAS(casid)
end
function put_elements(element_name, count)
body = ''
if element._element_symbol == nil then element = require('Module:Element') end
local check_group = mw.ustring.gsub(element_name,"<官能基/>","")
if element_name ~= check_group then
if( tonumber(count) ~= nil and tonumber(count) > 1 )then
body = body .. '(' .. check_group .. ')' .. "<sub>" .. tostring(count) .. "</sub>"
else body = body .. check_group end
else
local number = 1 if tonumber(count) ~= nil then number = tonumber(count) end
if(link and link ~= '' or link == 'nolink')then
body = body .. element._element_symbol({element_name,number})
else
body = body .. element._elementlink({element_name,number})
end
end
return body
end
function p._formula(args)
local body = ''
if (args['link'] and args['link'] ~= '') then link = args['link'] end
last=''
if element._element_symbol == nil then element = require('Module:Element') end
for v, x in ipairs(args) do
number=tonumber(x)
lastn=tonumber(last)
if(number)then
if((not (lastn)) and last ~= '')then
body = body .. put_elements(last, x)
else if(body == '' and last == '')then
body = x .. body
else if (lastn)then
if(number==1)then
body = body .. '<sup>+</sup>'
else if(number==-1)then
body = body .. '<sup>-</sup>'
else if(number>0)then
body = body .. '<sup>'.. number ..'+</sup>'
else if(number<0)then
body = body .. '<sup>'.. -number ..'−</sup>'
end end end end
end end end
else
if ((not (lastn)) and last ~= '') then
body = body .. put_elements(last, 1)
end
end
last=x
end
lastn=tonumber(last)
if ((not (lastn)) and last ~= '') then
body = body .. put_elements(last, 1)
end
if(link and link ~= '' or link == 'nolink')then
if(link ~= '#' and link ~= 'nolink')then
body = '[[' .. link .. '|' .. body ..']]'
end
end
return body
end
function p._reaction(args)
local body = ''
last=''
if element._elementlink == nil then element = require('Module:Element') end
for v, x in ipairs(args) do
number=tonumber(x)
lastn=tonumber(last)
local check_text = mw.ustring.gsub(last,"<文字/>","")
if(number)then
if((not (lastn)) and last ~= '')then
feq = ''
if(number>1)then feq = feq .. number end
if (last == '+') then
body = body .. ' + ' .. feq
elseif (last == 'eq' or last == '→') then
body = body .. ' → ' .. feq
elseif (last == '↑') then
body = body .. '↑ ' .. feq
elseif (last == '↓') then
body = body .. '↓ ' .. feq
elseif (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] ' .. feq
elseif (last ~= check_text) then
body = body .. check_text .. feq
else
body = body .. put_elements(last, x)
end
else if(body == '' and last == '')then
body = x .. body
else if (lastn)then
if(number==1)then
body = body .. '<sup>+</sup>'
elseif(number==-1)then
body = body .. '<sup>-</sup>'
elseif(number>0)then
body = body .. '<sup>'.. number ..'+</sup>'
elseif(number<0)then
body = body .. '<sup>'.. -number ..'−</sup>'
end
else
body = last .. x .. body
end end end
else
if ((not (lastn)) and last ~= '') then
if (last == '+') then
body = body .. ' + '
elseif (last == 'eq' or last == '→') then
body = body .. ' → '
elseif (last == '↑') then
body = body .. '↑ '
elseif (last == '↓') then
body = body .. '↓ '
elseif (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] '
elseif (last ~= check_text) then
body = body .. check_text
else
body = body .. put_elements(last, 1)
end
end
end
last=x
end
lastn=tonumber(last)
if ((not (lastn)) and last ~= '') then
local check_text = mw.ustring.gsub(last,"<文字/>","")
if (last == '+') then
body = body .. ' + '
elseif (last == 'eq' or last == '→') then
body = body .. ' → '
elseif (last == '↑') then
body = body .. '↑ '
elseif (last == '↓') then
body = body .. '↓ '
elseif (last == 'eqm') then
body = body .. ' [[File:Equilibrium.svg|15px|化学平衡符号]] '
elseif (last ~= check_text) then
body = body .. check_text
else
body = body .. put_elements(last, 1)
end
end
return body
end
--本模塊的沙盒(測試)函數
function p.sandbox(frame)
-- For calling from #invoke.
local args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame) --frame.args
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
end
return p._reaction(args)
end
function p._sandbox(args)
if element.getListID == nil then element = require('Module:Element') end
if element_data[1] == nil then element_data = mw.loadData('Module:Element/data') end
return element_data[element.getListID(args[1])].name
end
return p