Skip to content

Commit

Permalink
fix(PR 1034): clean up PR 1034 issues
Browse files Browse the repository at this point in the history
- fix linter issues
- fix tests
- seperate unnecessary files changes
- closes validatorjs#1034 validatorjs#951
- Other references validatorjs#1071
  • Loading branch information
ezkemboi committed Aug 11, 2019
2 parents 98b2ca6 + 9b17f26 commit 9d24518
Show file tree
Hide file tree
Showing 8 changed files with 1,372 additions and 1,744 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -100,6 +100,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
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -151,6 +151,10 @@ var _isWhitelisted = _interopRequireDefault(require("./lib/isWhitelisted"));

var _normalizeEmail = _interopRequireDefault(require("./lib/normalizeEmail"));

var _toString = _interopRequireDefault(require("./lib/util/toString"));

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand Down Expand Up @@ -236,7 +240,9 @@ var validator = {
blacklist: _blacklist.default,
isWhitelisted: _isWhitelisted.default,
normalizeEmail: _normalizeEmail.default,
toString: toString
toString: _toString.default,
isIBAN: _isIBAN.default,
isIBANLocales: _isIBAN.locales
};
var _default = validator;
exports.default = _default;
Expand Down
128 changes: 128 additions & 0 deletions lib/isIBAN.js
@@ -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})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z0-9]{12}$/,
MT: /^(MT[0-9]{2})[A-Z]{4}\d{5}[A-Z0-9]{18}$/,
NL: /^(NL[0-9]{2})[A-Z]{4}\d{10}$/,
NO: /^(NO[0-9]{2})\d{11}$/,
PL: /^(PL[0-9]{2})\d{24}$/,
PS: /^(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;
7 changes: 7 additions & 0 deletions src/index.js
Expand Up @@ -95,8 +95,13 @@ import isWhitelisted from './lib/isWhitelisted';

import normalizeEmail from './lib/normalizeEmail';

import toString from './lib/util/toString';

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

const version = '11.1.0';


const validator = {
version,
toDate,
Expand Down Expand Up @@ -178,6 +183,8 @@ const validator = {
isWhitelisted,
normalizeEmail,
toString,
isIBAN,
isIBANLocales,
};

export default validator;
109 changes: 109 additions & 0 deletions src/lib/isIBAN.js
@@ -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: /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-9]{2})\d{11}$/,
PL: /^(PL[0-9]{2})\d{24}$/,
PS: /^(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
*/
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);

0 comments on commit 9d24518

Please sign in to comment.