npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

gtin-checksum

v1.0.0

Published

GS1 modulo-10 check-digit calculator and validator for GTIN-8, GTIN-12 (UPC-A), GTIN-13 (EAN-13), and GTIN-14 (ITF-14). Zero dependencies, TypeScript-native, tested against official GS1 test vectors.

Readme

gtin-checksum

npm version license types bundle size

GS1 modulo-10 check-digit calculator and validator for GTIN-8, GTIN-12 (UPC-A), GTIN-13 (EAN-13), and GTIN-14 (ITF-14). Zero runtime dependencies, TypeScript-native, ESM-only, tested against public GS1 / Wikipedia test vectors.

A correctly computed GTIN check digit is the difference between a product feed that lands in Google Merchant Center and one that gets rejected for "Invalid GTIN". This module exists because the algorithm is one short page in the GS1 General Specifications — but every e-commerce stack ends up re-implementing it, usually with a subtle bug at the weight-alternation step.

Install

npm install gtin-checksum

Node 18+ · ESM-only · ~1 KB minified.

Usage

import {
  computeCheckDigit,
  isValidGtin,
  appendCheckDigit,
  gtinVariant,
} from 'gtin-checksum';

computeCheckDigit('400638133393');   // 1
appendCheckDigit('400638133393');    // '4006381333931'

isValidGtin('4006381333931');        // true   (GTIN-13)
isValidGtin('036000291452');         // true   (GTIN-12 / UPC-A)
isValidGtin('73513537');             // true   (GTIN-8)
isValidGtin('00012345678905');       // true   (GTIN-14 / ITF-14)
isValidGtin('4006381333932');        // false  (wrong check digit)
isValidGtin('not-a-number');         // false  (no throw — returns false)

gtinVariant('4006381333931');        // 13
gtinVariant('not-a-number');         // null

API

computeCheckDigit(dataDigits: string): number

Returns the GS1 modulo-10 check digit (0-9) for the given data digits — i.e. the GTIN without its trailing check digit. Throws RangeError on non-digit input or a length that is not one of 7, 11, 12, 13 (corresponding to GTIN-8/12/13/14).

isValidGtin(gtin: string): boolean

Returns true iff the input is a string of all digits whose length is 8, 12, 13, or 14, and whose trailing digit matches the computed check digit. Never throws — returns false for any malformed input.

appendCheckDigit(dataDigits: string): string

Convenience wrapper. Returns the full GTIN by appending the computed check digit to the input.

gtinVariant(gtin: string): 8 | 12 | 13 | 14 | null

Returns the GTIN length if it matches one of the four canonical variants; null otherwise. Does not verify the check digit — use isValidGtin for that.

GTIN_LENGTHS

Readonly tuple [8, 12, 13, 14] — the four canonical GTIN lengths.

Algorithm

Per GS1 General Specifications §7.9:

  1. Starting at the rightmost data digit (i.e. the digit immediately to the left of the check digit position), multiply by 3.
  2. Move left, alternating weights 1 and 3.
  3. Sum the products.
  4. The check digit is (10 - sum mod 10) mod 10 — i.e. the smallest non-negative value that makes the total a multiple of 10.

Worked example (GTIN-13: 4006381333931)

| Position (from left) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | Digit | 4 | 0 | 0 | 6 | 3 | 8 | 1 | 3 | 3 | 3 | 9 | 3 | 1 (check) | | Weight | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | — | | Product | 4 | 0 | 0 | 18 | 3 | 24 | 1 | 9 | 3 | 9 | 9 | 9 | — |

Sum = 89 · (10 − 89 mod 10) mod 10 = (10 − 9) mod 10 = 1 · check digit matches.

Why a separate package

Most e-commerce projects pull a 50 KB barcode library that drags in symbology renderers, image generators, and canvas peer-dependencies — just to validate a check digit. This module is the opposite: 4 exported functions, zero dependencies, types-first, and an explicit test suite against named public vectors so you can audit correctness without trusting opaque vendor code.

Built and maintained by Marius Pahomi at FeedArc — where verifying GTINs at scale is a daily concern.

Development

npm install
npm test         # vitest, all test vectors
npm run build    # emits dist/ with d.ts + sourcemaps

Contributing

Bug reports and additional test vectors (especially edge cases — pseudo-GTINs, embedded weight/price codes, restricted ranges) are welcome via GitHub Issues.

License

MIT © Marius Pahomi