Skip to content

Commit deb1d1e

Browse files
varsubhamtux-tn
andauthored
feat(isEAN, isAlphanumeric): add support for EAN-14 and sAlphanumeric update (#1577)
* added support for EAN-14 along with test case * feat(isAlphanumeric): added options(optional parameter) * Delete validator.min.js * Delete validator.js Co-authored-by: Sarhan Aissi <[email protected]>
1 parent 022b100 commit deb1d1e

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Validator | Description
8585
**equals(str, comparison)** | check if the string matches the comparison.
8686
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
8787
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
88-
**isAlphanumeric(str [, locale])** | check if the string contains only letters and numbers.<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`.
88+
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>Locale is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fr-CA', 'fr-FR', 'he', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
8989
**isAscii(str)** | check if the string contains ASCII chars only.
9090
**isBase32(str)** | check if a string is base32 encoded.
9191
**isBase58(str)** | check if a string is base58 encoded.

src/lib/isAlphanumeric.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
import assertString from './util/assertString';
22
import { alphanumeric } from './alpha';
33

4-
export default function isAlphanumeric(str, locale = 'en-US') {
5-
assertString(str);
4+
export default function isAlphanumeric(_str, locale = 'en-US', options = {}) {
5+
assertString(_str);
6+
7+
let str = _str;
8+
const { ignore } = options;
9+
10+
if (ignore) {
11+
if (ignore instanceof RegExp) {
12+
str = str.replace(ignore, '');
13+
} else if (typeof ignore === 'string') {
14+
str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
15+
} else {
16+
throw new Error('ignore should be instance of a String or RegExp');
17+
}
18+
}
19+
620
if (locale in alphanumeric) {
721
return alphanumeric[locale].test(str);
822
}

src/lib/isEAN.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
* the thirteen-digit EAN-13, while the
44
* less commonly used 8-digit EAN-8 barcode was
55
* introduced for use on small packages.
6+
* Also EAN/UCC-14 is used for Grouping of individual
7+
* trade items above unit level(Intermediate, Carton or Pallet).
8+
* For more info about EAN-14 checkout: https://www.gtin.info/itf-14-barcodes/
69
* EAN consists of:
710
* GS1 prefix, manufacturer code, product code and check digit
811
* Reference: https://en.wikipedia.org/wiki/International_Article_Number
12+
* Reference: https://www.gtin.info/
913
*/
1014

1115
import assertString from './util/assertString';
1216

1317
/**
14-
* Define EAN Lenghts; 8 for EAN-8; 13 for EAN-13
15-
* and Regular Expression for valid EANs (EAN-8, EAN-13),
16-
* with exact numberic matching of 8 or 13 digits [0-9]
18+
* Define EAN Lenghts; 8 for EAN-8; 13 for EAN-13; 14 for EAN-14
19+
* and Regular Expression for valid EANs (EAN-8, EAN-13, EAN-14),
20+
* with exact numberic matching of 8 or 13 or 14 digits [0-9]
1721
*/
1822
const LENGTH_EAN_8 = 8;
19-
const validEanRegex = /^(\d{8}|\d{13})$/;
23+
const LENGTH_EAN_14 = 14;
24+
const validEanRegex = /^(\d{8}|\d{13}|\d{14})$/;
2025

2126

2227
/**
@@ -28,7 +33,7 @@ const validEanRegex = /^(\d{8}|\d{13})$/;
2833
* @return {number}
2934
*/
3035
function getPositionWeightThroughLengthAndIndex(length, index) {
31-
if (length === LENGTH_EAN_8) {
36+
if (length === LENGTH_EAN_8 || length === LENGTH_EAN_14) {
3237
return (index % 2 === 0) ? 3 : 1;
3338
}
3439

@@ -56,7 +61,7 @@ function calculateCheckDigit(ean) {
5661

5762
/**
5863
* Check if string is valid EAN:
59-
* Matches EAN-8/EAN-13 regex
64+
* Matches EAN-8/EAN-13/EAN-14 regex
6065
* Has valid check digit.
6166
*
6267
* @param {string} str

test/validators.js

+42
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,45 @@ describe('Validators', () => {
15731573
});
15741574
});
15751575

1576+
it('should validate alphanumeric string with ignored characters', () => {
1577+
test({
1578+
validator: 'isAlphanumeric',
1579+
args: ['en-US', { ignore: '@_- ' }], // ignore [@ space _ -]
1580+
valid: [
1581+
'Hello@123',
1582+
'this is a valid alphaNumeric string',
1583+
'En-US @ alpha_numeric',
1584+
],
1585+
invalid: [
1586+
'In*Valid',
1587+
'hello$123',
1588+
'{invalid}',
1589+
],
1590+
});
1591+
1592+
test({
1593+
validator: 'isAlphanumeric',
1594+
args: ['en-US', { ignore: /[\s/-]/g }], // ignore [space -]
1595+
valid: [
1596+
'en-US',
1597+
'this is a valid alphaNumeric string',
1598+
],
1599+
invalid: [
1600+
'INVALID$ AlphaNum Str',
1601+
'hello@123',
1602+
'abc*123',
1603+
],
1604+
});
1605+
1606+
test({
1607+
validator: 'isAlphanumeric',
1608+
args: ['en-US', { ignore: 1234 }], // invalid ignore matcher (ignore should be instance of a String or RegExp)
1609+
error: [
1610+
'alpha',
1611+
],
1612+
});
1613+
});
1614+
15761615
it('should validate defined english aliases', () => {
15771616
test({
15781617
validator: 'isAlphanumeric',
@@ -4716,6 +4755,9 @@ describe('Validators', () => {
47164755
'9771234567003',
47174756
'9783161484100',
47184757
'73513537',
4758+
'00012345600012',
4759+
'10012345678902',
4760+
'20012345678909',
47194761
],
47204762
invalid: [
47214763
'5901234123451',

0 commit comments

Comments
 (0)