-
Notifications
You must be signed in to change notification settings - Fork 0
/
isbn.go
121 lines (91 loc) · 2.2 KB
/
isbn.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package checker
import (
"reflect"
"strings"
)
// Program to check for ISBN
// https://www.geeksforgeeks.org/program-check-isbn/
// How to Verify an ISBN
// https://www.instructables.com/How-to-verify-a-ISBN/
// CheckerISBN is the name of the checker.
const CheckerISBN = "isbn"
// ResultNotISBN indicates that the given value is not a valid ISBN.
const ResultNotISBN = "NOT_ISBN"
// IsISBN10 checks if the given value is a valid ISBN-10 number.
func IsISBN10(value string) Result {
value = strings.ReplaceAll(value, "-", "")
if len(value) != 10 {
return ResultNotISBN
}
digits := []rune(value)
sum := 0
for i, e := 0, len(digits); i < e; i++ {
n := isbnDigitToInt(digits[i])
sum += n * (e - i)
}
if sum%11 != 0 {
return ResultNotISBN
}
return ResultValid
}
// IsISBN13 checks if the given value is a valid ISBN-13 number.
func IsISBN13(value string) Result {
value = strings.ReplaceAll(value, "-", "")
if len(value) != 13 {
return ResultNotISBN
}
digits := []rune(value)
sum := 0
for i, d := range digits {
n := isbnDigitToInt(d)
if i%2 != 0 {
n *= 3
}
sum += n
}
if sum%10 != 0 {
return ResultNotISBN
}
return ResultValid
}
// IsISBN checks if the given value is a valid ISBN number.
func IsISBN(value string) Result {
value = strings.ReplaceAll(value, "-", "")
if len(value) == 10 {
return IsISBN10(value)
} else if len(value) == 13 {
return IsISBN13(value)
}
return ResultNotISBN
}
// isbnDigitToInt returns the integer value of given ISBN digit.
func isbnDigitToInt(r rune) int {
if r == 'X' {
return 10
}
return int(r - '0')
}
// makeISBN makes a checker function for the URL checker.
func makeISBN(config string) CheckFunc {
if config != "" && config != "10" && config != "13" {
panic("invalid format")
}
return func(value, parent reflect.Value) Result {
return checkISBN(value, parent, config)
}
}
// checkISBN checks if the given value is a valid ISBN number.
func checkISBN(value, _ reflect.Value, mode string) Result {
if value.Kind() != reflect.String {
panic("string expected")
}
number := value.String()
switch mode {
case "10":
return IsISBN10(number)
case "13":
return IsISBN13(number)
default:
return IsISBN(number)
}
}