Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore clean 1034 #1085

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Validator | Description
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**isIBAN(str, locale)** | check if the string is a valid IBAN number,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IL', 'IN', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MX', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match with the first twi caracters of the IBAN number (wich corresponds to the ISO 3166-1 alpha-2 country code). Locale list is `validator.isIBANLocales`).
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW', 'he-IL']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
**isIPRange(str)** | check if the string is an IP Range(version 4 only).
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ var _isBefore = _interopRequireDefault(require("./lib/isBefore"));

var _isIn = _interopRequireDefault(require("./lib/isIn"));

var _isIBAN = _interopRequireWildcard(require("./lib/isIBAN"));

var _isCreditCard = _interopRequireDefault(require("./lib/isCreditCard"));

var _isIdentityCard = _interopRequireDefault(require("./lib/isIdentityCard"));
Expand Down Expand Up @@ -209,6 +211,8 @@ var validator = {
isAfter: _isAfter.default,
isBefore: _isBefore.default,
isIn: _isIn.default,
isIBAN: _isIBAN.default,
isIBANLocales: _isIBAN.locales,
isCreditCard: _isCreditCard.default,
isIdentityCard: _isIdentityCard.default,
isISIN: _isISIN.default,
Expand Down
128 changes: 128 additions & 0 deletions lib/isIBAN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isIBAN;
exports.locales = void 0;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable max-len */
var iban = {
AD: /^(AD[0-9]{2})\d{8}[0-9A-Z]{12}$/,
AL: /^(AL[0-9]{2})\d{8}[A-Z0-9]{16}$/,
AT: /^(AT[0-9]{2})\d{16}$/,
BA: /^(BA[0-9]{2})\d{16}$/,
BE: /^(BE[0-9]{2})\d{12}$/,
BG: /^(BG[0-9]{2})[A-Z]{4}\d{4}[A-Z0-9]{10}$/,
BH: /^(BH[0-9]{2})[A-Z]{4}[A-Z0-9]{14}$/,
BR: /^(BR[0-9]{2})\d{23}[A-Z][A-Z0-9]$/,
CH: /^(CH[0-9]{2})\d{17}$/,
CY: /^(CY[0-9]{2})\d{8}[A-Z0-9]{16}$/,
CZ: /^(CZ[0-9]{2})\d{20}$/,
DE: /^(DE[0-9]{2})\d{18}$/,
DK: /^(DK[0-9]{2})\d{14}$/,
EE: /^(EE[0-9]{2})\d{16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S: /^(PS[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/,
PT: /^(PT[0-9]{2})\d{21}$/,
QA: /^(QA[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/,
RO: /^(RO[0-9]{2})[A-Z]{4}[A-Z0-9]{16}$/,
SA: /^(SA[0-9]{2})\d{2}[A-Z0-9]{18}$/,
SE: /^(SE[0-9]{2})\d{20}$/,
SI: /^(SI[0-9]{2})\d{15}$/,
SK: /^(SK[0-9]{2})\d{20}$/,
SM: /^(SM[0-9]{2})[A-Z]{1}\d{10}[A-Z0-9]{12}$/,
RS: /^(RS[0-9]{2})\d{18}$/,
TN: /^(TN[0-9]{2})\d{20}$/,
TR: /^(TR[0-9]{2})\d{5}[A-Z0-9]{17}$/,
UA: /^(UA[0-9]{2})\d{6}[A-Z0-9]{19}$/,
XK: /^(XK[0-9]{2})\d{16}$/
};
/* eslint-enable max-len */

/**
* @function IbanCalcul - calculate if the IBAN corresponds to the reglementation: converting
* it into an integer and performing a basic mod-97 operation. If the IBAN is valid, the remainder
* equals 1.
* 1- Check that the total IBAN length is correct as per the country. If not, the IBAN is invalid
* 2- Move the four initial characters to the end of the string
* 3- Replace each letter in the string with two digits, thereby expanding the string,
* where A = 10..., Z = 35
* 4- Interpret the string as a decimal integer and compute the remainder of that number on
* division by 97
* @param {e} - recieves the IBAN tested
* @warns IBAN's length differs from country to another, the maximum length is 34 characters so
* calculating its modulus 97 is not possible for all machines.To solve this, the function calculate
* the modulus of the first 9 digit by 97 and add the result to the beginning of the remain IBAN
* number. The process is repeated untill the result's length is less than 10
*/

var IbanCalcul = function IbanCalcul(e) {
var countryCode = e.slice(0, 4);
var result = e.slice(4, e.length) + countryCode;

for (var counter = 1; counter <= Math.ceil(e.length / 10) + 1; counter++) {
var remainder = result.slice(0, 10).replace(/[A-Z]/g, function (element) {
return element.charCodeAt() - 55;
}) % 97;
result = remainder + result.slice(10);
}

if (result === '1') return true;
return false;
};

function isIBAN(str, locale) {
(0, _assertString.default)(str);
str = str.toUpperCase();

if (locale in iban) {
return iban[locale].test(str) && IbanCalcul(str);
} else if (!locale || locale === 'any') {
locale = str.slice(0, 2);

if (locale in iban) {
return iban[locale].test(str) && IbanCalcul(str);
}

return false;
}

throw new Error("Invalid locale '".concat(locale, "'"));
}

var locales = Object.keys(iban);
exports.locales = locales;
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import isBefore from './lib/isBefore';

import isIn from './lib/isIn';

import isIBAN, { locales as isIBANLocales } from './lib/isIBAN';

import isCreditCard from './lib/isCreditCard';
import isIdentityCard from './lib/isIdentityCard';

Expand Down Expand Up @@ -150,6 +152,8 @@ const validator = {
isAfter,
isBefore,
isIn,
isIBAN,
isIBANLocales,
isCreditCard,
isIdentityCard,
isISIN,
Expand Down
109 changes: 109 additions & 0 deletions src/lib/isIBAN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import assertString from './util/assertString';

/* eslint-disable max-len */
const iban = {
AD: /^(AD[0-9]{2})\d{8}[0-9A-Z]{12}$/,
AL: /^(AL[0-9]{2})\d{8}[A-Z0-9]{16}$/,
AT: /^(AT[0-9]{2})\d{16}$/,
BA: /^(BA[0-9]{2})\d{16}$/,
BE: /^(BE[0-9]{2})\d{12}$/,
BG: /^(BG[0-9]{2})[A-Z]{4}\d{4}[A-Z0-9]{10}$/,
BH: /^(BH[0-9]{2})[A-Z]{4}[A-Z0-9]{14}$/,
BR: /^(BR[0-9]{2})\d{23}[A-Z][A-Z0-9]$/,
CH: /^(CH[0-9]{2})\d{17}$/,
CY: /^(CY[0-9]{2})\d{8}[A-Z0-9]{16}$/,
CZ: /^(CZ[0-9]{2})\d{20}$/,
DE: /^(DE[0-9]{2})\d{18}$/,
DK: /^(DK[0-9]{2})\d{14}$/,
EE: /^(EE[0-9]{2})\d{16}$/,
ES: /^(ES[0-9]{2})\d{20}$/,
FO: /^(FO[0-9]{2})\d{14}$/,
FI: /^(FI[0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QA: /^(QA[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/,
RO: /^(RO[0-9]{2})[A-Z]{4}[A-Z0-9]{16}$/,
SA: /^(SA[0-9]{2})\d{2}[A-Z0-9]{18}$/,
SE: /^(SE[0-9]{2})\d{20}$/,
SI: /^(SI[0-9]{2})\d{15}$/,
SK: /^(SK[0-9]{2})\d{20}$/,
SM: /^(SM[0-9]{2})[A-Z]{1}\d{10}[A-Z0-9]{12}$/,
RS: /^(RS[0-9]{2})\d{18}$/,
TN: /^(TN[0-9]{2})\d{20}$/,
TR: /^(TR[0-9]{2})\d{5}[A-Z0-9]{17}$/,
UA: /^(UA[0-9]{2})\d{6}[A-Z0-9]{19}$/,
XK: /^(XK[0-9]{2})\d{16}$/,
};
/* eslint-enable max-len */

/**
* @function IbanCalcul - calculate if the IBAN corresponds to the reglementation: converting
* it into an integer and performing a basic mod-97 operation. If the IBAN is valid, the remainder
* equals 1.
* 1- Check that the total IBAN length is correct as per the country. If not, the IBAN is invalid
* 2- Move the four initial characters to the end of the string
* 3- Replace each letter in the string with two digits, thereby expanding the string,
* where A = 10..., Z = 35
* 4- Interpret the string as a decimal integer and compute the remainder of that number on
* division by 97
* @param {e} - recieves the IBAN tested
* @warns IBAN's length differs from country to another, the maximum length is 34 characters so
* calculating its modulus 97 is not possible for all machines.To solve this, the function calculate
* the modulus of the first 9 digit by 97 and add the result to the beginning of the remain IBAN
* number. The process is repeated untill the result's length is less than 10
*/
const IbanCalcul = (e) => {
const countryCode = e.slice(0, 4);
let result = e.slice(4, e.length) + countryCode;
for (let counter = 1; counter <= Math.ceil(e.length / 10) + 1; counter++) {
let remainder =
result.slice(0, 10).replace(/[A-Z]/g, element => element.charCodeAt() - 55) % 97;
result = remainder + result.slice(10);
}
if (result === '1') return true;
return false;
};

export default function isIBAN(str, locale) {
assertString(str);
str = str.toUpperCase();
if (locale in iban) {
return iban[locale].test(str) && IbanCalcul(str);
} else if (!locale || locale === 'any') {
locale = str.slice(0, 2);
if (locale in iban) {
return iban[locale].test(str) && IbanCalcul(str);
}
return false;
}
throw new Error(`Invalid locale '${locale}'`);
}

export const locales = Object.keys(iban);
Loading