aboutsummaryrefslogtreecommitdiff
path: root/barcode/barcode.go
blob: f4000022954b4e535de89cf6bf429875042f3b30 (plain) (blame)
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
package barcode

import (
	"strconv"
	"strings"

	"github.com/neonxp/checksum"
)

// Check barcode
func Check(number string) error {
	sum := 0
	numbers := strings.Split(number, "")
	l := len(numbers)
	for i := l; i > 0; i-- {
		ch := numbers[i-1]
		num, err := strconv.Atoi(string(ch))
		if err != nil {
			return checksum.ErrInvalidNumber
		}
		if (l-i)%2 != 0 {
			sum += num * 3
		} else {
			sum += num
		}
	}
	if sum%10 != 0 {
		return checksum.ErrInvalidChecksum
	}
	return nil
}

func Generate(number string) (string, error) {
	return "", checksum.ErrNotImplemented
}