-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluhn.go
61 lines (50 loc) · 1.13 KB
/
luhn.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
// 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 v2
import (
"reflect"
"unicode"
)
const (
// nameLUHN is the name of the LUHN check.
nameLUHN = "luhn"
)
var (
// ErrNotLUHN indicates that the given value is not a valid LUHN number.
ErrNotLUHN = NewCheckError("NOT_LUHN")
)
// IsLUHN checks if the value is a valid LUHN number.
func IsLUHN(value string) (string, error) {
var sum int
var alt bool
for i := len(value) - 1; i >= 0; i-- {
r := rune(value[i])
if !unicode.IsDigit(r) {
return value, ErrNotLUHN
}
n := int(r - '0')
if alt {
n *= 2
if n > 9 {
n -= 9
}
}
sum += n
alt = !alt
}
if sum%10 != 0 {
return value, ErrNotLUHN
}
return value, nil
}
// checkLUHN checks if the value is a valid LUHN number.
func checkLUHN(value reflect.Value) (reflect.Value, error) {
_, err := IsLUHN(value.Interface().(string))
return value, err
}
// makeLUHN makes a checker function for the LUHN checker.
func makeLUHN(_ string) CheckFunc[reflect.Value] {
return checkLUHN
}