aboutsummaryrefslogtreecommitdiff
path: root/isin
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-08-30 02:53:06 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-08-30 02:53:06 +0300
commitdd42100aa2f062a4e53399a3cabce732eaf0f6e5 (patch)
tree5913585fe0878b3cb4b4fe852f3b3345cfca4f94 /isin
parent878cb24695b7d39fa9de65a1e164abc84dc67ead (diff)
Barcode and ISINHEADmaster
Diffstat (limited to 'isin')
-rw-r--r--isin/isin.go49
-rw-r--r--isin/isin_test.go22
2 files changed, 71 insertions, 0 deletions
diff --git a/isin/isin.go b/isin/isin.go
new file mode 100644
index 0000000..0117506
--- /dev/null
+++ b/isin/isin.go
@@ -0,0 +1,49 @@
+package isin
+
+import (
+ "strconv"
+ "strings"
+
+ "github.com/neonxp/checksum"
+)
+
+// Check ISIN code
+func Check(number string) error {
+ sum := 0
+ numbersString := ""
+ for _, ch := range strings.ToLower(number) {
+ if int(ch) >= 'a' && int(ch) <= 'z' {
+ numbersString = numbersString + strconv.Itoa(int(ch-'a'+10))
+ continue
+ }
+ numbersString = numbersString + string(ch)
+ }
+ numbers := strings.Split(numbersString, "")
+ 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 {
+ n := num * 2
+ if n > 9 {
+ sum += n - 9
+ } else {
+ sum += n
+ }
+ } else {
+ sum += num
+ }
+ }
+ if sum%10 != 0 {
+ return checksum.ErrInvalidChecksum
+ }
+ return nil
+}
+
+func Generate(number string) (string, error) {
+ // TODO implement
+ return "", checksum.ErrNotImplemented
+}
diff --git a/isin/isin_test.go b/isin/isin_test.go
new file mode 100644
index 0000000..b7d084d
--- /dev/null
+++ b/isin/isin_test.go
@@ -0,0 +1,22 @@
+package isin
+
+import (
+ "testing"
+
+ "github.com/neonxp/checksum"
+)
+
+func TestISIN(t *testing.T) {
+ samples := map[string]error{
+ "4600051000058": checksum.ErrInvalidChecksum,
+ "?600051000057": checksum.ErrInvalidNumber,
+ "A600051000057": checksum.ErrInvalidChecksum,
+ "4000000000006": nil,
+ "US0378331005": nil,
+ }
+ for num, result := range samples {
+ if err := Check(num); err != result {
+ t.Errorf("Expected %+v actual %+v for %s", result, err, num)
+ }
+ }
+}