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

feat(isEAN) - added support for EAN-14 along with test case, feat(isAlphanumeric)- added options(optional parameter) #1577

Merged
merged 4 commits into from
Mar 3, 2021
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Validator | Description
**equals(str, comparison)** | check if the string matches the comparison.
**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now).
**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.
**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`.
**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.
**isAscii(str)** | check if the string contains ASCII chars only.
**isBase32(str)** | check if a string is base32 encoded.
**isBase58(str)** | check if a string is base58 encoded.
Expand Down
18 changes: 16 additions & 2 deletions src/lib/isAlphanumeric.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import assertString from './util/assertString';
import { alphanumeric } from './alpha';

export default function isAlphanumeric(str, locale = 'en-US') {
assertString(str);
export default function isAlphanumeric(_str, locale = 'en-US', options = {}) {
assertString(_str);

let str = _str;
const { ignore } = options;

if (ignore) {
if (ignore instanceof RegExp) {
str = str.replace(ignore, '');
} else if (typeof ignore === 'string') {
str = str.replace(new RegExp(`[${ignore.replace(/[-[\]{}()*+?.,\\^$|#\\s]/g, '\\$&')}]`, 'g'), ''); // escape regex for ignore
} else {
throw new Error('ignore should be instance of a String or RegExp');
}
}

if (locale in alphanumeric) {
return alphanumeric[locale].test(str);
}
Expand Down
17 changes: 11 additions & 6 deletions src/lib/isEAN.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
* the thirteen-digit EAN-13, while the
* less commonly used 8-digit EAN-8 barcode was
* introduced for use on small packages.
* Also EAN/UCC-14 is used for Grouping of individual
* trade items above unit level(Intermediate, Carton or Pallet).
* For more info about EAN-14 checkout: https://www.gtin.info/itf-14-barcodes/
* EAN consists of:
* GS1 prefix, manufacturer code, product code and check digit
* Reference: https://en.wikipedia.org/wiki/International_Article_Number
* Reference: https://www.gtin.info/
*/

import assertString from './util/assertString';

/**
* Define EAN Lenghts; 8 for EAN-8; 13 for EAN-13
* and Regular Expression for valid EANs (EAN-8, EAN-13),
* with exact numberic matching of 8 or 13 digits [0-9]
* Define EAN Lenghts; 8 for EAN-8; 13 for EAN-13; 14 for EAN-14
* and Regular Expression for valid EANs (EAN-8, EAN-13, EAN-14),
* with exact numberic matching of 8 or 13 or 14 digits [0-9]
*/
const LENGTH_EAN_8 = 8;
const validEanRegex = /^(\d{8}|\d{13})$/;
const LENGTH_EAN_14 = 14;
const validEanRegex = /^(\d{8}|\d{13}|\d{14})$/;


/**
Expand All @@ -28,7 +33,7 @@ const validEanRegex = /^(\d{8}|\d{13})$/;
* @return {number}
*/
function getPositionWeightThroughLengthAndIndex(length, index) {
if (length === LENGTH_EAN_8) {
if (length === LENGTH_EAN_8 || length === LENGTH_EAN_14) {
return (index % 2 === 0) ? 3 : 1;
}

Expand Down Expand Up @@ -56,7 +61,7 @@ function calculateCheckDigit(ean) {

/**
* Check if string is valid EAN:
* Matches EAN-8/EAN-13 regex
* Matches EAN-8/EAN-13/EAN-14 regex
* Has valid check digit.
*
* @param {string} str
Expand Down
42 changes: 42 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,45 @@ describe('Validators', () => {
});
});

it('should validate alphanumeric string with ignored characters', () => {
test({
validator: 'isAlphanumeric',
args: ['en-US', { ignore: '@_- ' }], // ignore [@ space _ -]
valid: [
'Hello@123',
'this is a valid alphaNumeric string',
'En-US @ alpha_numeric',
],
invalid: [
'In*Valid',
'hello$123',
'{invalid}',
],
});

test({
validator: 'isAlphanumeric',
args: ['en-US', { ignore: /[\s/-]/g }], // ignore [space -]
valid: [
'en-US',
'this is a valid alphaNumeric string',
],
invalid: [
'INVALID$ AlphaNum Str',
'hello@123',
'abc*123',
],
});

test({
validator: 'isAlphanumeric',
args: ['en-US', { ignore: 1234 }], // invalid ignore matcher (ignore should be instance of a String or RegExp)
error: [
'alpha',
],
});
});

it('should validate defined english aliases', () => {
test({
validator: 'isAlphanumeric',
Expand Down Expand Up @@ -4682,6 +4721,9 @@ describe('Validators', () => {
'9771234567003',
'9783161484100',
'73513537',
'00012345600012',
'10012345678902',
'20012345678909',
],
invalid: [
'5901234123451',
Expand Down
Loading