checkdigit

package module
Version: v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 1 Imported by: 2

README

checkdigit

Build Status codecov Go Report Card GoDoc GitHub license

About

Provide check digit algorithms and calculators written by Go.

Provided methods

Algorithms
Calculators

Usage

See examples.

License

Released under the MIT License.

Documentation

Overview

Package checkdigit provides check digit algorithms and calculators written by Go.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidArgument = errors.New("checkdigit: invalid argument")

ErrInvalidArgument is happening when given the wrong argument.

Functions

This section is empty.

Types

type Generator

type Generator interface {
	Generate(seed string) (int, error)
}

A Generator generates a check digit by implemented algorithm or calculator.

type Provider

type Provider interface {
	Verifier
	Generator
}

A Provider has Verifier and Generator interfaces.

func NewDamm

func NewDamm() Provider

NewDamm returns a new Provider that implemented the Damm algorithm.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewDamm()

	const seed = "572"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 572, check digit: 4, verify: true

func NewEAN13

func NewEAN13() Provider

NewEAN13 returns a new Provider that implemented GTIN-13 with position correction calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewEAN13()

	const seed = "590123412345"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 590123412345, check digit: 7, verify: true

func NewEAN8

func NewEAN8() Provider

NewEAN8 returns a new Provider that implemented GTIN-8 with position correction calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewEAN8()

	const seed = "9638507"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 9638507, check digit: 4, verify: true

func NewISBN10

func NewISBN10() Provider

NewISBN10 returns a new Provider that implemented modulus 11 weight 10 to 2 calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewISBN10()

	const seed = "155860832"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	digit := "X"
	if cd != 10 {
		digit = strconv.Itoa(cd)
	}

	ok := p.Verify(seed + digit)
	fmt.Printf("seed: %s, check digit: %s, verify: %t\n", seed, digit, ok)

}
Output:

seed: 155860832, check digit: X, verify: true

func NewISBN13

func NewISBN13() Provider

NewISBN13 returns a new Provider that implemented modulus 10 weight 3 calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewISBN13()

	const seed = "978000271217"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 978000271217, check digit: 0, verify: true

func NewITF

func NewITF() Provider

NewITF returns a new Provider that implemented GTIN-14 calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewITF()

	const seed = "1456995111617"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 1456995111617, check digit: 6, verify: true

func NewJAN13

func NewJAN13() Provider

NewJAN13 returns a new Provider that implemented GTIN-13 with position correction calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewJAN13()

	const seed = "456995111617"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 456995111617, check digit: 9, verify: true

func NewJAN8

func NewJAN8() Provider

NewJAN8 returns a new Provider that implemented GTIN-8 with position correction calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewJAN8()

	const seed = "4996871"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 4996871, check digit: 2, verify: true

func NewLuhn

func NewLuhn() Provider

NewLuhn returns a new Provider that implemented the Luhn algorithm.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewLuhn()

	const seed = "411111111111111"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 411111111111111, check digit: 1, verify: true

func NewSSCC

func NewSSCC() Provider

NewSSCC returns a new Provider that implemented GTIN-18 calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewSSCC()

	const seed = "04569951110000001"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 04569951110000001, check digit: 6, verify: true

func NewUPC

func NewUPC() Provider

NewUPC returns a new Provider that implemented GTIN-12 with position correction calculator.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewUPC()

	const seed = "01234567890"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 01234567890, check digit: 5, verify: true

func NewVerhoeff

func NewVerhoeff() Provider

NewVerhoeff returns a new Provider that implemented the Verhoeff algorithm.

Example
package main

import (
	"fmt"
	"log"
	"strconv"

	"github.com/osamingo/checkdigit"
)

func main() {

	p := checkdigit.NewVerhoeff()

	const seed = "236"
	cd, err := p.Generate(seed)
	if err != nil {
		log.Fatalln("failed to generate check digit")
	}

	ok := p.Verify(seed + strconv.Itoa(cd))
	fmt.Printf("seed: %s, check digit: %d, verify: %t\n", seed, cd, ok)

}
Output:

seed: 236, check digit: 3, verify: true

type Verifier

type Verifier interface {
	Verify(code string) bool
}

A Verifier is verifying to code by implemented algorithm or calculator.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL