1.0.6 • Published 2 years ago

validate-luhn-mod-n v1.0.6

Weekly downloads
5
License
MIT
Repository
github
Last release
2 years ago

validate-luhn-mod-n

GitHub Node CI Node.js Package

Validates Luhn Mod N check values

Code based on https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm with minimal modifications.

Installing

npm install validate-luhn-mod-n

API

Parameters

ParameterDescription
codePointFromCharacterFunction to obtain a code point from a character
nNumber of valid characters
inputInput string

Using

The following example is based on the one found at https://en.wikipedia.org/wiki/Luhn_mod_N_algorithm

const map = 'abcdef';  // Character to code-point map
const validateLuhnModN = require('validate-luhn-mod-n');

const valid = validateLuhnModN(character => map.indexOf(character), map.length, 'abcdefe'))

// valid === true 

The following example uses JavaScript's native function parseInt to do the mapping based on radix/mod 36.

const validateLuhnModN = require('validate-luhn-mod-n');
const radix = 36
const valid = validateLuhnModN(character => Number.parseInt(character, radix), radix, '1134806PJFB000010013CD18D');

// valid === true 

The following example builds upon the previous one and demonstrates a possible way to do validation against an arbitrary regular expression before calculating the check character.

const validateLuhnModN = require('validate-luhn-mod-n');
const radix = 36;
const pattern = /^([A-Z]|\d){4}\d{3}([A-Z]|\d){15}\d{2}([A-Z]|\d)$/;

if (!pattern.test(input)) {
  throw new Error('Invalid identifier format!');
}

const valid = validateLuhnModN(character => Number.parseInt(character, radix), radix, '1134806PJFB000010013CD18D');

// valid === true
1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago