parcel

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 7 Imported by: 0

README

go-package-tracking

Go Reference

A go package for matching tracking numbers to couriers, or for discovering tracking numbers in text.

Install

$ go get dev.freespoke.com/go-package-tracking@latest

Usage

// Track a number.
tracking, err := parcel.Track("986578788855")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Service: %s\nURL: %s\n\n", tracking.Service, tracking.TrackingURL)

// Find tracking numbers in a string.
// Tracking numbers are grouped by service name.
tracks, err := parcel.Find("track 986578788855 and JVGL0999999990")
if err != nil {
    log.Fatal(err)
}

for k, v := range tracks {
    for _, track := range v {
        fmt.Printf("Service: %s\nURL: %s\n", k, track.TrackingURL)
    }
}

Resources

[jkeen tracking number data](https://github.com/jkeen/

Notes

The original package specifies its regex as PCRE which is not fully supported by Go. To prevent breaking changes from impacting this package, the dependency to jkeen/tracking_number_data is currently tied to the specific commit which includes go.mod. Once a compatible release is tagged, the dependency can be updated.

A small helper function "fixes" the regex provided.

Tests are generated to run against the test cases embedded in the courier json file. Separate tests also validate the check digit functions.

The signatures for the two exposed functions are

func Track(string) (parcel.Tracking, error)

func Find(string) (map[string][]parcelTracking, error)

License

MIT (See LICENSE.md).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadString  = errors.New("extended characters present")
	ErrNoServices = errors.New("no tracking services loaded")
)

Functions

func Find

func Find(in string) (map[string][]Tracking, error)

Find extracts detected tracking numbers from a string based on word boundaries. It returns a list of tracking results with the corresponding tracking number.

Types

type Tracking

type Tracking struct {
	// Always returned
	Courier        string
	Service        string
	TrackingNumber string
	SerialNumber   string

	// Always populated if used
	CheckDigit  string
	TrackingURL string

	// Extra details that may be encoded into the tracking number.
	Details map[string]string
}

Tracking contains results extracted from a valid tracking number.

func Track

func Track(in string) ([]Tracking, error)

Track identifies valid package tracking codes. If a valid code is identified, it returns encoded tracking information. There may be multiple matches.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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