Module:Check ISIN

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

require('Module:No globals') local arguments = require('Module:Arguments')

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 isin:gmatch(".") 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, sum = string_:reverse, 0 for i = 1, rev:len do		local digit = ((i % 2) == 0 and 2 or 1) * tonumber(rev:sub(i, i)) sum = sum + digit - (digit > 9 and 9 or 0) end return (sum % 10) == 0 end

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

function p._check(args) local isin = args["isin"] or args[1] or "" if _check_isin(isin) then return args["valid"] or args[2] or "valid" else return args["invalid"] or args[3] or "invalid" end end

function p.check(frame) local args = arguments.getArgs(frame, {trim = false, removeBlanks = false}) return p._check(args) end

return p