BarcodeGenerator (BarcodeGenerator v1.0.0) View Source

BarcodeGenerator generates a list of barcodes from a given start and end barcode.

This library allows you to generate GTIN barcodes by passing it the first and last barcode of a range.

Examples

Validating

BarcodeGenerator allows simple check-digit validation for GTIN barcodes, both in binary and numeric format:

BarcodeGenerator.valid?("6291041500206")
# true

BarcodeGenerator.valid?(6291041500206)
# true

BarcodeGenerator.valid?("6291041500200")
# false

BarcodeGenerator.valid?(6291041500200)
# false

Generating

BarcodeGenerator can generate barcodes in three different ways:

Plain list

BarcodeGenerator.generate/2 generates barcodes in a simple list format.

BarcodeGenerator.generate(6_291_041_500_200, 6_291_041_500_299)

Stream

BarcodeGenerator.generate_stream/2 returns a Stream that can be enumerated. Barcodes are generated as the stream is consumed, reducing memory footprint.

stream = BarcodeGenerator.generate_stream(6_291_041_500_200, 6_291_041_500_299)
barcodes = Enum.to_list(stream)

Flow (optional dependency)

BarcodeGenerator.generate_flow/3 returns a Flow, but requires that flow is present as dependency. Generating barcodes using Flow is heavily optimized to use all available resources to generate as quickly as possible.

BarcodeGenerator.generate_flow/3 accepts an optional third argument, opts, which is passed to Flow.from_enumerable/2, and defaults to max_demand: 1000.

# Assuming `{:flow, "~> 1.0"}` is in mix.exs
flow = BarcodeGenerator.generate_flow(6_291_041_500_200, 6_291_041_500_299)
barcodes = Enum.to_list(flow)

Link to this section Summary

Functions

Generates a list of barcodes

Generates a stream of barcodes

Validates a barcode

Link to this section Types

Specs

digit() :: 0..9

Specs

stack() :: [stack_item()]

Specs

stack_item() :: {digit(), index :: non_neg_integer(), sum :: non_neg_integer()}

Specs

stack_with_base() :: {stack(), non_neg_integer()}

Link to this section Functions

Link to this function

generate(starting_barcode, ending_barcode)

View Source

Specs

Generates a list of barcodes

Given the first and last barcode, will return a list of valid barcodes in that range.

Examples

iex> BarcodeGenerator.generate(6_291_041_500_200, 6_291_041_500_299)
[6291041500206, 6291041500213, 6291041500220, 6291041500237, 6291041500244,
6291041500251, 6291041500268, 6291041500275, 6291041500282, 6291041500299]
Link to this function

generate_flow(starting_barcode, ending_barcode, opts \\ [max_demand: 1000])

View Source

Specs

generate_flow(non_neg_integer(), non_neg_integer(), keyword()) :: Flow.t()

Generates barcodes in parallel in a Flow

Given the first and last barcode, will return a Flow that emits valid barcodes in that range.

When generating in a Flow, the ordering of the barcodes isn't guaranteed. The algorithm distributes batches of barcodes to be generated across different partitions. It reuses calculations done for the previous barcode (because only the last digit of the base changes) to efficiently generate large ranges of barcodes.

Optionally accepts custom options passed to Flow.from_enumerable/2, defaults to max_demand: 1000.

Examples

iex> flow = BarcodeGenerator.generate_flow(6_291_041_500_200, 6_291_041_500_299)
iex> Enum.to_list(flow) |> Enum.sort()
[6291041500206, 6291041500213, 6291041500220, 6291041500237, 6291041500244,
6291041500251, 6291041500268, 6291041500275, 6291041500282, 6291041500299]
Link to this function

generate_stream(starting_barcode, ending_barcode)

View Source

Specs

generate_stream(non_neg_integer(), non_neg_integer()) :: Enumerable.t()

Generates a stream of barcodes

Given the first and last barcode, will return a stream of valid barcodes in that range.

Examples

iex> stream = BarcodeGenerator.generate_stream(6_291_041_500_200, 6_291_041_500_299)
iex> Enum.to_list(stream)
[6291041500206, 6291041500213, 6291041500220, 6291041500237, 6291041500244,
6291041500251, 6291041500268, 6291041500275, 6291041500282, 6291041500299]

Specs

valid?(integer() | String.t()) :: boolean()

Validates a barcode

Checks if the barcode has a valid check digit.

Examples

iex> BarcodeGenerator.valid?("6291041500206")
true

iex> BarcodeGenerator.valid?(6291041500206)
true

iex> BarcodeGenerator.valid?("6291041500200")
false

iex> BarcodeGenerator.valid?(6291041500200)
false