File: issn.lua

package info (click to toggle)
lua-uri 0.1%2B20130926%2Bgit14fa255d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 408 kB
  • ctags: 332
  • sloc: makefile: 53
file content (65 lines) | stat: -rw-r--r-- 1,840 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
local M = { _NAME = "uri.urn.issn" }
local Util = require "uri._util"
local URN = require "uri.urn"
Util.subclass_of(M, URN)

local function _parse_issn (issn)
    local _, _, nums1, nums2, checksum
        = issn:find("^(%d%d%d%d)-?(%d%d%d)([%dxX])$")
    if checksum == "x" then checksum = "X" end
    return nums1, nums2, checksum
end

local function _valid_issn (issn)
    local nums1, nums2, actual_checksum = _parse_issn(issn)
    if not nums1 then return nil, "invalid ISSN syntax" end
    local nums = nums1 .. nums2

    local expected_checksum = 0
    for i = 1, 7 do
        expected_checksum = expected_checksum + tonumber(nums:sub(i, i)) * (9 - i)
    end
    expected_checksum = (11 - expected_checksum % 11) % 11
    expected_checksum = (expected_checksum == 10) and "X"
                                                  or tostring(expected_checksum)
    if actual_checksum ~= expected_checksum then
        return nil, "wrong checksum, expected " .. expected_checksum
    end

    return true
end

local function _normalize_issn (issn)
    local nums1, nums2, checksum = _parse_issn(issn)
    return nums1 .. "-" .. nums2 .. checksum
end

function M.init (self)
    local nss = self:nss()
    local ok, msg = _valid_issn(nss)
    if not ok then return nil, "bad NSS value for ISSN URI (" .. msg .. ")" end
    M._SUPER.nss(self, _normalize_issn(nss))
    return self
end

function M.nss (self, new)
    local old = M._SUPER.nss(self)

    if new then
        local ok, msg = _valid_issn(new)
        if not ok then
            error("bad ISSN value '" .. new .. "' (" .. msg .. ")", 2)
        end
        M._SUPER.nss(self, _normalize_issn(new))
    end

    return old
end

function M.issn_digits (self, new)
    local old = self:nss(new)
    return old:sub(1, 4) .. old:sub(6, 9)
end

return M
-- vi:ts=4 sw=4 expandtab