Module:FormatISBN

From Wikipedia

Documentation for this module may be created at Module:FormatISBN/doc

local p = {}
function SetupArrays()
-- 1011 rules generated from document dated Wed, 29 Aug 2012 18:12:16 GMT.
-- These are the lower limits of the range.
local levels = {1000000000000, 9780000000000, 9780200000000, 9780700000000, 9780850000000,
9780900000000, 9780950000000, 9781000000000, 9781100000000, 9781400000000,
9781550000000, 9781869800000, 9781999000000, 9782000000000, 9782200000000,
9782350000000, 9782400000000, 9782700000000, 9782840000000, 9782900000000,
9782950000000, 9783000000000, 9783030000000, 9783034000000, 9783037000000,
9783040000000, 9783200000000, 9783700000000, 9783850000000, 9783900000000,
9783950000000, 9783954000000, 9783970000000, 9783990000000, 9783995000000,
9784000000000, 9784200000000, 9784700000000, 9784850000000, 9784900000000,
9784950000000, 9785000000000, 9785200000000, 9785421000000, 9785430000000,
9785431000000, 9785440000000, 9785441000000, 9785450000000, 9785700000000,
9785850000000, 9785900000000, 9785910000000, 9785920000000, 9785930000000,
9785950000000, 9785950100000, 9785980000000, 9785990000000, 9785991000000,
9786000000000, 9786001000000, 9786005000000, 9786009000000, 9786010000000,
9786012000000, 9786017000000, 9786018000000, 9786018500000, 9786020000000,
9786021800000, 9786021900000, 9786022000000, 9786027500000, 9786028000000,
9786029500000, 9786030000000, 9786030500000, 9786035000000, 9786038000000,
9786039000000, 9786040000000, 9786045000000, 9786049000000, 9786049800000,
9786050000000, 9786050100000, 9786051000000, 9786054000000, 9786056000000,
9786059000000, 9786060000000, 9786061000000, 9786065000000, 9786068000000,
9786069200000, 9786070000000, 9786074000000, 9786077500000, 9786079500000,
9786080000000, 9786081000000, 9786082000000, 9786084500000, 9786086500000,
9786087000000, 9786090000000, 9786094000000, 9786098000000, 9786099500000,
9786100000000, 9786110000000, 9786120000000, 9786123000000, 9786124000000,
9786124500000, 9786125000000, 9786130000000, 9786140000000, 9786144000000,
9786148000000, 9786149500000, 9786150000000, 9786151000000, 9786155000000,
9786158000000, 9786159000000, 9786160000000, 9786162000000, 9786167000000,
9786169000000, 9786170000000, 9786175000000, 9786177000000, 9786179000000,
9786180000000, 9786182000000, 9786185000000, 9786188000000, 9786190000000,
9786191500000, 9786197000000, 9786199000000, 9786200000000, 9786210000000,
9787000000000, 9787100000000, 9787500000000, 9787800000000, 9787900000000,
9788000000000, 9788020000000, 9788070000000, 9788085000000, 9788090000000,
9788100000000, 9788120000000, 9788170000000, 9788185000000, 9788190000000,
9788200000000, 9788220000000, 9788270000000, 9788290000000, 9788299000000,
9788300000000, 9788320000000, 9788360000000, 9788370000000, 9788385000000,
9788390000000, 9788400000000, 9788414000000, 9788415000000, 9788420000000,
9788470000000, 9788485000000, 9788490000000, 9788492000000, 9788492400000,
9788493000000, 9788495000000, 9788497000000, 9788500000000, 9788520000000,
9788560000000, 9788570000000, 9788585000000, 9788590000000, 9788598000000,
9788600000000, 9788630000000, 9788660000000, 9788680000000, 9788690000000,
9788700000000, 9788730000000, 9788740000000, 9788765000000, 9788770000000,
9788780000000, 9788785000000, 9788795000000, 9788797000000, 9788800000000,
9788820000000, 9788860000000, 9788885000000, 9788890000000, 9788891000000,
9788893000000, 9788895000000, 9788900000000, 9788925000000, 9788955000000,
9788985000000, 9788995000000, 9788997000000, 9788999000000, 9789000000000,
9789020000000, 9789050000000, 9789070000000, 9789080000000, 9789085000000,
9789090000000, 9789091000000, 9789094000000, 9789095000000, 9789100000000,
9789120000000, 9789150000000, 9789165000000, 9789170000000, 9789180000000,
9789185000000, 9789195000000, 9789197000000, 9789200000000, 9789260000000,
9789280000000, 9789290000000, 9789295000000, 9789299000000, 9789300000000,
9789310000000, 9789350000000, 9789380000000, 9789395000000, 9789400000000,
9789460000000, 9789490000000, 9789500000000, 9789505000000, 9789509000000,
9789509900000, 9789510000000, 9789512000000, 9789515500000, 9789518900000,
9789519500000, 9789520000000, 9789522000000, 9789525000000, 9789526000000,
9789526600000, 9789526700000, 9789527000000, 9789528000000, 9789529500000,
9789529900000, 9789530000000, 9789531000000, 9789531500000, 9789535100000,
9789535500000, 9789536000000, 9789539500000, 9789540000000, 9789542900000,
9789543000000, 9789548000000, 9789549000000, 9789549300000, 9789550000000,
9789552000000, 9789554500000, 9789555000000, 9789555500000, 9789558000000,
9789559500000, 9789560000000, 9789562000000, 9789567000000, 9789570000000,
9789570300000, 9789570500000, 9789572000000, 9789572100000, 9789572800000,
9789573100000, 9789574400000, 9789578200000, 9789579700000, 9789580000000,
9789585700000, 9789586000000, 9789588000000, 9789589500000, 9789590000000,
9789592000000, 9789597000000, 9789598500000, 9789600000000, 9789602000000,
9789606600000, 9789606900000, 9789607000000, 9789608500000, 9789609300000,
9789609400000, 9789609800000, 9789610000000, 9789612000000, 9789616000000,
9789619000000, 9789619500000, 9789620000000, 9789622000000, 9789627000000,
9789628500000, 9789628700000, 9789629000000, 9789630000000, 9789632000000,
9789637000000, 9789638500000, 9789639000000, 9789640000000, 9789641500000,
9789642500000, 9789643000000, 9789645500000, 9789649000000, 9789649700000,
9789649900000, 9789650000000, 9789652000000, 9789656000000, 9789657000000,
9789658000000, 9789659000000, 9789660000000, 9789661300000, 9789661400000,
9789661500000, 9789661700000, 9789662000000, 9789662790000, 9789662900000,
9789663000000, 9789667000000, 9789669000000, 9789669100000, 9789669500000,
9789669800000, 9789670000000, 9789670100000, 9789671000000, 9789672000000,
9789673000000, 9789675000000, 9789676000000, 9789679000000, 9789679900000,
9789679990000, 9789680000000, 9789680100000, 9789684000000, 9789685000000,
9789688000000, 9789689000000, 9789690000000, 9789692000000, 9789694000000,
9789698000000, 9789700000000, 9789700100000, 9789706000000, 9789709000000,
9789709100000, 9789709700000, 9789710000000, 9789710160000, 9789710200000,
9789710300000, 9789710600000, 9789711000000, 9789715000000, 9789718500000,
9789719100000, 9789719600000, 9789719700000, 9789719900000, 9789720000000,
9789722000000, 9789725500000, 9789728000000, 9789729500000, 9789730000000,
9789731000000, 9789731700000, 9789732000000, 9789735500000, 9789737600000,
9789738500000, 9789738900000, 9789739500000, 9789740000000, 9789742000000,
9789747000000, 9789748500000, 9789749000000, 9789749500000, 9789750000000,
9789750200000, 9789752500000, 9789756000000, 9789759200000, 9789759900000,
9789760000000, 9789764000000, 9789766000000, 9789768000000, 9789769500000,
9789770000000, 9789772000000, 9789775000000, 9789777000000, 9789778500000,
9789779000000, 9789780000000, 9789782000000, 9789783000000, 9789788000000,
9789789000000, 9789790000000, 9789791000000, 9789791500000, 9789792000000,
9789793000000, 9789794000000, 9789798000000, 9789799500000, 9789800000000,
9789802000000, 9789806000000, 9789810000000, 9789811200000, 9789812000000,
9789812900000, 9789820000000, 9789821000000, 9789827000000, 9789829000000,
9789829800000, 9789830000000, 9789830200000, 9789832000000, 9789834000000,
9789834500000, 9789835000000, 9789838000000, 9789839000000, 9789839900000,
9789840000000, 9789844000000, 9789848000000, 9789849000000, 9789850000000,
9789854000000, 9789856000000, 9789859000000, 9789860000000, 9789861200000,
9789865600000, 9789868000000, 9789870000000, 9789871000000, 9789872000000,
9789873000000, 9789875000000, 9789879000000, 9789879500000, 9789880000000,
9789881500000, 9789881700000, 9789882000000, 9789888000000, 9789889700000,
9789890000000, 9789892000000, 9789895500000, 9789898000000, 9789899500000,
9789900000000, 9789927000000, 9789927100000, 9789927400000, 9789927500000,
9789928000000, 9789928100000, 9789928400000, 9789928500000, 9789929000000,
9789929400000, 9789929550000, 9789929800000, 9789930000000, 9789930500000,
9789930940000, 9789931000000, 9789931300000, 9789931900000, 9789932000000,
9789932400000, 9789932850000, 9789933000000, 9789933100000, 9789933400000,
9789933900000, 9789934000000, 9789934100000, 9789934500000, 9789934800000,
9789935000000, 9789935100000, 9789935400000, 9789935900000, 9789936000000,
9789936200000, 9789936400000, 9789936800000, 9789937000000, 9789937300000,
9789937500000, 9789937800000, 9789938000000, 9789938800000, 9789938950000,
9789939000000, 9789939500000, 9789939800000, 9789939900000, 9789940000000,
9789940200000, 9789940500000, 9789940900000, 9789941000000, 9789941100000,
9789941400000, 9789941900000, 9789942000000, 9789942900000, 9789942985000,
9789943000000, 9789943300000, 9789943400000, 9789944000000, 9789944100000,
9789944500000, 9789944600000, 9789944700000, 9789944800000, 9789944900000,
9789945000000, 9789945010000, 9789945080000, 9789945400000, 9789945570000,
9789945580000, 9789945850000, 9789946000000, 9789946200000, 9789946400000,
9789946900000, 9789947000000, 9789947200000, 9789947800000, 9789948000000,
9789948400000, 9789948850000, 9789949000000, 9789949100000, 9789949400000,
9789949900000, 9789950000000, 9789950300000, 9789950850000, 9789951000000,
9789951400000, 9789951850000, 9789952000000, 9789952200000, 9789952400000,
9789952800000, 9789953000000, 9789953100000, 9789953400000, 9789953600000,
9789953900000, 9789954000000, 9789954200000, 9789954400000, 9789954800000,
9789955000000, 9789955400000, 9789955930000, 9789956000000, 9789956100000,
9789956400000, 9789956900000, 9789957000000, 9789957400000, 9789957700000,
9789957850000, 9789957880000, 9789958000000, 9789958040000, 9789958090000,
9789958100000, 9789958190000, 9789958200000, 9789958500000, 9789958900000,
9789959000000, 9789959200000, 9789959800000, 9789959950000, 9789959970000,
9789959980000, 9789960000000, 9789960600000, 9789960900000, 9789961000000,
9789961300000, 9789961700000, 9789961950000, 9789962000000, 9789962550000,
9789962560000, 9789962600000, 9789962850000, 9789963000000, 9789963200000,
9789963250000, 9789963280000, 9789963300000, 9789963550000, 9789963735000,
9789963750000, 9789964000000, 9789964700000, 9789964950000, 9789965000000,
9789965400000, 9789965900000, 9789966000000, 9789966150000, 9789966200000,
9789966700000, 9789966750000, 9789966960000, 9789967000000, 9789967400000,
9789967900000, 9789968000000, 9789968500000, 9789968940000, 9789969000000,
9789970000000, 9789970400000, 9789970900000, 9789971000000, 9789971600000,
9789971900000, 9789971990000, 9789972000000, 9789972100000, 9789972200000,
9789972250000, 9789972300000, 9789972600000, 9789972900000, 9789973000000,
9789973060000, 9789973090000, 9789973100000, 9789973700000, 9789973970000,
9789974000000, 9789974300000, 9789974550000, 9789974750000, 9789974950000,
9789975000000, 9789975100000, 9789975400000, 9789975450000, 9789975900000,
9789975950000, 9789976000000, 9789976600000, 9789976900000, 9789976990000,
9789977000000, 9789977900000, 9789977990000, 9789978000000, 9789978300000,
9789978400000, 9789978950000, 9789978990000, 9789979000000, 9789979500000,
9789979650000, 9789979660000, 9789979760000, 9789979900000, 9789980000000,
9789980400000, 9789980900000, 9789980990000, 9789981000000, 9789981100000,
9789981160000, 9789981200000, 9789981800000, 9789981950000, 9789982000000,
9789982800000, 9789982990000, 9789983000000, 9789983800000, 9789983950000,
9789983990000, 9789984000000, 9789984500000, 9789984900000, 9789985000000,
9789985500000, 9789985800000, 9789985900000, 9789986000000, 9789986400000,
9789986900000, 9789986940000, 9789986970000, 9789987000000, 9789987400000,
9789987880000, 9789988000000, 9789988300000, 9789988550000, 9789988750000,
9789989000000, 9789989100000, 9789989200000, 9789989300000, 9789989600000,
9789989950000, 9789990000000, 9789990100000, 9789990150000, 9789990180000,
9789990200000, 9789990300000, 9789990320000, 9789990390000, 9789990400000,
9789990460000, 9789990490000, 9789990500000, 9789990540000, 9789990580000,
9789990600000, 9789990630000, 9789990660000, 9789990670000, 9789990690000,
9789990695000, 9789990700000, 9789990800000, 9789990810000, 9789990890000,
9789990900000, 9789990940000, 9789990995000, 9789991000000, 9789991030000,
9789991090000, 9789991100000, 9789991160000, 9789991200000, 9789991240000,
9789991260000, 9789991290000, 9789991300000, 9789991330000, 9789991336000,
9789991360000, 9789991360500, 9789991400000, 9789991450000, 9789991490000,
9789991500000, 9789991550000, 9789991580000, 9789991600000, 9789991630000,
9789991670000, 9789991700000, 9789991730000, 9789991790000, 9789991800000,
9789991840000, 9789991880000, 9789991900000, 9789991930000, 9789991940000,
9789991970000, 9789991980000, 9789991985000, 9789991990000, 9789992000000,
9789992050000, 9789992090000, 9789992100000, 9789992120000, 9789992170000,
9789992180000, 9789992190000, 9789992200000, 9789992240000, 9789992270000,
9789992300000, 9789992320000, 9789992380000, 9789992400000, 9789992420000,
9789992480000, 9789992500000, 9789992540000, 9789992580000, 9789992600000,
9789992610000, 9789992660000, 9789992690000, 9789992700000, 9789992730000,
9789992760000, 9789992800000, 9789992810000, 9789992880000, 9789992900000,
9789992950000, 9789992980000, 9789993000000, 9789993050000, 9789993080000,
9789993100000, 9789993150000, 9789993180000, 9789993200000, 9789993210000,
9789993260000, 9789993270000, 9789993280000, 9789993300000, 9789993330000,
9789993360000, 9789993400000, 9789993420000, 9789993480000, 9789993500000,
9789993530000, 9789993560000, 9789993570000, 9789993590000, 9789993600000,
9789993610000, 9789993660000, 9789993700000, 9789993720000, 9789993760000,
9789993800000, 9789993820000, 9789993860000, 9789993890000, 9789993900000,
9789993960000, 9789993990000, 9789994000000, 9789994010000, 9789994070000,
9789994100000, 9789994130000, 9789994180000, 9789994200000, 9789994250000,
9789994280000, 9789994300000, 9789994330000, 9789994360000, 9789994400000,
9789994450000, 9789994480000, 9789994500000, 9789994560000, 9789994590000,
9789994600000, 9789994630000, 9789994660000, 9789994700000, 9789994730000,
9789994770000, 9789994796000, 9789994800000, 9789994850000, 9789994880000,
9789994900000, 9789994920000, 9789994990000, 9789995000000, 9789995050000,
9789995080000, 9789995100000, 9789995200000, 9789995250000, 9789995280000,
9789995300000, 9789995330000, 9789995380000, 9789995394000, 9789995400000,
9789995430000, 9789995470000, 9789995488000, 9789995500000, 9789995520000,
9789995560000, 9789995580000, 9789995590000, 9789995600000, 9789995660000,
9789995686000, 9789995700000, 9789995720000, 9789995780000, 9789995800000,
9789995850000, 9789995895000, 9789995900000, 9789995930000, 9789995960000,
9789996000000, 9789996010000, 9789996095000, 9789996100000, 9789996140000,
9789996190000, 9789996200000, 9789996250000, 9789996280000, 9789996300000,
9789996350000, 9789996400000, 9789996420000, 9789996480000, 9789996500000,
9789996540000, 9789996580000, 9789996600000, 9789996630000, 9789996670000,
9789996680000, 9789996690000, 9789996700000, 9789996720000, 9789996760000,
9789996790000, 9789996800000, 9789996840000, 9789996860000, 9789996890000,
9789996900000, 9789996950000, 9789996980000, 9789997000000, 9789997050000,
9789997090000, 9789997100000, 9789997160000, 9789997185000, 9789997200000,
9791000000000, 9791020000000, 9791070000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}
return levels, lengroup, lenpub
end

