-
Notifications
You must be signed in to change notification settings - Fork 0
/
isbn.go
127 lines (96 loc) · 2.37 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
122
123
124
125
126
127
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package checker
import (
"errors"
"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/
// tagISBN is the tag of the checker.
const tagISBN = "isbn"
// ErrNotISBN indicates that the given value is not a valid ISBN.
var ErrNotISBN = errors.New("please enter a valid ISBN number")
// IsISBN10 checks if the given value is a valid ISBN-10 number.
func IsISBN10(value string) error {
value = strings.ReplaceAll(value, "-", "")
if len(value) != 10 {
return ErrNotISBN
}
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 ErrNotISBN
}
return nil
}
// IsISBN13 checks if the given value is a valid ISBN-13 number.
func IsISBN13(value string) error {
value = strings.ReplaceAll(value, "-", "")
if len(value) != 13 {
return ErrNotISBN
}
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 ErrNotISBN
}
return nil
}
// IsISBN checks if the given value is a valid ISBN number.
func IsISBN(value string) error {
value = strings.ReplaceAll(value, "-", "")
if len(value) == 10 {
return IsISBN10(value)
} else if len(value) == 13 {
return IsISBN13(value)
}
return ErrNotISBN
}
// 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) error {
return checkISBN(value, parent, config)
}
}
// checkISBN checks if the given value is a valid ISBN number.
func checkISBN(value, _ reflect.Value, mode string) error {
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)
}
}