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

ES module for webpack tree shaking #1015

Merged
merged 13 commits into from
Nov 21, 2019
35 changes: 23 additions & 12 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
{
"presets": [
["@babel/preset-env", {"targets": {"node": "0.10"}}]
],
"plugins": [
[
"add-module-exports",
{
"addDefaultProperty": true
}
]
]
}
"env": {
"es": {
"presets": [
["@babel/preset-env", {
"modules": false
}]
]
},
"development": {
"presets": [
["@babel/preset-env", { "targets": { "node": "0.10" } }]
],
"plugins": [
[
"add-module-exports",
{
"addDefaultProperty": true
}
]
]
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Or, import only a subset of the library:
import isEmail from 'validator/lib/isEmail';
```

#### Tree-shakeable ES imports

```javascript
import isEmail from 'validator/es/lib/isEmail';
```

### Client-side usage

The library can be loaded either as a standalone script, or through an [AMD][amd]-compatible loader
Expand Down
156 changes: 156 additions & 0 deletions es/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import toDate from './lib/toDate';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are having to repeat all the code in the /es folder, it means changes have to be made in duplicate. Could we think of a build process where this code is generated into the folder and all the necessary transformations made? 🤔

Copy link
Contributor Author

@banjankri banjankri Apr 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done already, please see "build:es" script in package.json. It is then invoked as part of the "build" script. Added also cleanup for it accordingly, following what was done for "clean:browser" and "clean:node".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, got it 👍

import toFloat from './lib/toFloat';
import toInt from './lib/toInt';
import toBoolean from './lib/toBoolean';
import equals from './lib/equals';
import contains from './lib/contains';
import matches from './lib/matches';
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';
import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha';
import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric';
import isNumeric from './lib/isNumeric';
import isPort from './lib/isPort';
import isLowercase from './lib/isLowercase';
import isUppercase from './lib/isUppercase';
import isAscii from './lib/isAscii';
import isFullWidth from './lib/isFullWidth';
import isHalfWidth from './lib/isHalfWidth';
import isVariableWidth from './lib/isVariableWidth';
import isMultibyte from './lib/isMultibyte';
import isSurrogatePair from './lib/isSurrogatePair';
import isInt from './lib/isInt';
import isFloat, { locales as isFloatLocales } from './lib/isFloat';
import isDecimal from './lib/isDecimal';
import isHexadecimal from './lib/isHexadecimal';
import isDivisibleBy from './lib/isDivisibleBy';
import isHexColor from './lib/isHexColor';
import isISRC from './lib/isISRC';
import isMD5 from './lib/isMD5';
import isHash from './lib/isHash';
import isJWT from './lib/isJWT';
import isJSON from './lib/isJSON';
import isEmpty from './lib/isEmpty';
import isLength from './lib/isLength';
import isByteLength from './lib/isByteLength';
import isUUID from './lib/isUUID';
import isMongoId from './lib/isMongoId';
import isAfter from './lib/isAfter';
import isBefore from './lib/isBefore';
import isIn from './lib/isIn';
import isCreditCard from './lib/isCreditCard';
import isIdentityCard from './lib/isIdentityCard';
import isISIN from './lib/isISIN';
import isISBN from './lib/isISBN';
import isISSN from './lib/isISSN';
import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone';
import isCurrency from './lib/isCurrency';
import isISO8601 from './lib/isISO8601';
import isRFC3339 from './lib/isRFC3339';
import isISO31661Alpha2 from './lib/isISO31661Alpha2';
import isISO31661Alpha3 from './lib/isISO31661Alpha3';
import isBase64 from './lib/isBase64';
import isDataURI from './lib/isDataURI';
import isMagnetURI from './lib/isMagnetURI';
import isMimeType from './lib/isMimeType';
import isLatLong from './lib/isLatLong';
import isPostalCode, { locales as isPostalCodeLocales } from './lib/isPostalCode';
import ltrim from './lib/ltrim';
import rtrim from './lib/rtrim';
import trim from './lib/trim';
import escape from './lib/escape';
import unescape from './lib/unescape';
import stripLow from './lib/stripLow';
import whitelist from './lib/whitelist';
import blacklist from './lib/blacklist';
import isWhitelisted from './lib/isWhitelisted';
import normalizeEmail from './lib/normalizeEmail';
import toString from './lib/util/toString';
var version = '10.11.0';
var validator = {
version: version,
toDate: toDate,
toFloat: toFloat,
toInt: toInt,
toBoolean: toBoolean,
equals: equals,
contains: contains,
matches: matches,
isEmail: isEmail,
isURL: isURL,
isMACAddress: isMACAddress,
isIP: isIP,
isIPRange: isIPRange,
isFQDN: isFQDN,
isBoolean: isBoolean,
isAlpha: isAlpha,
isAlphaLocales: isAlphaLocales,
isAlphanumeric: isAlphanumeric,
isAlphanumericLocales: isAlphanumericLocales,
isNumeric: isNumeric,
isPort: isPort,
isLowercase: isLowercase,
isUppercase: isUppercase,
isAscii: isAscii,
isFullWidth: isFullWidth,
isHalfWidth: isHalfWidth,
isVariableWidth: isVariableWidth,
isMultibyte: isMultibyte,
isSurrogatePair: isSurrogatePair,
isInt: isInt,
isFloat: isFloat,
isFloatLocales: isFloatLocales,
isDecimal: isDecimal,
isHexadecimal: isHexadecimal,
isDivisibleBy: isDivisibleBy,
isHexColor: isHexColor,
isISRC: isISRC,
isMD5: isMD5,
isHash: isHash,
isJWT: isJWT,
isJSON: isJSON,
isEmpty: isEmpty,
isLength: isLength,
isByteLength: isByteLength,
isUUID: isUUID,
isMongoId: isMongoId,
isAfter: isAfter,
isBefore: isBefore,
isIn: isIn,
isCreditCard: isCreditCard,
isIdentityCard: isIdentityCard,
isISIN: isISIN,
isISBN: isISBN,
isISSN: isISSN,
isMobilePhone: isMobilePhone,
isMobilePhoneLocales: isMobilePhoneLocales,
isPostalCode: isPostalCode,
isPostalCodeLocales: isPostalCodeLocales,
isCurrency: isCurrency,
isISO8601: isISO8601,
isRFC3339: isRFC3339,
isISO31661Alpha2: isISO31661Alpha2,
isISO31661Alpha3: isISO31661Alpha3,
isBase64: isBase64,
isDataURI: isDataURI,
isMagnetURI: isMagnetURI,
isMimeType: isMimeType,
isLatLong: isLatLong,
ltrim: ltrim,
rtrim: rtrim,
trim: trim,
escape: escape,
unescape: unescape,
stripLow: stripLow,
whitelist: whitelist,
blacklist: blacklist,
isWhitelisted: isWhitelisted,
normalizeEmail: normalizeEmail,
toString: toString
};
export default validator;
96 changes: 96 additions & 0 deletions es/lib/alpha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
export var alpha = {
'en-US': /^[A-Z]+$/i,
'bg-BG': /^[А-Я]+$/i,
'cs-CZ': /^[A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,
'da-DK': /^[A-ZÆØÅ]+$/i,
'de-DE': /^[A-ZÄÖÜß]+$/i,
'el-GR': /^[Α-ω]+$/i,
'es-ES': /^[A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
'it-IT': /^[A-ZÀÉÈÌÎÓÒÙ]+$/i,
'nb-NO': /^[A-ZÆØÅ]+$/i,
'nl-NL': /^[A-ZÁÉËÏÓÖÜÚ]+$/i,
'nn-NO': /^[A-ZÆØÅ]+$/i,
'hu-HU': /^[A-ZÁÉÍÓÖŐÚÜŰ]+$/i,
'pl-PL': /^[A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
'pt-PT': /^[A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
'ru-RU': /^[А-ЯЁ]+$/i,
'sl-SI': /^[A-ZČĆĐŠŽ]+$/i,
'sk-SK': /^[A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
'sr-RS@latin': /^[A-ZČĆŽŠĐ]+$/i,
'sr-RS': /^[А-ЯЂЈЉЊЋЏ]+$/i,
'sv-SE': /^[A-ZÅÄÖ]+$/i,
'tr-TR': /^[A-ZÇĞİıÖŞÜ]+$/i,
'uk-UA': /^[А-ЩЬЮЯЄIЇҐі]+$/i,
'ku-IQ': /^[ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆھەیێيطؤثآإأكضصةظذ]+$/i,
ar: /^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/
};
export var alphanumeric = {
'en-US': /^[0-9A-Z]+$/i,
'bg-BG': /^[0-9А-Я]+$/i,
'cs-CZ': /^[0-9A-ZÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$/i,
'da-DK': /^[0-9A-ZÆØÅ]+$/i,
'de-DE': /^[0-9A-ZÄÖÜß]+$/i,
'el-GR': /^[0-9Α-ω]+$/i,
'es-ES': /^[0-9A-ZÁÉÍÑÓÚÜ]+$/i,
'fr-FR': /^[0-9A-ZÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$/i,
'it-IT': /^[0-9A-ZÀÉÈÌÎÓÒÙ]+$/i,
'hu-HU': /^[0-9A-ZÁÉÍÓÖŐÚÜŰ]+$/i,
'nb-NO': /^[0-9A-ZÆØÅ]+$/i,
'nl-NL': /^[0-9A-ZÁÉËÏÓÖÜÚ]+$/i,
'nn-NO': /^[0-9A-ZÆØÅ]+$/i,
'pl-PL': /^[0-9A-ZĄĆĘŚŁŃÓŻŹ]+$/i,
'pt-PT': /^[0-9A-ZÃÁÀÂÇÉÊÍÕÓÔÚÜ]+$/i,
'ru-RU': /^[0-9А-ЯЁ]+$/i,
'sl-SI': /^[0-9A-ZČĆĐŠŽ]+$/i,
'sk-SK': /^[0-9A-ZÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$/i,
'sr-RS@latin': /^[0-9A-ZČĆŽŠĐ]+$/i,
'sr-RS': /^[0-9А-ЯЂЈЉЊЋЏ]+$/i,
'sv-SE': /^[0-9A-ZÅÄÖ]+$/i,
'tr-TR': /^[0-9A-ZÇĞİıÖŞÜ]+$/i,
'uk-UA': /^[0-9А-ЩЬЮЯЄIЇҐі]+$/i,
'ku-IQ': /^[٠١٢٣٤٥٦٧٨٩0-9ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆھەیێيطؤثآإأكضصةظذ]+$/i,
ar: /^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$/
};
export var decimal = {
'en-US': '.',
ar: '٫'
};
export var englishLocales = ['AU', 'GB', 'HK', 'IN', 'NZ', 'ZA', 'ZM'];

for (var locale, i = 0; i < englishLocales.length; i++) {
locale = "en-".concat(englishLocales[i]);
alpha[locale] = alpha['en-US'];
alphanumeric[locale] = alphanumeric['en-US'];
decimal[locale] = decimal['en-US'];
} // Source: http://www.localeplanet.com/java/


export var arabicLocales = ['AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY', 'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE'];

for (var _locale, _i = 0; _i < arabicLocales.length; _i++) {
_locale = "ar-".concat(arabicLocales[_i]);
alpha[_locale] = alpha.ar;
alphanumeric[_locale] = alphanumeric.ar;
decimal[_locale] = decimal.ar;
} // Source: https://en.wikipedia.org/wiki/Decimal_mark


export var dotDecimal = [];
export var commaDecimal = ['bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'es-ES', 'fr-FR', 'it-IT', 'ku-IQ', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA'];

for (var _i2 = 0; _i2 < dotDecimal.length; _i2++) {
decimal[dotDecimal[_i2]] = decimal['en-US'];
}

for (var _i3 = 0; _i3 < commaDecimal.length; _i3++) {
decimal[commaDecimal[_i3]] = ',';
}

alpha['pt-BR'] = alpha['pt-PT'];
alphanumeric['pt-BR'] = alphanumeric['pt-PT'];
decimal['pt-BR'] = decimal['pt-PT']; // see #862

alpha['pl-Pl'] = alpha['pl-PL'];
alphanumeric['pl-Pl'] = alphanumeric['pl-PL'];
decimal['pl-Pl'] = decimal['pl-PL'];
5 changes: 5 additions & 0 deletions es/lib/blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assertString from './util/assertString';
export default function blacklist(str, chars) {
assertString(str);
return str.replace(new RegExp("[".concat(chars, "]+"), 'g'), '');
}
6 changes: 6 additions & 0 deletions es/lib/contains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import assertString from './util/assertString';
import toString from './util/toString';
export default function contains(str, elem) {
assertString(str);
return str.indexOf(toString(elem)) >= 0;
}
5 changes: 5 additions & 0 deletions es/lib/equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assertString from './util/assertString';
export default function equals(str, comparison) {
assertString(str);
return str === comparison;
}
5 changes: 5 additions & 0 deletions es/lib/escape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import assertString from './util/assertString';
export default function escape(str) {
assertString(str);
return str.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\//g, '&#x2F;').replace(/\\/g, '&#x5C;').replace(/`/g, '&#96;');
}
9 changes: 9 additions & 0 deletions es/lib/isAfter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import assertString from './util/assertString';
import toDate from './toDate';
export default function isAfter(str) {
var date = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : String(new Date());
assertString(str);
var comparison = toDate(date);
var original = toDate(str);
return !!(original && comparison && original > comparison);
}
13 changes: 13 additions & 0 deletions es/lib/isAlpha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assertString from './util/assertString';
import { alpha } from './alpha';
export default function isAlpha(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
assertString(str);

if (locale in alpha) {
return alpha[locale].test(str);
}

throw new Error("Invalid locale '".concat(locale, "'"));
}
export var locales = Object.keys(alpha);
13 changes: 13 additions & 0 deletions es/lib/isAlphanumeric.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assertString from './util/assertString';
import { alphanumeric } from './alpha';
export default function isAlphanumeric(str) {
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
assertString(str);

if (locale in alphanumeric) {
return alphanumeric[locale].test(str);
}

throw new Error("Invalid locale '".concat(locale, "'"));
}
export var locales = Object.keys(alphanumeric);
10 changes: 10 additions & 0 deletions es/lib/isAscii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assertString from './util/assertString';
/* eslint-disable no-control-regex */

var ascii = /^[\x00-\x7F]+$/;
/* eslint-enable no-control-regex */

export default function isAscii(str) {
assertString(str);
return ascii.test(str);
}
Loading