function ClearCurrentFormatting(ISBN)
    -- This to remove spaces, dashes, change 'x' to 'X'
	local clearFormatted, count = ISBN:gsub("[ %-]", ""):gsub("x", "X")
	return clearFormatted
end

function ValidateChecksum(ISBN, tenDigitISBN)
    -- function expects ISBN to be a string of 13 chars
	local cs = 0, givenchecksum, k1, checkSum
	if ISBN:sub(13) == 'X' then
		givenCheckSum = 10
	else
		givenCheckSum = tonumber(ISBN:sub(13))
	end

	if tenDigitISBN then
		for k1 = 1, 9 do
			cs = cs + tonumber(ISBN:sub(3+k1, 3+k1)) * k1
		end
		checkSum = cs % 11
	else
		for k1 = 1, 12, 2 do
			cs = cs + tonumber(ISBN:sub(k1, k1)) * 1
		end
		for k1 = 2, 12, 2 do
			cs = cs + tonumber(ISBN:sub(k1, k1)) * 3
		end
		checkSum = (10 - cs % 10) % 10
	end

	if checkSum == givenCheckSum then return true else return false end
end

function ValidateChars(ISBN, tenDigitISBN)
	-- Check for invalid letters
	local k1
	if tenDigitISBN and ISBN:sub(13) == "X" then
		ISBN = ISBN:sub(1,12)
	end
	for k1 = 1, ISBN:len() do
		if ISBN:sub(k1,k1) < '0' or ISBN:sub(k1,k1) > '9' then
			return false
		end
	end
	return true
