Socket
Socket
Sign inDemoInstall

cdigit

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cdigit

Collection of check digit algorithms implemented in JavaScript


Version published
Weekly downloads
4K
increased by25.77%
Maintainers
1
Weekly downloads
 
Created
Source

cdigit: Check Digit Algorithms in JS

cdigit - Collection of check digit algorithms implemented in JavaScript

Synopsis

Node.js:

const cdigit = require('cdigit');

// Luhn (a.k.a. Mod 10) algorithm
console.log(cdigit.luhn.compute('1234'));   // '4'
console.log(cdigit.luhn.generate('1234'));  // '12344'
console.log(cdigit.luhn.validate('12344')); // true

Command line:

npx cdigit --algo damm compute 1234
npx cdigit --algo damm generate 1234
npx cdigit --algo damm validate 12340

Supported Algorithms

Generic Algorithms

Algorithmcdigit nameInput stringCheck character(s)
LuhnluhnNumeric (0-9)1 digit (0-9)
VerhoeffverhoeffNumeric (0-9)1 digit (0-9)
DammdammNumeric (0-9)1 digit (0-9)

ISO/IEC 7064 Family

ISO/IEC 7064 describes eight check digit (character) systems for numeric, alphabetic, or alphanumeric strings.

Algorithmcdigit nameInput stringCheck character(s)
ISO/IEC 7064, MOD 11-2mod11_2Numeric (0-9)1 digit or 'X' (0-9X)
ISO/IEC 7064, MOD 37-2mod37_2Alphanumeric (0-9A-Z)1 digit, letter, or '*' (0-9A-Z*)
ISO/IEC 7064, MOD 97-10mod97_10Numeric (0-9)2 digits (0-9)
ISO/IEC 7064, MOD 661-26mod661_26Alphabetic (A-Z)2 letters (A-Z)
ISO/IEC 7064, MOD 1271-36mod1271_36Alphanumeric (0-9A-Z)2 digits or letters (0-9A-Z)
ISO/IEC 7064, MOD 11-10mod11_10Numeric (0-9)1 digit (0-9)
ISO/IEC 7064, MOD 27-26mod27_26Alphabetic (A-Z)1 letter (A-Z)
ISO/IEC 7064, MOD 37-36mod37_36Alphanumeric (0-9A-Z)1 digit or letter (0-9A-Z)

GTIN (Global Trade Item Number) Family

Algorithmcdigit nameInput stringCheck character(s)Also referred to as
GTIN-8gtinNumeric (0-9)1 digit (0-9)EAN-8
GTIN-12gtinNumeric (0-9)1 digit (0-9)UPC, UPC-A
GTIN-13gtinNumeric (0-9)1 digit (0-9)EAN, JAN, ISBN-13, etc.
GTIN-14gtinNumeric (0-9)1 digit (0-9)EAN, UCC-14

cdigit currently provides only one generic gtin object for GTINs and other GS1 data structures as they share the same algorithm. Note that cdigit.gtin does not validate the length of a given GTIN string.

Usage - Node.js

Load cdigit and access to an algorithm object by cdigit.name listed in Supported Algorithms section.

const cdigit = require('cdigit');
const algo = cdigit.mod97_10;

Algorithm objects implement the following methods.

validate(num: string): boolean

Check if a given string is valid in accordance with the algorithm. The argument must include check digit(s) as well as the source number.

console.log(cdigit.mod97_10.validate('123482'));  // true

generate(num: string): string

Generate a valid number string from a given source number. The generated string includes the check digit(s) computed and placed in accordance with the algorithm.

console.log(cdigit.mod97_10.generate('1234'));  // '123482'

compute(num: string): string

Generate check digit(s) from a given source number. This returns the check digit(s) only.

console.log(cdigit.mod97_10.compute('1234')); // '82'

See example.js for usage examples.

Usage - Command Line

Usage: cdigit [options] [command]

Options:
  -a, --algo <name>  specify check digit algorithm by name
  -h, --help         output usage information

Commands:
  validate <string>  check if string is valid
  generate <string>  generate valid number from string
  compute <string>   compute check digit from string

-a, --algo <name> option accepts the names listed in Supported Algorithms section and defaults to luhn or the value of CDIGIT_CLI_DEFAULT_ALGO environment variable (if set).

License

Copyright (c) 2018 LiosK

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

See Also

Keywords

FAQs

Package last updated on 25 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc