-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.15.0 feature to get banks for specific countries
- Loading branch information
1 parent
ba191f1
commit a73ed8e
Showing
9 changed files
with
691 additions
and
879 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
const fs = require('fs-promise'); | ||
const path = require('path'); | ||
const helper = require('./helper'); | ||
const { writeFile, readdir, lstatSync } = require('fs-promise'); | ||
const { join } = require('path'); | ||
const { logError, logSuccess } = require('./helper'); | ||
|
||
const path = name => join(__dirname, name ? `banks/${name}` : 'banks'); | ||
|
||
const generateRequiresFile = (filePath, requires) => { | ||
const content = `module.exports = [${requires}\n];\n`; | ||
|
||
fs.writeFile(path.join(__dirname, `banks/${filePath}`), content).then(() => { | ||
writeFile(path(filePath), content).then(() => { | ||
const dirPath = new RegExp(`${__dirname}/`); | ||
helper.success(filePath.replace(dirPath, '')); | ||
}).catch((err) => { helper.error(`Can't write ${filePath} \n${err}`); }); | ||
logSuccess(filePath.replace(dirPath, '')); | ||
}).catch((err) => { logError(`Can't write ${filePath} \n${err}`); }); | ||
}; | ||
|
||
const createRequires = (files) => { | ||
const countries = files.filter(file => fs.lstatSync(path.join(__dirname, `banks/${file}`)).isDirectory()); | ||
const countries = files.filter(file => lstatSync(path(file)).isDirectory()); | ||
const indexFilesRequires = countries.map(country => `\n require('./${country}/index')`); | ||
|
||
countries.forEach((country) => { | ||
fs.readdir(path.join(__dirname, `banks/${country}`)) | ||
readdir(path(country)) | ||
.then((items) => { | ||
const banksRequires = items | ||
.filter(file => /\.json$/.test(file)) | ||
.map(bank => `\n require('./${bank.replace(/\.json$/, '')}')`); | ||
generateRequiresFile(`${country}/index.js`, banksRequires); | ||
}).catch((err) => { helper.error(`Can't read ${country} directory \n${err}`); }); | ||
}).catch((err) => { logError(`Can't read ${country} directory \n${err}`); }); | ||
}); | ||
|
||
generateRequiresFile('index.js', indexFilesRequires); | ||
}; | ||
|
||
fs.readdir(path.join(__dirname, 'banks')).then(createRequires).catch(helper.error); | ||
readdir(path()).then(createRequires).catch(logError); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
var type = require('./type'); | ||
|
||
function BanksDB(data) { | ||
var banksData = []; | ||
var prefixes = {}; | ||
|
||
if (Array.isArray(data)) { | ||
banksData = data; | ||
} else if (data) { | ||
banksData.push(data); | ||
} else { | ||
console.warn('Empty or invalid arguments of Banks DB Core call. Check Banks DB docs for details.'); | ||
} | ||
|
||
this.data = banksData.reduce(function (acc, current) { | ||
return acc.concat(current); | ||
}, []); | ||
|
||
this.data.forEach(function (bank) { | ||
bank.code = bank.country + '-' + bank.name; | ||
bank.prefixes.forEach(function (prefix) { | ||
prefixes[prefix] = bank; | ||
}); | ||
}); | ||
|
||
this.findBank = function (cardNumber) { | ||
cardNumber = cardNumber || ''; | ||
|
||
var card = cardNumber.toString().replace(/[^\d]/g, ''); | ||
var first5 = card.substr(0, 5); | ||
var first6 = card.substr(0, 6); | ||
var bank = prefixes[first6] || prefixes[first5]; | ||
var result = { | ||
type: type(card) | ||
}; | ||
|
||
if (bank) { | ||
for (var el in bank) { | ||
result[el] = bank[el]; | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
} | ||
|
||
module.exports = function banksDBCore(data) { | ||
return new BanksDB(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
const chalk = require('chalk'); | ||
|
||
module.exports = { | ||
error: (text) => { | ||
logError: (text) => { | ||
console.error(chalk.red(`✘ ${text}`)); | ||
}, | ||
success: (text) => { | ||
logSuccess: (text) => { | ||
console.log(`${chalk.green('✓ ')}${chalk.white(text)}`); | ||
}, | ||
warn: (text) => { | ||
logWarning: (text) => { | ||
console.error(chalk.yellow(`⚠ ${text}`)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,8 @@ | ||
var type = require('./type'); | ||
var data = require('./banks/index'); | ||
var banksDBCore = require('./core'); | ||
|
||
var banks = []; | ||
var prefixes = {}; | ||
var BanksDB = banksDBCore(data); | ||
|
||
data.forEach(function (item) { | ||
banks = banks.concat(item); | ||
}); | ||
module.exports = BanksDB.findBank.bind(BanksDB); | ||
|
||
banks.forEach(function (bank) { | ||
bank.code = bank.country + '-' + bank.name; | ||
bank.prefixes.forEach(function (prefix) { | ||
prefixes[prefix] = bank; | ||
}); | ||
}); | ||
|
||
module.exports = function findBank(cardNumber) { | ||
cardNumber = cardNumber || ''; | ||
var card = cardNumber.toString().replace(/[^\d]/g, ''); | ||
var first5 = card.substr(0, 5); | ||
var first6 = card.substr(0, 6); | ||
var bank = prefixes[first6] || prefixes[first5]; | ||
var result = { | ||
type: type(card) | ||
}; | ||
|
||
if (bank) { | ||
for (var el in bank) { | ||
result[el] = bank[el]; | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
module.exports.data = banks; | ||
module.exports.data = BanksDB.data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "banks-db", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"description": "Bankcards BIN database", | ||
"author": "Linda Rian <[email protected]>", | ||
"repository": "ramoona/banks-db", | ||
|
@@ -22,8 +22,8 @@ | |
"eslint-plugin-jsx-a11y": "6.x.x", | ||
"eslint-plugin-react": "7.x.x", | ||
"fs-promise": "2.x.x", | ||
"jsonfile-promised": "0.x.x", | ||
"jest": "23.x.x" | ||
"jest": "23.x.x", | ||
"jsonfile-promised": "0.x.x" | ||
}, | ||
"scripts": { | ||
"test": "jest && node lint.js && eslint *.js", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,42 @@ | ||
const banksDB = require('.'); | ||
const findBank = require('.'); | ||
const banksDBCore = require('./core'); | ||
const ruBanksData = require('./banks/ru'); | ||
const type = require('./type'); | ||
|
||
const BanksDB = banksDBCore(ruBanksData); | ||
|
||
/* eslint-disable no-undef */ | ||
|
||
test('returns card type', () => expect(banksDB('5211784563802833').type).toBe('mastercard')); | ||
test('returns card type', () => expect(findBank('5211784563802833').type).toBe('mastercard')); | ||
|
||
test('finds bank by first 6 symbols', () => expect(banksDB('5211784563802833').name).toBe('alfabank')); | ||
test('finds bank by first 6 symbols', () => expect(findBank('5211784563802833').name).toBe('alfabank')); | ||
|
||
test('finds bank by first 5 symbols', () => expect(banksDB('4622384563802833').name).toBe('vtb24')); | ||
test('finds bank by first 5 symbols', () => expect(findBank('4622384563802833').name).toBe('vtb24')); | ||
|
||
test('returns false on unknown bank', () => expect(banksDB('4111111111111111').name).toBeUndefined()); | ||
test('returns false on unknown bank', () => expect(findBank('4111111111111111').name).toBeUndefined()); | ||
|
||
test('returns card type on unknown bank', () => expect(banksDB('4111111111111111').type).toBe('visa')); | ||
test('returns card type on unknown bank', () => expect(findBank('4111111111111111').type).toBe('visa')); | ||
|
||
test('finds bank by converted to string card number', () => expect(banksDB(4377734563802833).name).toBe('tinkoff')); | ||
test('finds bank by converted to string card number', () => expect(findBank(4377734563802833).name).toBe('tinkoff')); | ||
|
||
test('ignores non-digits symbols in card number', () => expect(banksDB('43-77-73-45-63-80-28-33').name).toBe('tinkoff')); | ||
test('ignores non-digits symbols in card number', () => expect(findBank('43-77-73-45-63-80-28-33').name).toBe('tinkoff')); | ||
|
||
test('ignores whitespaces', () => expect(banksDB('4627 3045 6380 2833').name).toBe('raiffeisen')); | ||
test('ignores whitespaces', () => expect(findBank('4627 3045 6380 2833').name).toBe('raiffeisen')); | ||
|
||
test('accepts undefined', () => expect(banksDB(undefined).name).toBeUndefined()); | ||
test('accepts undefined', () => expect(findBank(undefined).name).toBeUndefined()); | ||
|
||
test('returns card type', () => expect(type(4111111111111111)).toBe('visa')); | ||
|
||
test('returns undefined on unknown card type', () => expect(type(123456)).toBeUndefined()); | ||
|
||
test('returns all banks data', () => expect(Array.isArray(banksDB.data)).toBeTruthy()); | ||
test('returns country + bankname', () => expect(findBank('5211784563802833').code).toBe('ru-alfabank')); | ||
|
||
test('returns code from findBank.data', () => expect(typeof findBank.data[0].code).toBe('string')); | ||
|
||
test('returns all banks data', () => expect(Array.isArray(findBank.data)).toBeTruthy()); | ||
|
||
test('returns country + bankname', () => expect(banksDB('5211784563802833').code).toBe('ru-alfabank')); | ||
test('returns only specified banks', () => expect(BanksDB.data.length).toEqual(ruBanksData.length)); | ||
|
||
test('returns code from banksDB.data', () => expect(typeof banksDB.data[0].code).toBe('string')); | ||
test('finds bank in specified banks', () => expect(BanksDB.findBank('4622384563802833').name).toBe('vtb24')); | ||
|
||
/* eslint-enable no-undef */ |
Oops, something went wrong.