end

function ErrorMessage(error)
    -- This to return messages.
    return "<span style=\"color:red\"> ISBN fault: " .. error .. "</span>"
end

function p.CheckRangeAndDash(frame)
	-- nS should be a string to avoid leading zero nastiness
	local raw = frame.args[1]
	-- Declare variables ...
	-- strings ...
	local nS = ClearCurrentFormatting(raw), formattedString
	-- numbers ...
	local levels, fieldWidths, rangeIsValid, lowerLimit, upperLimit, midPoint, ISBNasInteger
	local s1, e1, s2, e2, s3, e3, s4, e4, s5, e5
	-- bools ...
	local tenDigitISBN, checkSumIsGood
	-- functions ...
	local mathfloor = math.floor

	-- Check length.
	if nS:len() == 13 then
		tenDigitISBN = false
	elseif nS:len() == 10 then
		tenDigitISBN = true
		nS = "978" .. nS
	else
		return nS .. ErrorMessage("not 10 or 13 digits")
	end

	if not ValidateChars(nS, tenDigitISBN) then
		return nS .. ErrorMessage("invalid character")
	end

    -- Create an integer from the ISBN and load the arrays.
	ISBNasInteger = tonumber(nS:sub(1, 12)) * 10
	levels, lengroup, lenpub = SetupArrays()

	-- Find the relevant bit of the arrays ...
	lowerLimit = 1
	upperLimit = table.getn(levels) + 1
	midPoint = mathfloor((lowerLimit + upperLimit) * 0.5)

	repeat
		if ISBNasInteger >= levels[midPoint] then
			lowerLimit = midPoint
		else
			upperLimit = midPoint
		end
		midPoint = mathfloor((lowerLimit + upperLimit) * 0.5)
	until (midPoint == lowerLimit)

	if (lenpub[lowerLimit] == 0) then
		return nS .. ErrorMessage("invalid range")
	end

	-- Create the start and ends of the string, create the output string.
	s1, e1 = 1, 3
	s2, e2 = 4, 3 + lengroup[lowerLimit]
	s3, e3 = e2 + 1, e2 + lenpub[lowerLimit]
	s4, e4 = e3 + 1, 12
	s5, e5 = 13, 13

	formattedString = string.format("%s-%s-%s-%s-%s",
		nS:sub(s1,e1), nS:sub(s2,e2), nS:sub(s3,e3), nS:sub(s4,e4), nS:sub(s5,e5))

	if tenDigitISBN then formattedString = formattedString:sub(5) end

	checkSumIsGood = ValidateChecksum(nS, tenDigitISBN)

	if checkSumIsGood then
		return formattedString
	else
		return formattedString .. ErrorMessage("bad checksum")
	end

end
return p