From 67b86c68b736f14e262cc61825bc2ba4fb1b236b Mon Sep 17 00:00:00 2001 From: elmaxe Date: Sun, 2 May 2021 17:48:41 +0200 Subject: [PATCH 1/4] feat: add support for Swedish license plates --- README.md | 2 +- src/lib/isLicensePlate.js | 2 ++ test/validators.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7dbed1295..83e76e570 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Validator | Description **isJWT(str)** | check if the string is valid JWT token. **isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.

`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format. **isLength(str [, options])** | check if the string's length falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. -**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL', 'pt-BR'']` or `any`). +**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL', 'pt-BR', 'sv-SE']` or `any`). **isLocale(str)** | check if the string is a locale **isLowercase(str)** | check if the string is lowercase. **isMACAddress(str)** | check if the string is a MAC address.

`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. diff --git a/src/lib/isLicensePlate.js b/src/lib/isLicensePlate.js index a28d66da8..1a8bb2e9b 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -10,6 +10,8 @@ const validators = { /^[A-Z]{2}[- ]?((\d{3}[- ]?(([A-Z]{2})|T))|(R[- ]?\d{3}))$/.test(str), 'pt-BR': str => /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), + 'sv-SE': str => + /(^[ABCDEFGHJKLMNOPRSTUWXYZ]{3} [\d]{2}[ABCDEFGHJKLMNOPRSTUWXYZ\d]$)|(^[A-ZÅÄÖ ]{2,7}$)/.test(str), }; export default function isLicensePlate(str, locale) { diff --git a/test/validators.js b/test/validators.js index 56e581d39..5917c51ea 100644 --- a/test/validators.js +++ b/test/validators.js @@ -10722,6 +10722,40 @@ describe('Validators', () => { 'FS AB 1234 A', ], }); + test({ + validator: 'isLicensePlate', + args: ['sv-SE'], + valid: [ + 'ABC 123', + 'ABC 12A', + 'A WORD', + 'WORD', + 'ÅSNA', + 'EN VARG', + 'CERISE', + 'AA', + 'ABCDEFG', + 'ÅÄÖ', + 'ÅÄÖ ÅÄÖ', + ], + invalid: [ + '', + 'IQV 123', + 'ABI 12Q', + 'ÅÄÖ 123', + 'ÅÄÖ 12A', + 'AB1 A23', + 'AB1 12A', + 'lower', + 'abc 123', + 'abc 12A', + 'abc 12a', + 'AbC 12a', + 'WORDLONGERTHANSEVENCHARACTERS', + 'A', + 'ABC-123', + ], + }); }); it('should validate english VAT numbers', () => { test({ From c3da5b828c48b1bc1e089fb85a99cf6e7c300157 Mon Sep 17 00:00:00 2001 From: elmaxe Date: Sun, 2 May 2021 17:55:45 +0200 Subject: [PATCH 2/4] feat: allow spaces --- src/lib/isLicensePlate.js | 2 +- test/validators.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/isLicensePlate.js b/src/lib/isLicensePlate.js index 1a8bb2e9b..d3b81aaca 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -11,7 +11,7 @@ const validators = { 'pt-BR': str => /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), 'sv-SE': str => - /(^[ABCDEFGHJKLMNOPRSTUWXYZ]{3} [\d]{2}[ABCDEFGHJKLMNOPRSTUWXYZ\d]$)|(^[A-ZÅÄÖ ]{2,7}$)/.test(str), + /(^[ABCDEFGHJKLMNOPRSTUWXYZ]{3}[ ]?[\d]{2}[ABCDEFGHJKLMNOPRSTUWXYZ\d]$)|(^[A-ZÅÄÖ ]{2,7}$)/.test(str), }; export default function isLicensePlate(str, locale) { diff --git a/test/validators.js b/test/validators.js index 5917c51ea..a360eeebc 100644 --- a/test/validators.js +++ b/test/validators.js @@ -10728,6 +10728,8 @@ describe('Validators', () => { valid: [ 'ABC 123', 'ABC 12A', + 'ABC123', + 'ABC12A', 'A WORD', 'WORD', 'ÅSNA', @@ -10741,6 +10743,7 @@ describe('Validators', () => { invalid: [ '', 'IQV 123', + 'IQV123', 'ABI 12Q', 'ÅÄÖ 123', 'ÅÄÖ 12A', From 7d5ebc93c7623f95f35f18092b99f31d4ea01fd3 Mon Sep 17 00:00:00 2001 From: elmaxe Date: Mon, 6 Dec 2021 12:07:45 +0100 Subject: [PATCH 3/4] fix(docs): sort alphabetically --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f313b7c57..9935cd63d 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ Validator | Description **isJWT(str)** | check if the string is valid JWT token. **isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.

`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format. **isLength(str [, options])** | check if the string's length falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. -**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL', 'pt-BR', 'cs-CZ', 'sv-SE']` or `any`). +**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `any`). **isLocale(str)** | check if the string is a locale **isLowercase(str)** | check if the string is lowercase. **isMACAddress(str)** | check if the string is a MAC address.

`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. From d84dbe81b5f189274ec601f58c193f908e203322 Mon Sep 17 00:00:00 2001 From: elmaxe Date: Mon, 6 Dec 2021 12:21:44 +0100 Subject: [PATCH 4/4] fix(isLicensePlate): Trim, disallow 0 and fix regex --- src/lib/isLicensePlate.js | 2 +- test/validators.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/isLicensePlate.js b/src/lib/isLicensePlate.js index d0481d647..dcf9f30bd 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -13,7 +13,7 @@ const validators = { 'pt-BR': str => /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), 'sv-SE': str => - /(^[ABCDEFGHJKLMNOPRSTUWXYZ]{3}[ ]?[\d]{2}[ABCDEFGHJKLMNOPRSTUWXYZ\d]$)|(^[A-ZÅÄÖ ]{2,7}$)/.test(str), + /^[A-HJ-PR-UW-Z]{3} ?[\d]{2}[A-HJ-PR-UW-Z1-9]$|(^[A-ZÅÄÖ ]{2,7}$)/.test(str.trim()), }; export default function isLicensePlate(str, locale) { diff --git a/test/validators.js b/test/validators.js index 9c8f25204..36e7ab497 100644 --- a/test/validators.js +++ b/test/validators.js @@ -10989,6 +10989,7 @@ describe('Validators', () => { ], invalid: [ '', + ' ', 'IQV 123', 'IQV123', 'ABI 12Q',