pn-check-digits

Provides ways of calculating different standard check-digits.

Usage no npm install needed!

<script type="module">
  import pnCheckDigits from 'https://cdn.skypack.dev/pn-check-digits';
</script>

README

Provides ways of calculating different standard check-digits.

How to use:

npm install pn-check-digits

Universal Postal Union (UPU) S10 Check Digit Algorithm

The UPU S10 standard defines a system for assigning 13-character identifiers to postal items for the purpose of tracking and tracing them during shipping. The identifiers consist of a two letter service indicator code, an eight digit serial number (in the range 00000000 to 99999999), a single check-digit, and a two-letter ISO country code — the latter identifying the issuing postal administration's country. References: https://en.wikipedia.org/wiki/S10_(UPU_standard)

import { S10 } from 'pn-check-digits'

const serialNumber = '12345678';

// get checkdigit
const checkDigit = S10.compute(serialNumber); // '5'

// get serialNumber + checkdigit
const serialNumberWithCheckDigit = S10.generate(serialNumber); // '123456785'

// validate if valid serialNumber with checkdigit, returns true or false
const isValid = S10.validate(serialNumberWithCheckDigit); // true

UPC - A

The UPC Version A validates a serial number using a weight of 3 and 1 and using the Luhn Algorithm Modulo 10.

References: Check Digit Calculation Method

import { UPCCGCP } from 'pn-check-digits';

const serialNumberWithCheckDigit = '23232341';

// get check digit
const checkDigit = UPCCGCP.compute(serialNumberWithCheckDigit.substring(0, serialNumberWithCheckDigit.length - 1));

// validate if the digit is compliant
const isValid = UPCCGCP.validate(serialNumberWithCheckDigit); // true

MSI (Modified Plessey Code)

The MSI standards checks the digit using the Luhn Algorithm with a weight of 2 and 1. References: IBM Check Digit

import { MSI } from 'pn-check-digits`;

const serialNumberWithCheckDigit = '01234567';

// get check digit
const checkDigit = MSI.compute(serialNumberWithCheckDigit.substring(0, serialNumberWithCheckDigit.length - 1));

// validate if the digit is compliant
const isValid = MSI.validate(serialNumberWithCheckDigit); // true

IDENTCODE & LEITCODE

IDENTCODE

Identcode barcode symbology is based on the Code 25 Interleaved symbology. It is mainly used in post delivery system to tracking packages. Identcode barcode symbology is also called German Postal 2 of 5 Identcode, Deutsche Post AG Identcode, Deutsche Frachtpost Identcode, Identcode, CodeIdentcode, Deutsche Post AG (DHL). The Identcode Barcode is a numeric only, discrete linear barcode. The tracking number encoded in this barcode symbology contains the information of the customer identification, address and the mail piece. The Identcode Barcode could encode 11 digits plus 1 check digit which makes total length to 12.

Reference: Identcode Barcode Symbology

LEITCODE

Leitcode is also known as Deutsche Post Leitcode barcode, CodeLeitcode, Deutsche Post AG (DHL). Leitcode barcode symbology is based on the Interleaved 2 of 5 barcode. The Leitcode Barcode is a numeric only, discrete linear barcode. Encoding length of Leitcode Barcode symbology is 13 digits long with one checksum digit. The checksum digit is calculated automatically by employing the Modulo 10 method. It is used by the German Post (Deutsche Post AG) (Deutsche Frachtpost) in freight post system to track and mail packages. The value encoded in the Leitcode barcode symbology indicates the destination of the packages.

Reference: Leitcode Barcode Symbology

Though the internal team decided to use the weight of 4 and 9 - based from the Java Legacy code, we will use the term IdentCode with a weight of LeitCode.

import { IdentCode } from 'pn-check-digits';

const serialNumberWithCheckDigit = '56400000005';

// get check digit
const checkDigit = IdentCode.compute(serialNumberWithCheckDigit.substring(0, serialNumberWithCheckDigit.length - 1)); // 5
// validate if the digit is compliant
const isValid = IdentCode.validate(serialNumberWithCheckDigit); // true

ISO/IEC 7064

This is based from ISO/IEC 7064:2002 specifications the string must be in a format of: numeric (0 to 9), alphabetic (26 Letters: A-Z) or alphanumeric (combination of alphabets and numbers).

Reference: International Organization for Standardization - ISO/IEC 7064:2003

import { ISOIEC7064 } from 'pn-check-digits';

const serialNumber = '094459127867977';

// get the check digit
const checkDigit = ISOIEC7064.compute(serialNumber) // Z

// validate if the digit is compliant
const isValid = ISOIEC7064.validate(serialNumber) // true