luhn-generator
Version:
A generator of numbers that passes the validation of Luhn algorithm or Luhn formula, also known as the 'modulus 10' or 'mod 10' algorithm
1 lines • 1.43 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("luhn",[],e):"object"==typeof exports?exports.luhn=e():t.luhn=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";t.exports={checksum:function(t){for(var e=t.toString(),n=0,r=2,o=e.length-1;o>=0;o--){var u=Math.max(r,1)*e[o];n+=u>9?u.toString().split("").map(Number).reduce(function(t,e){return t+e},0):u,r*=-1}return n%=10,n>0?10-n:0},generate:function(t,e){var n=t.toString(),r={pad:0,weightFactor:2};return void 0!==e&&void 0!==e.pad&&(r.pad=e.pad,r.pad>n.length&&(n=Array(r.pad-String(n).length).join("0")+n)),n+this.checksum(n)},random:function(t,e){return this.generate(function(t){for(var e="";e.length<t;){var n=Math.random().toString();e+=n.substr(2,n.length),e.length>t&&(e=e.substr(0,t))}return e}(t-1),e)},validate:function(t){return this.checksum(t.toString().slice(0,-1))===parseInt(t.toString().slice(-1))}}}])});