Skip to content

Commit

Permalink
Build CreditCardGenerator.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jermainee committed Jun 7, 2021
1 parent 6eb1171 commit 35d30a8
Showing 1 changed file with 72 additions and 72 deletions.
144 changes: 72 additions & 72 deletions src/CreditCardGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,38 @@
exports.__esModule = true;
var CreditCardGenerator = /** @class */ (function () {
function CreditCardGenerator() {
this.creditCardPresets = {
"VISA": {
digitCount: 16,
prefixes: [
"4539",
"4556",
"4916",
"4532",
"4929",
"4485",
"4716",
"4"
]
},
"MasterCard": {
digitCount: 16,
prefixes: [
"51",
"52",
"53",
"54",
"55"
]
},
"Amex": {
digitCount: 15,
prefixes: [
"34",
"37"
]
},
"Diners": {
digitCount: 16,
prefixes: [
"300",
"301",
"302",
"303",
"36",
"38"
]
},
"Discover": {
digitCount: 16,
prefixes: ["6011"]
},
"EnRoute": {
digitCount: 16,
prefixes: [
"2014",
"2149"
]
},
"JCB": {
digitCount: 16,
prefixes: [
"35"
]
},
"Voyager": {
digitCount: 16,
prefixes: ["8699"]
}
};
}
CreditCardGenerator.prototype.generateSingle = function (vendor) {
CreditCardGenerator.generateSingle = function (vendor) {
if (!this.creditCardPresets[vendor]) {
throw new Error("[CreditCardGenerator] Unknown credit card vendor '" + vendor + "'");
}
return this.generateCreditCardNumber(this.creditCardPresets[vendor]);
return this.generateWithPreset(this.creditCardPresets[vendor]);
};
CreditCardGenerator.prototype.generateMultiple = function (vendor, count) {
CreditCardGenerator.generateMultiple = function (vendor, count) {
if (!this.creditCardPresets[vendor]) {
throw new Error("[CreditCardGenerator] Unknown credit card vendor '" + vendor + "'");
}
var preset = this.creditCardPresets[vendor];
var numbers = [];
while (numbers.length < count) {
numbers.push(this.generateCreditCardNumber(preset));
numbers.push(this.generateWithPreset(preset));
}
return numbers;
};
CreditCardGenerator.prototype.generateCreditCardNumber = function (preset) {
CreditCardGenerator.generateWithPreset = function (preset) {
var prefix = preset.prefixes[Math.floor(Math.random() * preset.prefixes.length)];
var numberWithPrefix = prefix + this.generateRandomNumber(preset.digitCount);
var checksum = this.calculateChecksum(numberWithPrefix);
return numberWithPrefix + checksum;
};
CreditCardGenerator.prototype.generateRandomNumber = function (length) {
CreditCardGenerator.generateRandomNumber = function (length) {
var cardNumber = "";
while (cardNumber.length < (length - 1)) {
cardNumber += Math.floor(Math.random() * 10);
}
return cardNumber;
};
CreditCardGenerator.prototype.calculateChecksum = function (cardNumber) {
CreditCardGenerator.calculateChecksum = function (cardNumber) {
var reversedCardNumber = this.reverseString(cardNumber);
var reversedCardNumberArray = reversedCardNumber.split("");
var sum = 0;
Expand All @@ -106,11 +42,75 @@ var CreditCardGenerator = /** @class */ (function () {
}
return ((Math.floor(sum / 10) + 1) * 10 - sum) % 10;
};
CreditCardGenerator.prototype.reverseString = function (string) {
CreditCardGenerator.reverseString = function (string) {
var stringParts = string.split("");
var reverseArray = stringParts.reverse();
return reverseArray.join("");
};
CreditCardGenerator.creditCardPresets = [
{
digitCount: 16,
prefixes: [
"4539",
"4556",
"4916",
"4532",
"4929",
"4485",
"4716",
"4"
]
},
{
digitCount: 16,
prefixes: [
"51",
"52",
"53",
"54",
"55"
]
},
{
digitCount: 15,
prefixes: [
"34",
"37"
]
},
{
digitCount: 16,
prefixes: [
"300",
"301",
"302",
"303",
"36",
"38"
]
},
{
digitCount: 16,
prefixes: ["6011"]
},
{
digitCount: 16,
prefixes: [
"2014",
"2149"
]
},
{
digitCount: 16,
prefixes: [
"35"
]
},
{
digitCount: 16,
prefixes: ["8699"]
}
];
return CreditCardGenerator;
}());
exports["default"] = CreditCardGenerator;

0 comments on commit 35d30a8

Please sign in to comment.