Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/chriso/validator.js
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/chriso/validator.js:
  Additional stuff for validatorjs#858
  Fixed semicolon issue
  Fixed stuff using eslint
  Created includes function for array to cater older browsers
  Update the changelog and bundle
  Iraq locale added
  10.4.0
  Update the changelog and min version
  Squased commits, made changes to isIpRange to avoid array destructuring.
  Update the changelog
  chore: add tests and update readme
  Squashing the many disparit commits into a single commit.
  Bump
  Update the changelog and min version
  10.3.0
  Support German numbers without a separator after country code
  Code updated with test cases
  Adde  Kuwait Number  Regex for Mobile Number Validation
  Accepting array of locales for mobile phone validation

# Conflicts:
#	validator.min.js
  • Loading branch information
onpeak-tdevos committed Jul 6, 2018
2 parents 7e2f459 + 38255de commit 4609cca
Show file tree
Hide file tree
Showing 22 changed files with 289 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
node_modules
coverage
package-lock.json
yarn.lock
yarn.lock
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#### HEAD

- New locale
([#856](https://github.com/chriso/validator.js/pull/856))

#### 10.4.0

- Added an `isIPRange()` validator
([#842](https://github.com/chriso/validator.js/pull/842))
- Accept an array of locales in `isMobilePhone()`
([#742](https://github.com/chriso/validator.js/pull/742))
- New locale
([#843](https://github.com/chriso/validator.js/pull/843))

#### 10.3.0

- Strict Gmail validation in `isEmail()`
([#832](https://github.com/chriso/validator.js/pull/832))
- New locales
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Chris O'Hara <cohara87@gmail.com>
Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Validator | Description
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**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).
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).<br/><br/>`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
Expand All @@ -102,7 +103,7 @@ Validator | Description
**isMACAddress(str)** | check if the string is a MAC address.
**isMD5(str)** | check if the string is a MD5 hash.
**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format
**isMobilePhone(str, locale [, options])** | check if the string is a mobile phone number,<br/><br/>(locale is one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-JO', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMobilePhone(str, locale [, options])** | check if the string is a mobile phone number,<br/><br/>(locale is either an array of locales (e.g `['sk-SK', 'sr-RS']`) OR one of `['ar-AE', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', 'bg-BG', 'cs-CZ', 'de-DE', 'da-DK', 'el-GR', 'en-AU', 'en-CA', 'en-GB', 'en-HK', 'en-IN', 'en-KE', 'en-NG', 'en-NZ', 'en-RW', 'en-SG', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-PK', 'es-ES', 'et-EE', 'fa-IR', 'fi-FI', 'fr-FR', 'he-IL', 'hu-HU', 'it-IT', 'ja-JP', 'kk-KZ', 'ko-KR', 'lt-LT', 'ms-MY', 'nb-NO', 'nn-NO', 'pl-PL', 'pt-PT', 'pt-BR', 'ro-RO', 'ru-RU', 'sk-SK', 'sr-RS', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-TW']` OR 'any'. If 'any' is used, function will check if any of the locales match).<br/><br/>`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`.
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
**isNumeric(str)** | check if the string contains only numbers.
Expand Down Expand Up @@ -174,7 +175,7 @@ Remember, validating can be troublesome sometimes. See [A list of articles about
## License (MIT)

```
Copyright (c) 2017 Chris O'Hara <cohara87@gmail.com>
Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var _isIP = require('./lib/isIP');

var _isIP2 = _interopRequireDefault(_isIP);

var _isIPRange = require('./lib/isIPRange');

var _isIPRange2 = _interopRequireDefault(_isIPRange);

var _isFQDN = require('./lib/isFQDN');

var _isFQDN2 = _interopRequireDefault(_isFQDN);
Expand Down Expand Up @@ -282,7 +286,7 @@ var _toString2 = _interopRequireDefault(_toString);

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

var version = '10.2.0';
var version = '10.4.0';

var validator = {
version: version,
Expand All @@ -297,6 +301,7 @@ var validator = {
isURL: _isURL2.default,
isMACAddress: _isMACAddress2.default,
isIP: _isIP2.default,
isIPRange: _isIPRange2.default,
isFQDN: _isFQDN2.default,
isBoolean: _isBoolean2.default,
isAlpha: _isAlpha2.default,
Expand Down
6 changes: 5 additions & 1 deletion lib/isDecimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

var _includes = require('./util/includes');

var _includes2 = _interopRequireDefault(_includes);

var _alpha = require('./alpha');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand All @@ -34,7 +38,7 @@ function isDecimal(str, options) {
(0, _assertString2.default)(str);
options = (0, _merge2.default)(options, default_decimal_options);
if (options.locale in _alpha.decimal) {
return !blacklist.includes(str.replace(/ /g, '')) && decimalRegExp(options).test(str);
return !(0, _includes2.default)(blacklist, str.replace(/ /g, '')) && decimalRegExp(options).test(str);
}
throw new Error('Invalid locale \'' + options.locale + '\'');
}
Expand Down
40 changes: 40 additions & 0 deletions lib/isIPRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isIPRange;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

var _isIP = require('./isIP');

var _isIP2 = _interopRequireDefault(_isIP);

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

var subnetMaybe = /^\d{1,2}$/;

function isIPRange(str) {
(0, _assertString2.default)(str);
var parts = str.split('/');

// parts[0] -> ip, parts[1] -> subnet
if (parts.length !== 2) {
return false;
}

if (!subnetMaybe.test(parts[1])) {
return false;
}

// Disallow preceding 0 i.e. 01, 02, ...
if (parts[1].length > 1 && parts[1].startsWith('0')) {
return false;
}

return (0, _isIP2.default)(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
}
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion lib/isISO31661Alpha2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

var _includes = require('./util/includes');

var _includes2 = _interopRequireDefault(_includes);

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

// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
var validISO31661Alpha2CountriesCodes = ['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW'];

function isISO31661Alpha2(str) {
(0, _assertString2.default)(str);
return validISO31661Alpha2CountriesCodes.includes(str.toUpperCase());
return (0, _includes2.default)(validISO31661Alpha2CountriesCodes, str.toUpperCase());
}
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion lib/isISO31661Alpha3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

var _includes = require('./util/includes');

var _includes2 = _interopRequireDefault(_includes);

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

// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
var validISO31661Alpha3CountriesCodes = ['AFG', 'ALA', 'ALB', 'DZA', 'ASM', 'AND', 'AGO', 'AIA', 'ATA', 'ATG', 'ARG', 'ARM', 'ABW', 'AUS', 'AUT', 'AZE', 'BHS', 'BHR', 'BGD', 'BRB', 'BLR', 'BEL', 'BLZ', 'BEN', 'BMU', 'BTN', 'BOL', 'BES', 'BIH', 'BWA', 'BVT', 'BRA', 'IOT', 'BRN', 'BGR', 'BFA', 'BDI', 'KHM', 'CMR', 'CAN', 'CPV', 'CYM', 'CAF', 'TCD', 'CHL', 'CHN', 'CXR', 'CCK', 'COL', 'COM', 'COG', 'COD', 'COK', 'CRI', 'CIV', 'HRV', 'CUB', 'CUW', 'CYP', 'CZE', 'DNK', 'DJI', 'DMA', 'DOM', 'ECU', 'EGY', 'SLV', 'GNQ', 'ERI', 'EST', 'ETH', 'FLK', 'FRO', 'FJI', 'FIN', 'FRA', 'GUF', 'PYF', 'ATF', 'GAB', 'GMB', 'GEO', 'DEU', 'GHA', 'GIB', 'GRC', 'GRL', 'GRD', 'GLP', 'GUM', 'GTM', 'GGY', 'GIN', 'GNB', 'GUY', 'HTI', 'HMD', 'VAT', 'HND', 'HKG', 'HUN', 'ISL', 'IND', 'IDN', 'IRN', 'IRQ', 'IRL', 'IMN', 'ISR', 'ITA', 'JAM', 'JPN', 'JEY', 'JOR', 'KAZ', 'KEN', 'KIR', 'PRK', 'KOR', 'KWT', 'KGZ', 'LAO', 'LVA', 'LBN', 'LSO', 'LBR', 'LBY', 'LIE', 'LTU', 'LUX', 'MAC', 'MKD', 'MDG', 'MWI', 'MYS', 'MDV', 'MLI', 'MLT', 'MHL', 'MTQ', 'MRT', 'MUS', 'MYT', 'MEX', 'FSM', 'MDA', 'MCO', 'MNG', 'MNE', 'MSR', 'MAR', 'MOZ', 'MMR', 'NAM', 'NRU', 'NPL', 'NLD', 'NCL', 'NZL', 'NIC', 'NER', 'NGA', 'NIU', 'NFK', 'MNP', 'NOR', 'OMN', 'PAK', 'PLW', 'PSE', 'PAN', 'PNG', 'PRY', 'PER', 'PHL', 'PCN', 'POL', 'PRT', 'PRI', 'QAT', 'REU', 'ROU', 'RUS', 'RWA', 'BLM', 'SHN', 'KNA', 'LCA', 'MAF', 'SPM', 'VCT', 'WSM', 'SMR', 'STP', 'SAU', 'SEN', 'SRB', 'SYC', 'SLE', 'SGP', 'SXM', 'SVK', 'SVN', 'SLB', 'SOM', 'ZAF', 'SGS', 'SSD', 'ESP', 'LKA', 'SDN', 'SUR', 'SJM', 'SWZ', 'SWE', 'CHE', 'SYR', 'TWN', 'TJK', 'TZA', 'THA', 'TLS', 'TGO', 'TKL', 'TON', 'TTO', 'TUN', 'TUR', 'TKM', 'TCA', 'TUV', 'UGA', 'UKR', 'ARE', 'GBR', 'USA', 'UMI', 'URY', 'UZB', 'VUT', 'VEN', 'VNM', 'VGB', 'VIR', 'WLF', 'ESH', 'YEM', 'ZMB', 'ZWE'];

function isISO31661Alpha3(str) {
(0, _assertString2.default)(str);
return validISO31661Alpha3CountriesCodes.includes(str.toUpperCase());
return (0, _includes2.default)(validISO31661Alpha3CountriesCodes, str.toUpperCase());
}
module.exports = exports['default'];
16 changes: 14 additions & 2 deletions lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ var phones = {
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/,
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
'ar-EG': /^((\+?20)|0)?1[012]\d{8}$/,
'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/,
'ar-JO': /^(\+?962|0)?7[789]\d{7}$/,
'ar-KW': /^(\+?965)[569]\d{7}$/,
'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/,
'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/,
'ar-TN': /^(\+?216)?[2459]\d{7}$/,
'be-BY': /^(\+?375)?(24|25|29|33|44)\d{7}$/,
'bg-BG': /^(\+?359|0)?8[789]\d{7}$/,
'cs-CZ': /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/,
'da-DK': /^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$/,
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
'de-DE': /^(\+?49[ \.\-]?)?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
'el-GR': /^(\+?30|0)?(69\d{8})$/,
'en-AU': /^(\+?61|0)4\d{8}$/,
'en-GB': /^(\+?44|0)7\d{9}$/,
Expand Down Expand Up @@ -87,7 +89,17 @@ function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some(function (key) {
if (phones.hasOwnProperty(key)) {
var phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (var key in phones) {
Expand Down
13 changes: 13 additions & 0 deletions lib/util/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
var includes = function includes(arr, val) {
return arr.some(function (arrVal) {
return val === arrVal;
});
};

exports.default = includes;
module.exports = exports["default"];
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "validator",
"description": "String validation and sanitization",
"version": "10.2.0",
"version": "10.4.0",
"homepage": "http://github.com/chriso/validator.js",
"files": [
"index.js",
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import isEmail from './lib/isEmail';
import isURL from './lib/isURL';
import isMACAddress from './lib/isMACAddress';
import isIP from './lib/isIP';
import isIPRange from './lib/isIPRange';
import isFQDN from './lib/isFQDN';

import isBoolean from './lib/isBoolean';
Expand Down Expand Up @@ -91,7 +92,7 @@ import normalizeEmail from './lib/normalizeEmail';

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

const version = '10.2.0';
const version = '10.4.0';

const validator = {
version,
Expand All @@ -106,6 +107,7 @@ const validator = {
isURL,
isMACAddress,
isIP,
isIPRange,
isFQDN,
isBoolean,
isAlpha,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/isDecimal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import merge from './util/merge';
import assertString from './util/assertString';
import includes from './util/includes';
import { decimal } from './alpha';

function decimalRegExp(options) {
Expand All @@ -19,7 +20,7 @@ export default function isDecimal(str, options) {
assertString(str);
options = merge(options, default_decimal_options);
if (options.locale in decimal) {
return !blacklist.includes(str.replace(/ /g, '')) && decimalRegExp(options).test(str);
return !includes(blacklist, str.replace(/ /g, '')) && decimalRegExp(options).test(str);
}
throw new Error(`Invalid locale '${options.locale}'`);
}
25 changes: 25 additions & 0 deletions src/lib/isIPRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assertString from './util/assertString';
import isIP from './isIP';

const subnetMaybe = /^\d{1,2}$/;

export default function isIPRange(str) {
assertString(str);
const parts = str.split('/');

// parts[0] -> ip, parts[1] -> subnet
if (parts.length !== 2) {
return false;
}

if (!subnetMaybe.test(parts[1])) {
return false;
}

// Disallow preceding 0 i.e. 01, 02, ...
if (parts[1].length > 1 && parts[1].startsWith('0')) {
return false;
}

return isIP(parts[0], 4) && parts[1] <= 32 && parts[1] >= 0;
}
3 changes: 2 additions & 1 deletion src/lib/isISO31661Alpha2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import includes from './util/includes';

// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
const validISO31661Alpha2CountriesCodes = [
Expand Down Expand Up @@ -31,5 +32,5 @@ const validISO31661Alpha2CountriesCodes = [

export default function isISO31661Alpha2(str) {
assertString(str);
return validISO31661Alpha2CountriesCodes.includes(str.toUpperCase());
return includes(validISO31661Alpha2CountriesCodes, str.toUpperCase());
}
3 changes: 2 additions & 1 deletion src/lib/isISO31661Alpha3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import includes from './util/includes';

// from https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
const validISO31661Alpha3CountriesCodes = [
Expand All @@ -22,5 +23,5 @@ const validISO31661Alpha3CountriesCodes = [

export default function isISO31661Alpha3(str) {
assertString(str);
return validISO31661Alpha3CountriesCodes.includes(str.toUpperCase());
return includes(validISO31661Alpha3CountriesCodes, str.toUpperCase());
}
16 changes: 14 additions & 2 deletions src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ const phones = {
'ar-AE': /^((\+?971)|0)?5[024568]\d{7}$/,
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
'ar-EG': /^((\+?20)|0)?1[012]\d{8}$/,
'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/,
'ar-JO': /^(\+?962|0)?7[789]\d{7}$/,
'ar-KW': /^(\+?965)[569]\d{7}$/,
'ar-SA': /^(!?(\+?966)|0)?5\d{8}$/,
'ar-SY': /^(!?(\+?963)|0)?9\d{8}$/,
'ar-TN': /^(\+?216)?[2459]\d{7}$/,
'be-BY': /^(\+?375)?(24|25|29|33|44)\d{7}$/,
'bg-BG': /^(\+?359|0)?8[789]\d{7}$/,
'cs-CZ': /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/,
'da-DK': /^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$/,
'de-DE': /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
'de-DE': /^(\+?49[ \.\-]?)?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
'el-GR': /^(\+?30|0)?(69\d{8})$/,
'en-AU': /^(\+?61|0)4\d{8}$/,
'en-GB': /^(\+?44|0)7\d{9}$/,
Expand Down Expand Up @@ -76,7 +78,17 @@ export default function isMobilePhone(str, locale, options) {
if (options && options.strictMode && !str.startsWith('+')) {
return false;
}
if (locale in phones) {
if (Array.isArray(locale)) {
return locale.some((key) => {
if (phones.hasOwnProperty(key)) {
const phone = phones[key];
if (phone.test(str)) {
return true;
}
}
return false;
});
} else if (locale in phones) {
return phones[locale].test(str);
} else if (locale === 'any') {
for (const key in phones) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/util/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const includes = (arr, val) => arr.some(arrVal => val === arrVal);

export default includes;
Loading

0 comments on commit 4609cca

Please sign in to comment.