Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

credit-card-type

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

credit-card-type - npm Package Compare versions

Comparing version 8.0.0 to 8.1.0

lib/add-matching-cards-to-results.js

6

CHANGELOG.md

@@ -0,1 +1,7 @@

8.1.0
=====
- Add support for Hiper cards
- Add support for Hipercard cards
8.0.0

@@ -2,0 +8,0 @@ =====

754

dist/js/app.built.js
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.creditCardType=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
var types = require('./lib/card-types');
var clone = require('./lib/clone');
var findBestMatch = require('./lib/find-best-match');
var isValidInputType = require('./lib/is-valid-input-type');
var addMatchingCardsToResults = require('./lib/add-matching-cards-to-results');
var testOrder;
var types = {};
var customCards = {};
var VISA = 'visa';
var MASTERCARD = 'mastercard';
var AMERICAN_EXPRESS = 'american-express';
var DINERS_CLUB = 'diners-club';
var DISCOVER = 'discover';
var ELO = 'elo';
var JCB = 'jcb';
var UNIONPAY = 'unionpay';
var MAESTRO = 'maestro';
var MIR = 'mir';
var CVV = 'CVV';
var CID = 'CID';
var CVC = 'CVC';
var CVN = 'CVN';
var CVP2 = 'CVP2';
var CVE = 'CVE';
var cardNames = {
VISA: 'visa',
MASTERCARD: 'mastercard',
AMERICAN_EXPRESS: 'american-express',
DINERS_CLUB: 'diners-club',
DISCOVER: 'discover',
JCB: 'jcb',
UNIONPAY: 'unionpay',
MAESTRO: 'maestro',
ELO: 'elo',
MIR: 'mir',
HIPER: 'hiper',
HIPERCARD: 'hipercard'
};
var ORIGINAL_TEST_ORDER = [
VISA,
MASTERCARD,
AMERICAN_EXPRESS,
DINERS_CLUB,
DISCOVER,
JCB,
UNIONPAY,
MAESTRO,
ELO,
MIR
cardNames.VISA,
cardNames.MASTERCARD,
cardNames.AMERICAN_EXPRESS,
cardNames.DINERS_CLUB,
cardNames.DISCOVER,
cardNames.JCB,
cardNames.UNIONPAY,
cardNames.MAESTRO,
cardNames.ELO,
cardNames.MIR,
cardNames.HIPER,
cardNames.HIPERCARD
];
function clone(originalObject) {
var dupe;
if (!originalObject) { return null; }
dupe = JSON.parse(JSON.stringify(originalObject));
return dupe;
}
testOrder = clone(ORIGINAL_TEST_ORDER);
types[VISA] = {
niceType: 'Visa',
type: VISA,
patterns: [
4
],
gaps: [4, 8, 12],
lengths: [16, 18, 19],
code: {
name: CVV,
size: 3
}
};
types[MASTERCARD] = {
niceType: 'Mastercard',
type: MASTERCARD,
patterns: [
[51, 55],
[2221, 2229],
[223, 229],
[23, 26],
[270, 271],
2720
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVC,
size: 3
}
};
types[AMERICAN_EXPRESS] = {
niceType: 'American Express',
type: AMERICAN_EXPRESS,
patterns: [
34,
37
],
gaps: [4, 10],
lengths: [15],
code: {
name: CID,
size: 4
}
};
types[DINERS_CLUB] = {
niceType: 'Diners Club',
type: DINERS_CLUB,
patterns: [
[300, 305],
36,
38,
39
],
gaps: [4, 10],
lengths: [14, 16, 19],
code: {
name: CVV,
size: 3
}
};
types[DISCOVER] = {
niceType: 'Discover',
type: DISCOVER,
patterns: [
6011,
[644, 649],
65
],
gaps: [4, 8, 12],
lengths: [16, 19],
code: {
name: CID,
size: 3
}
};
types[JCB] = {
niceType: 'JCB',
type: JCB,
patterns: [
2131,
1800,
[3528, 3589]
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVV,
size: 3
}
};
types[UNIONPAY] = {
niceType: 'UnionPay',
type: UNIONPAY,
patterns: [
620,
[624, 626],
[62100, 62182],
[62184, 62187],
[62185, 62197],
[62200, 62205],
[622010, 622999],
622018,
[622019, 622999],
[62207, 62209],
[622126, 622925],
[623, 626],
6270,
6272,
6276,
[627700, 627779],
[627781, 627799],
[6282, 6289],
6291,
6292
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVN,
size: 3
}
};
types[MAESTRO] = {
niceType: 'Maestro',
type: MAESTRO,
patterns: [
493698,
[500000, 506698],
[506779, 508999],
[56, 59],
63,
67,
6
],
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: CVC,
size: 3
}
};
types[ELO] = {
niceType: 'Elo',
type: ELO,
patterns: [
401178,
401179,
438935,
457631,
457632,
431274,
451416,
457393,
504175,
[506699, 506778],
[509000, 509999],
627780,
636297,
636368,
[650031, 650033],
[650035, 650051],
[650405, 650439],
[650485, 650538],
[650541, 650598],
[650700, 650718],
[650720, 650727],
[650901, 650978],
[651652, 651679],
[655000, 655019],
[655021, 655058]
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVE,
size: 3
}
};
types[MIR] = {
niceType: 'Mir',
type: MIR,
patterns: [
[2200, 2204]
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVP2,
size: 3
}
};
function findType(type) {

@@ -254,44 +49,18 @@ return customCards[type] || types[type];

function isValidInputType(cardNumber) {
return typeof cardNumber === 'string' || cardNumber instanceof String;
function getAllCardTypes() {
return testOrder.map(function (type) {
return clone(findType(type));
});
}
function hasEnoughResultsToDetermineBestMatch(results) {
var numberOfResultsWithMaxStrengthProperty = results.filter(function (result) {
return result.matchStrength;
}).length;
function getCardPosition(name, ignoreErrorForNotExisting) {
var position = testOrder.indexOf(name);
// if all possible results have a maxStrength property
// that means the card number is sufficiently long
// enough to determine conclusively what the type is
return numberOfResultsWithMaxStrengthProperty > 0 &&
numberOfResultsWithMaxStrengthProperty === results.length;
}
function findBestMatch(results) {
if (!hasEnoughResultsToDetermineBestMatch(results)) {
return;
if (!ignoreErrorForNotExisting && position === -1) {
throw new Error('"' + name + '" is not a supported card type.');
}
return results.reduce(function (bestMatch, result) { // eslint-disable-line consistent-return
if (!bestMatch) {
return result;
}
// if the current best match pattern is less specific
// than this result, set the result as the new best match
if (bestMatch.matchStrength < result.matchStrength) {
return result;
}
return bestMatch;
});
return position;
}
function getAllCardTypes() {
return testOrder.map(function (type) {
return clone(findType(type));
});
}
function creditCardType(cardNumber) {

@@ -306,3 +75,3 @@ var bestMatch;

if (cardNumber.length === 0) {
return getAllCardTypes();
return getAllCardTypes(testOrder);
}

@@ -313,3 +82,3 @@

loopOverCardPatterns(cardNumber, cardConfiguration, results);
addMatchingCardsToResults(cardNumber, cardConfiguration, results);
});

@@ -326,55 +95,2 @@

function loopOverCardPatterns(cardNumber, cardConfiguration, results) {
var i, pattern, patternLength, clonedCardConfiguration;
for (i = 0; i < cardConfiguration.patterns.length; i++) {
pattern = cardConfiguration.patterns[i];
if (!matches(cardNumber, pattern)) {
continue;
}
clonedCardConfiguration = clone(cardConfiguration);
if (Array.isArray(pattern)) {
patternLength = String(pattern[0]).length;
} else {
patternLength = String(pattern).length;
}
if (cardNumber.length >= patternLength) {
clonedCardConfiguration.matchStrength = patternLength;
}
results.push(clonedCardConfiguration);
break;
}
}
function matches(cardNumber, pattern) {
if (Array.isArray(pattern)) {
return matchesRange(cardNumber, pattern[0], pattern[1]);
}
return matchesPattern(cardNumber, pattern);
}
function matchesPattern(cardNumber, pattern) {
pattern = String(pattern);
return pattern.substring(0, cardNumber.length) === cardNumber.substring(0, pattern.length);
}
// Adapted from https://github.com/polvo-labs/card-type/blob/aaab11f80fa1939bccc8f24905a06ae3cd864356/src/cardType.js#L37-L42
function matchesRange(cardNumber, min, max) {
var maxLengthToCheck = String(min).length;
var substr = cardNumber.substr(0, maxLengthToCheck);
var integerRepresentationOfCardNumber = parseInt(substr, 10);
min = parseInt(String(min).substr(0, substr.length), 10);
max = parseInt(String(max).substr(0, substr.length), 10);
return integerRepresentationOfCardNumber >= min && integerRepresentationOfCardNumber <= max;
}
creditCardType.getTypeInfo = function (type) {

@@ -384,12 +100,2 @@ return clone(findType(type));

function getCardPosition(name, ignoreErrorForNotExisting) {
var position = testOrder.indexOf(name);
if (!ignoreErrorForNotExisting && position === -1) {
throw new Error('"' + name + '" is not a supported card type.');
}
return position;
}
creditCardType.removeCard = function (name) {

@@ -446,18 +152,364 @@ var position = getCardPosition(name);

creditCardType.types = {
VISA: VISA,
MASTERCARD: MASTERCARD,
AMERICAN_EXPRESS: AMERICAN_EXPRESS,
DINERS_CLUB: DINERS_CLUB,
DISCOVER: DISCOVER,
JCB: JCB,
UNIONPAY: UNIONPAY,
MAESTRO: MAESTRO,
ELO: ELO,
MIR: MIR
};
creditCardType.types = cardNames;
module.exports = creditCardType;
},{"./lib/add-matching-cards-to-results":2,"./lib/card-types":3,"./lib/clone":4,"./lib/find-best-match":5,"./lib/is-valid-input-type":6}],2:[function(require,module,exports){
'use strict';
var clone = require('./clone');
var matches = require('./matches');
function addMatchingCardsToResults(cardNumber, cardConfiguration, results) {
var i, pattern, patternLength, clonedCardConfiguration;
for (i = 0; i < cardConfiguration.patterns.length; i++) {
pattern = cardConfiguration.patterns[i];
if (!matches(cardNumber, pattern)) {
continue;
}
clonedCardConfiguration = clone(cardConfiguration);
if (Array.isArray(pattern)) {
patternLength = String(pattern[0]).length;
} else {
patternLength = String(pattern).length;
}
if (cardNumber.length >= patternLength) {
clonedCardConfiguration.matchStrength = patternLength;
}
results.push(clonedCardConfiguration);
break;
}
}
module.exports = addMatchingCardsToResults;
},{"./clone":4,"./matches":7}],3:[function(require,module,exports){
'use strict';
var cardTypes = {
visa: {
niceType: 'Visa',
type: 'visa',
patterns: [
4
],
gaps: [4, 8, 12],
lengths: [16, 18, 19],
code: {
name: 'CVV',
size: 3
}
},
mastercard: {
niceType: 'Mastercard',
type: 'mastercard',
patterns: [
[51, 55],
[2221, 2229],
[223, 229],
[23, 26],
[270, 271],
2720
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
'american-express': {
niceType: 'American Express',
type: 'american-express',
patterns: [
34,
37
],
gaps: [4, 10],
lengths: [15],
code: {
name: 'CID',
size: 4
}
},
'diners-club': {
niceType: 'Diners Club',
type: 'diners-club',
patterns: [
[300, 305],
36,
38,
39
],
gaps: [4, 10],
lengths: [14, 16, 19],
code: {
name: 'CVV',
size: 3
}
},
discover: {
niceType: 'Discover',
type: 'discover',
patterns: [
6011,
[644, 649],
65
],
gaps: [4, 8, 12],
lengths: [16, 19],
code: {
name: 'CID',
size: 3
}
},
jcb: {
niceType: 'JCB',
type: 'jcb',
patterns: [
2131,
1800,
[3528, 3589]
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CVV',
size: 3
}
},
unionpay: {
niceType: 'UnionPay',
type: 'unionpay',
patterns: [
620,
[624, 626],
[62100, 62182],
[62184, 62187],
[62185, 62197],
[62200, 62205],
[622010, 622999],
622018,
[622019, 622999],
[62207, 62209],
[622126, 622925],
[623, 626],
6270,
6272,
6276,
[627700, 627779],
[627781, 627799],
[6282, 6289],
6291,
6292
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CVN',
size: 3
}
},
maestro: {
niceType: 'Maestro',
type: 'maestro',
patterns: [
493698,
[500000, 506698],
[506779, 508999],
[56, 59],
63,
67,
6
],
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: 'CVC',
size: 3
}
},
elo: {
niceType: 'Elo',
type: 'elo',
patterns: [
401178,
401179,
438935,
457631,
457632,
431274,
451416,
457393,
504175,
[506699, 506778],
[509000, 509999],
627780,
636297,
636368,
[650031, 650033],
[650035, 650051],
[650405, 650439],
[650485, 650538],
[650541, 650598],
[650700, 650718],
[650720, 650727],
[650901, 650978],
[651652, 651679],
[655000, 655019],
[655021, 655058]
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVE',
size: 3
}
},
mir: {
niceType: 'Mir',
type: 'mir',
patterns: [
[2200, 2204]
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CVP2',
size: 3
}
},
hiper: {
niceType: 'Hiper',
type: 'hiper',
patterns: [
637095,
637568,
637599,
637609,
637612
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
hipercard: {
niceType: 'Hipercard',
type: 'hipercard',
patterns: [
606282
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
}
};
module.exports = cardTypes;
},{}],4:[function(require,module,exports){
'use strict';
function clone(originalObject) {
var dupe;
if (!originalObject) { return null; }
dupe = JSON.parse(JSON.stringify(originalObject));
return dupe;
}
module.exports = clone;
},{}],5:[function(require,module,exports){
'use strict';
function hasEnoughResultsToDetermineBestMatch(results) {
var numberOfResultsWithMaxStrengthProperty = results.filter(function (result) {
return result.matchStrength;
}).length;
// if all possible results have a maxStrength property
// that means the card number is sufficiently long
// enough to determine conclusively what the type is
return numberOfResultsWithMaxStrengthProperty > 0 &&
numberOfResultsWithMaxStrengthProperty === results.length;
}
function findBestMatch(results) {
if (!hasEnoughResultsToDetermineBestMatch(results)) {
return;
}
return results.reduce(function (bestMatch, result) { // eslint-disable-line consistent-return
if (!bestMatch) {
return result;
}
// if the current best match pattern is less specific
// than this result, set the result as the new best match
if (bestMatch.matchStrength < result.matchStrength) {
return result;
}
return bestMatch;
});
}
module.exports = findBestMatch;
},{}],6:[function(require,module,exports){
'use strict';
function isValidInputType(cardNumber) {
return typeof cardNumber === 'string' || cardNumber instanceof String;
}
module.exports = isValidInputType;
},{}],7:[function(require,module,exports){
'use strict';
// Adapted from https://github.com/polvo-labs/card-type/blob/aaab11f80fa1939bccc8f24905a06ae3cd864356/src/cardType.js#L37-L42
function matchesRange(cardNumber, min, max) {
var maxLengthToCheck = String(min).length;
var substr = cardNumber.substr(0, maxLengthToCheck);
var integerRepresentationOfCardNumber = parseInt(substr, 10);
min = parseInt(String(min).substr(0, substr.length), 10);
max = parseInt(String(max).substr(0, substr.length), 10);
return integerRepresentationOfCardNumber >= min && integerRepresentationOfCardNumber <= max;
}
function matchesPattern(cardNumber, pattern) {
pattern = String(pattern);
return pattern.substring(0, cardNumber.length) === cardNumber.substring(0, pattern.length);
}
function matches(cardNumber, pattern) {
if (Array.isArray(pattern)) {
return matchesRange(cardNumber, pattern[0], pattern[1]);
}
return matchesPattern(cardNumber, pattern);
}
module.exports = matches;
},{}]},{},[1])(1)
});

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.creditCardType=e()}}(function(){return function e(n,t,r){function i(s,o){if(!t[s]){if(!n[s]){var p="function"==typeof require&&require;if(!o&&p)return p(s,!0);if(a)return a(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var u=t[s]={exports:{}};n[s][0].call(u.exports,function(e){var t=n[s][1][e];return i(t?t:e)},u,u.exports,e,n,t,r)}return t[s].exports}for(var a="function"==typeof require&&require,s=0;s<r.length;s++)i(r[s]);return i}({1:[function(e,n,t){"use strict";function r(e){var n;return e?n=JSON.parse(JSON.stringify(e)):null}function i(e){return m[e]||h[e]}function a(e){return"string"==typeof e||e instanceof String}function s(e){var n=e.filter(function(e){return e.matchStrength}).length;return n>0&&n===e.length}function o(e){if(s(e))return e.reduce(function(e,n){return e?e.matchStrength<n.matchStrength?n:e:n})}function p(){return l.map(function(e){return r(i(e))})}function c(e){var n,t=[];return a(e)?0===e.length?p():(l.forEach(function(n){var r=i(n);u(e,r,t)}),n=o(t),n?[n]:t):[]}function u(e,n,t){var i,a,s,o;for(i=0;i<n.patterns.length;i++)if(a=n.patterns[i],f(e,a)){o=r(n),s=Array.isArray(a)?String(a[0]).length:String(a).length,e.length>=s&&(o.matchStrength=s),t.push(o);break}}function f(e,n){return Array.isArray(n)?d(e,n[0],n[1]):g(e,n)}function g(e,n){return n=String(n),n.substring(0,e.length)===e.substring(0,n.length)}function d(e,n,t){var r=String(n).length,i=e.substr(0,r),a=parseInt(i,10);return n=parseInt(String(n).substr(0,i.length),10),t=parseInt(String(t).substr(0,i.length),10),a>=n&&a<=t}function y(e,n){var t=l.indexOf(e);if(!n&&t===-1)throw new Error('"'+e+'" is not a supported card type.');return t}var l,h={},m={},v="visa",C="mastercard",S="american-express",E="diners-club",T="discover",b="elo",w="jcb",A="unionpay",O="maestro",z="mir",I="CVV",x="CID",M="CVC",N="CVN",V="CVP2",D="CVE",R=[v,C,S,E,T,w,A,O,b,z];l=r(R),h[v]={niceType:"Visa",type:v,patterns:[4],gaps:[4,8,12],lengths:[16,18,19],code:{name:I,size:3}},h[C]={niceType:"Mastercard",type:C,patterns:[[51,55],[2221,2229],[223,229],[23,26],[270,271],2720],gaps:[4,8,12],lengths:[16],code:{name:M,size:3}},h[S]={niceType:"American Express",type:S,patterns:[34,37],gaps:[4,10],lengths:[15],code:{name:x,size:4}},h[E]={niceType:"Diners Club",type:E,patterns:[[300,305],36,38,39],gaps:[4,10],lengths:[14,16,19],code:{name:I,size:3}},h[T]={niceType:"Discover",type:T,patterns:[6011,[644,649],65],gaps:[4,8,12],lengths:[16,19],code:{name:x,size:3}},h[w]={niceType:"JCB",type:w,patterns:[2131,1800,[3528,3589]],gaps:[4,8,12],lengths:[16,17,18,19],code:{name:I,size:3}},h[A]={niceType:"UnionPay",type:A,patterns:[620,[624,626],[62100,62182],[62184,62187],[62185,62197],[62200,62205],[622010,622999],622018,[622019,622999],[62207,62209],[622126,622925],[623,626],6270,6272,6276,[627700,627779],[627781,627799],[6282,6289],6291,6292],gaps:[4,8,12],lengths:[16,17,18,19],code:{name:N,size:3}},h[O]={niceType:"Maestro",type:O,patterns:[493698,[5e5,506698],[506779,508999],[56,59],63,67,6],gaps:[4,8,12],lengths:[12,13,14,15,16,17,18,19],code:{name:M,size:3}},h[b]={niceType:"Elo",type:b,patterns:[401178,401179,438935,457631,457632,431274,451416,457393,504175,[506699,506778],[509e3,509999],627780,636297,636368,[650031,650033],[650035,650051],[650405,650439],[650485,650538],[650541,650598],[650700,650718],[650720,650727],[650901,650978],[651652,651679],[655e3,655019],[655021,655058]],gaps:[4,8,12],lengths:[16],code:{name:D,size:3}},h[z]={niceType:"Mir",type:z,patterns:[[2200,2204]],gaps:[4,8,12],lengths:[16,17,18,19],code:{name:V,size:3}},c.getTypeInfo=function(e){return r(i(e))},c.removeCard=function(e){var n=y(e);l.splice(n,1)},c.addCard=function(e){var n=y(e.type,!0);m[e.type]=e,n===-1&&l.push(e.type)},c.updateCard=function(e,n){var t,i=m[e]||h[e];if(!i)throw new Error('"'+e+'" is not a recognized type. Use `addCard` instead.');if(n.type&&i.type!==n.type)throw new Error("Cannot overwrite type parameter.");t=r(i,!0),Object.keys(t).forEach(function(e){n[e]&&(t[e]=n[e])}),m[t.type]=t},c.changeOrder=function(e,n){var t=y(e);l.splice(t,1),l.splice(n,0,e)},c.resetModifications=function(){l=r(R),m={}},c.types={VISA:v,MASTERCARD:C,AMERICAN_EXPRESS:S,DINERS_CLUB:E,DISCOVER:T,JCB:w,UNIONPAY:A,MAESTRO:O,ELO:b,MIR:z},n.exports=c},{}]},{},[1])(1)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;"undefined"!=typeof window?t=window:"undefined"!=typeof global?t=global:"undefined"!=typeof self&&(t=self),t.creditCardType=e()}}(function(){return function e(t,n,r){function i(a,o){if(!n[a]){if(!t[a]){var c="function"==typeof require&&require;if(!o&&c)return c(a,!0);if(s)return s(a,!0);var p=new Error("Cannot find module '"+a+"'");throw p.code="MODULE_NOT_FOUND",p}var u=n[a]={exports:{}};t[a][0].call(u.exports,function(e){var n=t[a][1][e];return i(n?n:e)},u,u.exports,e,t,n,r)}return n[a].exports}for(var s="function"==typeof require&&require,a=0;a<r.length;a++)i(r[a]);return i}({1:[function(e,t,n){"use strict";function r(e){return l[e]||c[e]}function i(){return o.map(function(e){return p(r(e))})}function s(e,t){var n=o.indexOf(e);if(!t&&n===-1)throw new Error('"'+e+'" is not a supported card type.');return n}function a(e){var t,n=[];return f(e)?0===e.length?i(o):(o.forEach(function(t){var i=r(t);d(e,i,n)}),t=u(n),t?[t]:n):[]}var o,c=e("./lib/card-types"),p=e("./lib/clone"),u=e("./lib/find-best-match"),f=e("./lib/is-valid-input-type"),d=e("./lib/add-matching-cards-to-results"),l={},y={VISA:"visa",MASTERCARD:"mastercard",AMERICAN_EXPRESS:"american-express",DINERS_CLUB:"diners-club",DISCOVER:"discover",JCB:"jcb",UNIONPAY:"unionpay",MAESTRO:"maestro",ELO:"elo",MIR:"mir",HIPER:"hiper",HIPERCARD:"hipercard"},g=[y.VISA,y.MASTERCARD,y.AMERICAN_EXPRESS,y.DINERS_CLUB,y.DISCOVER,y.JCB,y.UNIONPAY,y.MAESTRO,y.ELO,y.MIR,y.HIPER,y.HIPERCARD];o=p(g),a.getTypeInfo=function(e){return p(r(e))},a.removeCard=function(e){var t=s(e);o.splice(t,1)},a.addCard=function(e){var t=s(e.type,!0);l[e.type]=e,t===-1&&o.push(e.type)},a.updateCard=function(e,t){var n,r=l[e]||c[e];if(!r)throw new Error('"'+e+'" is not a recognized type. Use `addCard` instead.');if(t.type&&r.type!==t.type)throw new Error("Cannot overwrite type parameter.");n=p(r,!0),Object.keys(n).forEach(function(e){t[e]&&(n[e]=t[e])}),l[n.type]=n},a.changeOrder=function(e,t){var n=s(e);o.splice(n,1),o.splice(t,0,e)},a.resetModifications=function(){o=p(g),l={}},a.types=y,t.exports=a},{"./lib/add-matching-cards-to-results":2,"./lib/card-types":3,"./lib/clone":4,"./lib/find-best-match":5,"./lib/is-valid-input-type":6}],2:[function(e,t,n){"use strict";function r(e,t,n){var r,a,o,c;for(r=0;r<t.patterns.length;r++)if(a=t.patterns[r],s(e,a)){c=i(t),o=Array.isArray(a)?String(a[0]).length:String(a).length,e.length>=o&&(c.matchStrength=o),n.push(c);break}}var i=e("./clone"),s=e("./matches");t.exports=r},{"./clone":4,"./matches":7}],3:[function(e,t,n){"use strict";var r={visa:{niceType:"Visa",type:"visa",patterns:[4],gaps:[4,8,12],lengths:[16,18,19],code:{name:"CVV",size:3}},mastercard:{niceType:"Mastercard",type:"mastercard",patterns:[[51,55],[2221,2229],[223,229],[23,26],[270,271],2720],gaps:[4,8,12],lengths:[16],code:{name:"CVC",size:3}},"american-express":{niceType:"American Express",type:"american-express",patterns:[34,37],gaps:[4,10],lengths:[15],code:{name:"CID",size:4}},"diners-club":{niceType:"Diners Club",type:"diners-club",patterns:[[300,305],36,38,39],gaps:[4,10],lengths:[14,16,19],code:{name:"CVV",size:3}},discover:{niceType:"Discover",type:"discover",patterns:[6011,[644,649],65],gaps:[4,8,12],lengths:[16,19],code:{name:"CID",size:3}},jcb:{niceType:"JCB",type:"jcb",patterns:[2131,1800,[3528,3589]],gaps:[4,8,12],lengths:[16,17,18,19],code:{name:"CVV",size:3}},unionpay:{niceType:"UnionPay",type:"unionpay",patterns:[620,[624,626],[62100,62182],[62184,62187],[62185,62197],[62200,62205],[622010,622999],622018,[622019,622999],[62207,62209],[622126,622925],[623,626],6270,6272,6276,[627700,627779],[627781,627799],[6282,6289],6291,6292],gaps:[4,8,12],lengths:[16,17,18,19],code:{name:"CVN",size:3}},maestro:{niceType:"Maestro",type:"maestro",patterns:[493698,[5e5,506698],[506779,508999],[56,59],63,67,6],gaps:[4,8,12],lengths:[12,13,14,15,16,17,18,19],code:{name:"CVC",size:3}},elo:{niceType:"Elo",type:"elo",patterns:[401178,401179,438935,457631,457632,431274,451416,457393,504175,[506699,506778],[509e3,509999],627780,636297,636368,[650031,650033],[650035,650051],[650405,650439],[650485,650538],[650541,650598],[650700,650718],[650720,650727],[650901,650978],[651652,651679],[655e3,655019],[655021,655058]],gaps:[4,8,12],lengths:[16],code:{name:"CVE",size:3}},mir:{niceType:"Mir",type:"mir",patterns:[[2200,2204]],gaps:[4,8,12],lengths:[16,17,18,19],code:{name:"CVP2",size:3}},hiper:{niceType:"Hiper",type:"hiper",patterns:[637095,637568,637599,637609,637612],gaps:[4,8,12],lengths:[16],code:{name:"CVC",size:3}},hipercard:{niceType:"Hipercard",type:"hipercard",patterns:[606282],gaps:[4,8,12],lengths:[16],code:{name:"CVC",size:3}}};t.exports=r},{}],4:[function(e,t,n){"use strict";function r(e){var t;return e?t=JSON.parse(JSON.stringify(e)):null}t.exports=r},{}],5:[function(e,t,n){"use strict";function r(e){var t=e.filter(function(e){return e.matchStrength}).length;return t>0&&t===e.length}function i(e){if(r(e))return e.reduce(function(e,t){return e?e.matchStrength<t.matchStrength?t:e:t})}t.exports=i},{}],6:[function(e,t,n){"use strict";function r(e){return"string"==typeof e||e instanceof String}t.exports=r},{}],7:[function(e,t,n){"use strict";function r(e,t,n){var r=String(t).length,i=e.substr(0,r),s=parseInt(i,10);return t=parseInt(String(t).substr(0,i.length),10),n=parseInt(String(n).substr(0,i.length),10),s>=t&&s<=n}function i(e,t){return t=String(t),t.substring(0,e.length)===e.substring(0,t.length)}function s(e,t){return Array.isArray(t)?r(e,t[0],t[1]):i(e,t)}t.exports=s},{}]},{},[1])(1)});
'use strict';
var types = require('./lib/card-types');
var clone = require('./lib/clone');
var findBestMatch = require('./lib/find-best-match');
var isValidInputType = require('./lib/is-valid-input-type');
var addMatchingCardsToResults = require('./lib/add-matching-cards-to-results');
var testOrder;
var types = {};
var customCards = {};
var VISA = 'visa';
var MASTERCARD = 'mastercard';
var AMERICAN_EXPRESS = 'american-express';
var DINERS_CLUB = 'diners-club';
var DISCOVER = 'discover';
var ELO = 'elo';
var JCB = 'jcb';
var UNIONPAY = 'unionpay';
var MAESTRO = 'maestro';
var MIR = 'mir';
var CVV = 'CVV';
var CID = 'CID';
var CVC = 'CVC';
var CVN = 'CVN';
var CVP2 = 'CVP2';
var CVE = 'CVE';
var cardNames = {
VISA: 'visa',
MASTERCARD: 'mastercard',
AMERICAN_EXPRESS: 'american-express',
DINERS_CLUB: 'diners-club',
DISCOVER: 'discover',
JCB: 'jcb',
UNIONPAY: 'unionpay',
MAESTRO: 'maestro',
ELO: 'elo',
MIR: 'mir',
HIPER: 'hiper',
HIPERCARD: 'hipercard'
};
var ORIGINAL_TEST_ORDER = [
VISA,
MASTERCARD,
AMERICAN_EXPRESS,
DINERS_CLUB,
DISCOVER,
JCB,
UNIONPAY,
MAESTRO,
ELO,
MIR
cardNames.VISA,
cardNames.MASTERCARD,
cardNames.AMERICAN_EXPRESS,
cardNames.DINERS_CLUB,
cardNames.DISCOVER,
cardNames.JCB,
cardNames.UNIONPAY,
cardNames.MAESTRO,
cardNames.ELO,
cardNames.MIR,
cardNames.HIPER,
cardNames.HIPERCARD
];
function clone(originalObject) {
var dupe;
if (!originalObject) { return null; }
dupe = JSON.parse(JSON.stringify(originalObject));
return dupe;
}
testOrder = clone(ORIGINAL_TEST_ORDER);
types[VISA] = {
niceType: 'Visa',
type: VISA,
patterns: [
4
],
gaps: [4, 8, 12],
lengths: [16, 18, 19],
code: {
name: CVV,
size: 3
}
};
types[MASTERCARD] = {
niceType: 'Mastercard',
type: MASTERCARD,
patterns: [
[51, 55],
[2221, 2229],
[223, 229],
[23, 26],
[270, 271],
2720
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVC,
size: 3
}
};
types[AMERICAN_EXPRESS] = {
niceType: 'American Express',
type: AMERICAN_EXPRESS,
patterns: [
34,
37
],
gaps: [4, 10],
lengths: [15],
code: {
name: CID,
size: 4
}
};
types[DINERS_CLUB] = {
niceType: 'Diners Club',
type: DINERS_CLUB,
patterns: [
[300, 305],
36,
38,
39
],
gaps: [4, 10],
lengths: [14, 16, 19],
code: {
name: CVV,
size: 3
}
};
types[DISCOVER] = {
niceType: 'Discover',
type: DISCOVER,
patterns: [
6011,
[644, 649],
65
],
gaps: [4, 8, 12],
lengths: [16, 19],
code: {
name: CID,
size: 3
}
};
types[JCB] = {
niceType: 'JCB',
type: JCB,
patterns: [
2131,
1800,
[3528, 3589]
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVV,
size: 3
}
};
types[UNIONPAY] = {
niceType: 'UnionPay',
type: UNIONPAY,
patterns: [
620,
[624, 626],
[62100, 62182],
[62184, 62187],
[62185, 62197],
[62200, 62205],
[622010, 622999],
622018,
[622019, 622999],
[62207, 62209],
[622126, 622925],
[623, 626],
6270,
6272,
6276,
[627700, 627779],
[627781, 627799],
[6282, 6289],
6291,
6292
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVN,
size: 3
}
};
types[MAESTRO] = {
niceType: 'Maestro',
type: MAESTRO,
patterns: [
493698,
[500000, 506698],
[506779, 508999],
[56, 59],
63,
67,
6
],
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: CVC,
size: 3
}
};
types[ELO] = {
niceType: 'Elo',
type: ELO,
patterns: [
401178,
401179,
438935,
457631,
457632,
431274,
451416,
457393,
504175,
[506699, 506778],
[509000, 509999],
627780,
636297,
636368,
[650031, 650033],
[650035, 650051],
[650405, 650439],
[650485, 650538],
[650541, 650598],
[650700, 650718],
[650720, 650727],
[650901, 650978],
[651652, 651679],
[655000, 655019],
[655021, 655058]
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: CVE,
size: 3
}
};
types[MIR] = {
niceType: 'Mir',
type: MIR,
patterns: [
[2200, 2204]
],
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: CVP2,
size: 3
}
};
function findType(type) {

@@ -253,44 +48,18 @@ return customCards[type] || types[type];

function isValidInputType(cardNumber) {
return typeof cardNumber === 'string' || cardNumber instanceof String;
function getAllCardTypes() {
return testOrder.map(function (type) {
return clone(findType(type));
});
}
function hasEnoughResultsToDetermineBestMatch(results) {
var numberOfResultsWithMaxStrengthProperty = results.filter(function (result) {
return result.matchStrength;
}).length;
function getCardPosition(name, ignoreErrorForNotExisting) {
var position = testOrder.indexOf(name);
// if all possible results have a maxStrength property
// that means the card number is sufficiently long
// enough to determine conclusively what the type is
return numberOfResultsWithMaxStrengthProperty > 0 &&
numberOfResultsWithMaxStrengthProperty === results.length;
}
function findBestMatch(results) {
if (!hasEnoughResultsToDetermineBestMatch(results)) {
return;
if (!ignoreErrorForNotExisting && position === -1) {
throw new Error('"' + name + '" is not a supported card type.');
}
return results.reduce(function (bestMatch, result) { // eslint-disable-line consistent-return
if (!bestMatch) {
return result;
}
// if the current best match pattern is less specific
// than this result, set the result as the new best match
if (bestMatch.matchStrength < result.matchStrength) {
return result;
}
return bestMatch;
});
return position;
}
function getAllCardTypes() {
return testOrder.map(function (type) {
return clone(findType(type));
});
}
function creditCardType(cardNumber) {

@@ -305,3 +74,3 @@ var bestMatch;

if (cardNumber.length === 0) {
return getAllCardTypes();
return getAllCardTypes(testOrder);
}

@@ -312,3 +81,3 @@

loopOverCardPatterns(cardNumber, cardConfiguration, results);
addMatchingCardsToResults(cardNumber, cardConfiguration, results);
});

@@ -325,55 +94,2 @@

function loopOverCardPatterns(cardNumber, cardConfiguration, results) {
var i, pattern, patternLength, clonedCardConfiguration;
for (i = 0; i < cardConfiguration.patterns.length; i++) {
pattern = cardConfiguration.patterns[i];
if (!matches(cardNumber, pattern)) {
continue;
}
clonedCardConfiguration = clone(cardConfiguration);
if (Array.isArray(pattern)) {
patternLength = String(pattern[0]).length;
} else {
patternLength = String(pattern).length;
}
if (cardNumber.length >= patternLength) {
clonedCardConfiguration.matchStrength = patternLength;
}
results.push(clonedCardConfiguration);
break;
}
}
function matches(cardNumber, pattern) {
if (Array.isArray(pattern)) {
return matchesRange(cardNumber, pattern[0], pattern[1]);
}
return matchesPattern(cardNumber, pattern);
}
function matchesPattern(cardNumber, pattern) {
pattern = String(pattern);
return pattern.substring(0, cardNumber.length) === cardNumber.substring(0, pattern.length);
}
// Adapted from https://github.com/polvo-labs/card-type/blob/aaab11f80fa1939bccc8f24905a06ae3cd864356/src/cardType.js#L37-L42
function matchesRange(cardNumber, min, max) {
var maxLengthToCheck = String(min).length;
var substr = cardNumber.substr(0, maxLengthToCheck);
var integerRepresentationOfCardNumber = parseInt(substr, 10);
min = parseInt(String(min).substr(0, substr.length), 10);
max = parseInt(String(max).substr(0, substr.length), 10);
return integerRepresentationOfCardNumber >= min && integerRepresentationOfCardNumber <= max;
}
creditCardType.getTypeInfo = function (type) {

@@ -383,12 +99,2 @@ return clone(findType(type));

function getCardPosition(name, ignoreErrorForNotExisting) {
var position = testOrder.indexOf(name);
if (!ignoreErrorForNotExisting && position === -1) {
throw new Error('"' + name + '" is not a supported card type.');
}
return position;
}
creditCardType.removeCard = function (name) {

@@ -445,15 +151,4 @@ var position = getCardPosition(name);

creditCardType.types = {
VISA: VISA,
MASTERCARD: MASTERCARD,
AMERICAN_EXPRESS: AMERICAN_EXPRESS,
DINERS_CLUB: DINERS_CLUB,
DISCOVER: DISCOVER,
JCB: JCB,
UNIONPAY: UNIONPAY,
MAESTRO: MAESTRO,
ELO: ELO,
MIR: MIR
};
creditCardType.types = cardNames;
module.exports = creditCardType;
{
"name": "credit-card-type",
"version": "8.0.0",
"version": "8.1.0",
"description": "A library for determining credit card type",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -42,9 +42,9 @@ Credit Card Type [![Build Status](https://travis-ci.org/braintree/credit-card-type.svg)](https://travis-ci.org/braintree/credit-card-type) [![npm version](https://badge.fury.io/js/credit-card-type.svg)](http://badge.fury.io/js/credit-card-type) [![Bower](https://badge.fury.io/bo/credit-card-type.svg)](http://badge.fury.io/bo/credit-card-type)

| Key | Type | Description |
|------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `Mastercard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro`<br />- `Mir`<br />- `Elo` |
| `type` | `String` | A code-friendly presentation of the card brand (useful to class names in CSS). Please refer to Card Type "Constants" below for the list of possible values.<br/>- `visa`<br />- `mastercard`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro`<br />- `mir`<br /> - `elo` |
| `gaps` | `Array` | The expected indeces of gaps in a string representation of the card number. For example, in a Visa card, `4111 1111 1111 1111`, there are expected spaces in the 4th, 8th, and 12th positions. This is useful in setting your own formatting rules. |
| `lengths` | `Array` | The expected lengths of the card number as an array of strings (excluding spaces and `/` characters). |
| `code` | `Object` | The information regarding the security code for the determined card. Learn more about the [code object](#code) below. |
| Key | Type | Description |
|------------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `Mastercard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro`<br />- `Mir`<br />- `Elo`<br />- `Hiper`<br />- `Hipercard` |
| `type` | `String` | A code-friendly presentation of the card brand (useful to class names in CSS). Please refer to Card Type "Constants" below for the list of possible values.<br/>- `visa`<br />- `mastercard`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro`<br />- `mir`<br /> - `elo`<br /> - `hiper`<br /> - `hipercard` |
| `gaps` | `Array` | The expected indeces of gaps in a string representation of the card number. For example, in a Visa card, `4111 1111 1111 1111`, there are expected spaces in the 4th, 8th, and 12th positions. This is useful in setting your own formatting rules. |
| `lengths` | `Array` | The expected lengths of the card number as an array of strings (excluding spaces and `/` characters). |
| `code` | `Object` | The information regarding the security code for the determined card. Learn more about the [code object](#code) below. |

@@ -61,12 +61,14 @@ If no card types are found, this returns an empty array.

* `VISA`
* `MASTERCARD`
* `AMERICAN_EXPRESS`
* `DINERS_CLUB`
* `DISCOVER`
* `ELO`
* `HIPERCARD`
* `HIPER`
* `JCB`
* `UNIONPAY`
* `MAESTRO`
* `MASTERCARD`
* `MIR`
* `ELO`
* `UNIONPAY`
* `VISA`

@@ -89,2 +91,4 @@ #### `code`

| `Elo` | `CVE` | 3 |
| `Hiper` | `CVC` | 3 |
| `Hipercard` | `CVC` | 4 |

@@ -145,11 +149,2 @@ A full response for a `Visa` card will look like this:

```javascript
var visa = creditCardType.getTypeInfo(creditCardType.types.VISA);
visa.prefixPattern = /^(4)$/;
visa.exactPattern = /^(4[0-1])\d*$/; // restrict to only match visas that start with 40 or 41
creditCardType.addCard(visa);
```
#### Adding Card Types

@@ -207,4 +202,6 @@

creditCardType.types.MAESTRO,
creditCardType.types.ELO,
creditCardType.types.MIR,
creditCardType.types.ELO
creditCardType.types.HIPER,
creditCardType.types.HIPERCARD
]

@@ -211,0 +208,0 @@ ```

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