-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisfreightcontainerid.go
91 lines (80 loc) · 1.39 KB
/
isfreightcontainerid.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
package validatorgo
import (
"math"
"regexp"
"strconv"
"unicode/utf8"
)
var alphaFreightNumVal = map[string]int{
"A": 10,
"B": 12,
"C": 13,
"D": 14,
"E": 15,
"F": 16,
"G": 17,
"H": 18,
"I": 19,
"J": 20,
"K": 21,
"L": 23,
"M": 24,
"N": 25,
"O": 26,
"P": 27,
"Q": 28,
"R": 29,
"S": 30,
"T": 31,
"U": 32,
"V": 34,
"W": 35,
"X": 36,
"Y": 37,
"Z": 38,
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
}
// A validator that checks alias for IsISO6346, check if the string is a valid [ISO 6346] shipping container identification.
//
// ok := validatorgo.IsFreightContainerID("ABCU1234567")
// fmt.Println(ok) // true
// ok := validatorgo.IsFreightContainerID("AB123456789")
// fmt.Println(ok) // false
//
// [ISO 6346]: https://en.wikipedia.org/wiki/ISO_6346
func IsFreightContainerID(str string) bool {
// Check if length is 11
if utf8.RuneCountInString(str) != 11 {
return false
}
re := regexp.MustCompile(`^([A-Z]{3})([UJZ])([0-9]{6})([0-9])$`)
match := re.MatchString(str)
if !match {
return false
}
sum := 0
for i := 0; i < 10; i++ {
char := string(str[i])
mag, ok := alphaFreightNumVal[char]
if !ok {
return false
}
mul := math.Pow(2.00, float64(i))
sum += int(mul) * mag
}
actCheck := sum % 11
givCheck, err := strconv.Atoi(string(str[10]))
if err != nil {
return false
}
return actCheck == givCheck
}