Module:Accession numbering: Difference between revisions
From CHIPPS
More actions
Created page with "require("Module:Verhoeff") id_to_n = { R=1, L=2, S=3, A=3, E=4 } function wrm_idproc(str_r, func, pp, gc) str_stripped = str_r:upper():gsub("^WRM%-?", "") prefix = str_stripped:match("^[RLSAE]") res = func(str_stripped:gsub("([RLSAE]?)(%d+)", function(p, ds, x) return (gc and "%u%05u" or "%u%06u"):format(id_to_n[p] or 0, ds) end)) if pp then return ("WRM-%s%04u"):format(prefix or "", res:sub(2)) else return res end end function wrm_..." |
No edit summary |
||
| Line 18: | Line 18: | ||
end | end | ||
function wrm_gencheck(str_r) | function wrm_gencheck(frame) | ||
str_r = type(frame) == "table" and frame.args[1] or frame | |||
return wrm_idproc(str_r, verhoeff_gen, true, true) | return wrm_idproc(str_r, verhoeff_gen, true, true) | ||
end | end | ||
| Line 26: | Line 27: | ||
end | end | ||
function wrm_fmtcheck(idcheck) | function wrm_fmtcheck(frame) | ||
idcheck = type(frame) == "table" and wrm_getcheck_f(frame) or frame | |||
idnocheck = idcheck:sub(1,-2) | idnocheck = idcheck:sub(1,-2) | ||
check = idcheck:sub(-1,-1) | check = idcheck:sub(-1,-1) | ||
| Line 33: | Line 36: | ||
return "<code>"..idnocheck.."<span class=\"check_digit\">".. | return "<code>"..idnocheck.."<span class=\"check_digit\">".. | ||
check.."</span></code>"..invalid_check | check.."</span></code>"..invalid_check | ||
end | end | ||
| Line 44: | Line 43: | ||
end | end | ||
function | function wrm_getcheckforsmw(frame) | ||
c = wrm_getcheck_f(frame) | c = type(frame) == "table" and wrm_getcheck_f(frame) or frame | ||
return c .. (wrm_verifycheck(c) and "" or "-invalid") | return c .. (wrm_verifycheck(c) and "" or "-invalid") | ||
end | end | ||
| Line 56: | Line 55: | ||
function wrm_next_an(frame) | function wrm_next_an(frame) | ||
prepend = type(frame) == " | prepend = type(frame) == "table" and frame.args[1] or frame | ||
return wrm_gencheck(prepend..wrm_next_id()) | return wrm_gencheck(prepend..wrm_next_id()) | ||
end | end | ||
| Line 63: | Line 62: | ||
{ | { | ||
wrm_fmtcheck = wrm_fmtcheck, | wrm_fmtcheck = wrm_fmtcheck, | ||
next_id = wrm_next_id, | next_id = wrm_next_id, | ||
next_an = wrm_next_an, | next_an = wrm_next_an, | ||
fmt = | fmt = wrm_fmtcheck, | ||
get = | get = wrm_getcheckforsmw, | ||
gen = | gen = wrm_gencheck, | ||
} | } | ||
Revision as of 03:10, 5 November 2025
Documentation for this module may be created at Module:Accession numbering/doc
require("Module:Verhoeff")
id_to_n = { R=1, L=2, S=3, A=3, E=4 }
function wrm_idproc(str_r, func, pp, gc)
str_stripped = str_r:upper():gsub("^WRM%-?", "")
prefix = str_stripped:match("^[RLSAE]")
res = func(str_stripped:gsub("([RLSAE]?)(%d+)",
function(p, ds, x)
return (gc and "%u%05u" or "%u%06u"):format(id_to_n[p] or 0, ds)
end))
if pp
then
return ("WRM-%s%04u"):format(prefix or "", res:sub(2))
else
return res
end
end
function wrm_gencheck(frame)
str_r = type(frame) == "table" and frame.args[1] or frame
return wrm_idproc(str_r, verhoeff_gen, true, true)
end
function wrm_verifycheck(str_r)
return wrm_idproc(str_r, verhoeff_verify, false, false)
end
function wrm_fmtcheck(frame)
idcheck = type(frame) == "table" and wrm_getcheck_f(frame) or frame
idnocheck = idcheck:sub(1,-2)
check = idcheck:sub(-1,-1)
invalid_check = wrm_verifycheck(idcheck) and "" or " is invalid!"
return "<code>"..idnocheck.."<span class=\"check_digit\">"..
check.."</span></code>"..invalid_check
end
function wrm_getcheck_f(frame)
pargs = frame:getParent().args
return pargs[1] or wrm_gencheck(pargs[2] or "WRM-R999")
end
function wrm_getcheckforsmw(frame)
c = type(frame) == "table" and wrm_getcheck_f(frame) or frame
return c .. (wrm_verifycheck(c) and "" or "-invalid")
end
function wrm_next_id()
return mw.smw.getQueryResult(
"[[Has AM-ID::+]] [[:+]]|?Has AM-ID=id|sort=Has AM-ID|order=desc|limit=1"
).results[1].printouts.id[1]:sub(6,-2)+1
end
function wrm_next_an(frame)
prepend = type(frame) == "table" and frame.args[1] or frame
return wrm_gencheck(prepend..wrm_next_id())
end
return
{
wrm_fmtcheck = wrm_fmtcheck,
next_id = wrm_next_id,
next_an = wrm_next_an,
fmt = wrm_fmtcheck,
get = wrm_getcheckforsmw,
gen = wrm_gencheck,
}