Module:Check ISIN

-- Module to check ISIN numbers. -- Example usage: --  -- Console example: --  mw.log(p.check({args={isin="IE00B4L5Y983"}}))

require('Module:No globals')

local p = {}

-- https://en.wikipedia.org/wiki/International_Securities_Identification_Number#Examples -- Letter mapping is base-36: A->10 to Z->35 local function _make_numeric(isin) local numeric = "" for c in string.gmatch(isin, ".") do numeric = numeric .. tostring(tonumber(c, 36)) end return numeric end

-- https://en.wikipedia.org/wiki/Luhn_algorithm local function _check_luhn(string_) local rev = string.reverse(string_) local sum = tonumber(string.sub(rev, 1, 1)) for i = 2, string.len(rev) do		local digit = ((i % 2) == 0 and 2 or 1) * tonumber(string.sub(rev, i, i)) sum = sum + digit - (digit > 9 and 9 or 0) end return (sum % 10) == 0 end

local function _check_isin(isin) isin = string.upper(isin) if string.len(isin) == 12 and string.match(isin, "^%u%u[%u%d]+%d$") then return _check_luhn(_make_numeric(isin)) end return false end

local function __check_isin(isin, valid, invalid) return _check_isin(isin) and valid or invalid end

function p.check(frame) local args = frame.args return __check_isin(			args["isin"] or args[1] or "",			args["valid"] or args[2] or "valid",			args["invalid"] or args[3] or "invalid"		) end

return p