# gtin

> GTIN (UPC, EAN, ITF, etc.) utilities.

Latest version **1.0.2** (published 2021-04-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install gtin
pnpm add gtin
yarn add gtin
bun add gtin
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-04-01 |
| First published | 2016-07-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/gtin) |
| Module format | CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | Ivan K |
| Maintainers | str_t |
| Keywords | gtin, upc, ean, ucc, itf, itf-14, itf14, ean-13, ean13, ucc-12, ucc12, upc-e, upce, upca, upc-a, validation, utilities, valid |

## Links

- npm: https://www.npmjs.com/package/gtin
- Repository: https://github.com/xbpf/gtin
- Homepage: https://github.com/xbpf/gtin#readme
- Issues: https://github.com/xbpf/gtin/issues
- npm.io page: https://npm.io/package/gtin

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-04-01
- 1.0.1 — 2020-07-29
- 1.0.0 — 2020-07-29
- 0.3.0 — 2016-07-12
- 0.2.0 — 2016-07-12
- 0.1.1 — 2016-07-12
- 0.1.0 — 2016-07-12

## README

gtin
===

[![npm version](https://img.shields.io/npm/v/gtin.svg?style=flat-square)](https://npmjs.com/package/gtin)
[![javascript standard style](https://img.shields.io/badge/code%20style-standard-blue.svg?style=flat-square)](http://standardjs.com/)
[![travis build](https://img.shields.io/travis/xbpf/gtin/master.svg?style=flat-square)](https://travis-ci.org/xbpf/gtin)
[![coveralls coverage](https://img.shields.io/coveralls/xbpf/gtin.svg?style=flat-square)](https://coveralls.io/github/xbpf/gtin)
[![david dependencies](https://david-dm.org/xbpf/gtin.svg?style=flat-square)](https://david-dm.org/xbpf/gtin)
[![david dev dependencies](https://david-dm.org/xbpf/gtin/dev-status.svg?style=flat-square)](https://david-dm.org/xbpf/gtin)


GTIN (UPC, EAN, ITF, etc.) utilities.

`npm install gtin`

---

* [gtin.isGTIN(barcode)](#user-content-gtin-isGTIN)
* [gtin.isValid(barcode)](#user-content-gtin-isValid)
* [gtin.minify(barcode)](#user-content-gtin-minify)
* [gtin.getFormat(barcode)](#user-content-gtin-getFormat)
* [gtin.getRealFormat(barcode)](#user-content-gtin-getRealFormat)
* [gtin.upcE.compress(barcode)](#user-content-gtin-upcE-compress)
* [gtin.upcE.expand(barcode)](#user-content-gtin-upcE-expand)

---

<a id='gtin-isGTIN'></a>
`gtin.isGTIN(barcode)`
---

Returns true or false, depending on if the string given is a GTIN barcode.
Throws an error if an empty string or anything other than a string is provided.

**NOTE**: This does not validate the code by check digit. Validation is done
with `gtin.isValid`.

```js
import { isGTIN } from 'gtin'

isGTIN('1234')      // false
isGTIN('12341238')  // true
isGTIN('')          // Error thrown
isGTIN(123)         // Error thrown
```

<a id='gtin-isValid'></a>
`gtin.isValid(barcode)`
---

Validates a GTIN (14, 13, 12, or 8-digit) barcode by check digit. Barcode must
be a string.

To validate a UPC-E barcode, expand it first: `isValid(upcE.expand('01278906'))`

```js
import { isValid } from 'gtin'

isValid('12341238')       // true
isValid('1234123412344')  // true
isValid('12341234123413') // true
isValid('012000007897')   // true
isValid('012000007896')   // false
isValid('abc')            // Error thrown
isValid(123)              // Error thrown
isValid('123')            // Error thrown
```

<a id='gtin-minify'></a>
`gtin.minify(barcode)`
---

Minifies GTIN to smallest possible representation, by stripping as many leading
zeroes as possible. Does not compress to UPC-E.

```js
import { minify } from 'gtin'

minify('00000012341238')  // '12341238'
minify('0000012341238')   // '12341238'
minify('01234123412344')  // '1234123412344
minify('001234123412344') // Error thrown
minify('abc')             // Error thrown
minify(123)               // Error thrown
minify('123')             // Error thrown
```

<a id='gtin-getFormat'></a>
`gtin.getFormat(barcode)`
---

Gets the format of the given barcode. Does not validate checksum.

```js
import { getFormat } from 'gtin'

getFormat('12341238')       // 'GTIN-8'
getFormat('123412341234')   // 'GTIN-12'
getFormat('1234123412344')  // 'GTIN-13'
getFormat('01234123412344') // 'GTIN-14'
getFormat('123412381')      // Error thrown
getFormat('abc')            // Error thrown
getFormat(123)              // Error thrown
getFormat('123')            // Error thrown
```

<a id='gtin-getRealFormat'></a>
`gtin.getRealFormat(barcode)`
---

Gets the real format of the given barcode, by minifying it first.

```js
import { getRealFormat } from 'gtin'

getRealFormat('1234123412344')  // 'GTIN-13'
getRealFormat('01234123412344') // 'GTIN-13'
getRealFormat('123412381')      // Error thrown
getRealFormat('abc')            // Error thrown
getRealFormat(123)              // Error thrown
getRealFormat('123')            // Error thrown
```

<a id='gtin-upcE-compress'></a>
`gtin.upcE.compress(barcode)`
---

Compress a UPC-A barcode to an 8-digit UPC-E barcode. Does not validate
code by check digit. Barcode must be a string.

* 12-digit UPC-A: Number system and check digits are taken into account.
* 11-digit UPC-A: Number system 0 is assumed. Check digit is taken into account.
* 10-digit UPC-A: Number system 0 is assumed. Check digit is generated.

```js
import { upcE } from 'gtin'

upcE.compress('1200000789')   // '01278907'
upcE.compress('12000007897')  // '01278907'
upcE.compress('012000007897') // '01278907'
upcE.compress('012000007896') // '01278906'
upcE.compress('012345678905') // null
upcE.compress(123)            // Error thrown
upcE.compress('123')          // Error thrown
upcE.compress('abc')          // Error thrown
```

<a id='gtin-upcE-expand'></a>
`gtin.upcE.expand(barcode)`
---

Expands a UPC-E barcode to a 12-digit UPC-A barcode. Does not validate
code by check digit. Barcode must be a string.

* 8-digit UPC-E: Number system and check digits are taken into account.
* 7-digit UPC-E: Number system 0 is assumed. Check digit is taken into account.
* 6-digit UPC-E: Number system 0 is assumed. Check digit is generated.

```js
import { upcE } from 'gtin'

upcE.expand('127890')    // '012000007897'
upcE.expand('1278907')   // '012000007897'
upcE.expand('01278907')  // '012000007897'
upcE.expand('01278906')  // '012000007896'
upcE.expand('123412341') // Error thrown
upcE.expand(123)         // Error thrown
upcE.expand('123')       // Error thrown
upcE.expand('abc')       // Error thrown
```

---
_Source: https://npm.io/package/gtin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
