Giter VIP home page Giter VIP logo

calculate-luhn-mod-n's Introduction

calculate-luhn-mod-n

GitHub Node CI Node.js Package

Calculates Luhn Mod N check values

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

Installing

npm install calculate-luhn-mod-n

API

Parameters

Parameter Description
codePointFromCharacter Function to obtain a code point from a character
characterFromCodePoint Function to obtain a character from a code point
n Number of valid characters
input Input 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 calculateLuhnModN = require('calculate-luhn-mod-n');

const checkCharacter = calculateLuhnModN(character => map.indexOf(character), codePoint => map[codePoint], map.length, 'abcdef'))

// checkCharacter === 'e'

The following example uses JavaScript native functions (parseInt and toString) to do the mapping based on radix/mod 36.

const calculateLuhnModN = require('calculate-luhn-mod-n');

function appendLuhnMod36(input) {
  const radix = 36;
  
  return input + calculateLuhnModN(character => Number.parseInt(character, radix), codePoint => codePoint.toString(radix).toUpperCase(), radix, input);
}

const output = appendLuhnMod36('1134806PJFB000010013CD18');
// output === '1134806PJFB000010013CD18D'

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 calculateLuhnModN = require('calculate-luhn-mod-n');

function appendLuhnMod36(input) {
  const radix = 36;
  const pattern = /^([A-Z]|\d){4}\d{3}([A-Z]|\d){15}\d{2}$/;

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

  return input + calculateLuhnModN(character => Number.parseInt(character, radix), codePoint => codePoint.toString(radix).toUpperCase(), radix, input);
}

const output = appendLuhnMod36('1134806PJFB000010013CD18');
// output === '1134806PJFB000010013CD18D'

calculate-luhn-mod-n's People

Contributors

alfredmyers avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.