From 7c72f7d7f7e4ba667ae8ba211761ec0b793bf37c Mon Sep 17 00:00:00 2001 From: Zurab Date: Thu, 29 Dec 2022 16:16:39 +0100 Subject: [PATCH 1/5] allow user to keep the vat dirty --- src/lib/jsvat.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/jsvat.ts b/src/lib/jsvat.ts index dc0893b..b2e22b9 100644 --- a/src/lib/jsvat.ts +++ b/src/lib/jsvat.ts @@ -102,10 +102,14 @@ function isVatValid(vat: string, country: Country): boolean { return country.calcFn(regexResult[2]); } -export function checkVAT(vat: string, countriesList: ReadonlyArray = []): VatCheckResult { +export function checkVAT( + vat: string, + countriesList: ReadonlyArray = [], + shouldCleanVat?: boolean +): VatCheckResult { if (!vat) return makeResult(vat, false); const cleanVAT = removeExtraChars(vat); - const country = getCountry(cleanVAT, countriesList); - const isValid = country ? isVatValid(cleanVAT, country) : false; - return makeResult(cleanVAT, isValid, country); + const country = getCountry(shouldCleanVat ? cleanVAT : vat, countriesList); + const isValid = country ? isVatValid(shouldCleanVat ? cleanVAT : vat, country) : false; + return makeResult(shouldCleanVat ? cleanVAT : vat, isValid, country); } From 5df9164216aa0955f9c7b8f8196e28f2a8b1441c Mon Sep 17 00:00:00 2001 From: Zurab Date: Thu, 29 Dec 2022 16:37:59 +0100 Subject: [PATCH 2/5] set shouldCleanVat default value to true to avoid a braking change --- src/lib/jsvat.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/jsvat.ts b/src/lib/jsvat.ts index b2e22b9..a0e91cb 100644 --- a/src/lib/jsvat.ts +++ b/src/lib/jsvat.ts @@ -105,7 +105,7 @@ function isVatValid(vat: string, country: Country): boolean { export function checkVAT( vat: string, countriesList: ReadonlyArray = [], - shouldCleanVat?: boolean + shouldCleanVat: boolean = true ): VatCheckResult { if (!vat) return makeResult(vat, false); const cleanVAT = removeExtraChars(vat); From d3175bd5f28465064723893956dd59fcbfbf85d5 Mon Sep 17 00:00:00 2001 From: Zurab Date: Thu, 29 Dec 2022 17:44:14 +0100 Subject: [PATCH 3/5] improve code health --- lib/es6/lib/jsvat.js | 9 +++++---- src/lib/jsvat.ts | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/es6/lib/jsvat.js b/lib/es6/lib/jsvat.js index 59e1378..f823e5f 100644 --- a/lib/es6/lib/jsvat.js +++ b/lib/es6/lib/jsvat.js @@ -62,11 +62,12 @@ function isVatValid(vat, country) { return false; return country.calcFn(regexResult[2]); } -export function checkVAT(vat, countriesList = []) { +export function checkVAT(vat, countriesList = [], shouldRemoveExtraChars = true) { if (!vat) return makeResult(vat, false); const cleanVAT = removeExtraChars(vat); - const country = getCountry(cleanVAT, countriesList); - const isValid = country ? isVatValid(cleanVAT, country) : false; - return makeResult(cleanVAT, isValid, country); + const vatId = shouldRemoveExtraChars ? cleanVAT : vat; + const country = getCountry(vatId, countriesList); + const isValid = country ? isVatValid(vatId, country) : false; + return makeResult(vatId, isValid, country); } diff --git a/src/lib/jsvat.ts b/src/lib/jsvat.ts index a0e91cb..aa2aadf 100644 --- a/src/lib/jsvat.ts +++ b/src/lib/jsvat.ts @@ -105,11 +105,12 @@ function isVatValid(vat: string, country: Country): boolean { export function checkVAT( vat: string, countriesList: ReadonlyArray = [], - shouldCleanVat: boolean = true + shouldRemoveExtraChars: boolean = true ): VatCheckResult { if (!vat) return makeResult(vat, false); const cleanVAT = removeExtraChars(vat); - const country = getCountry(shouldCleanVat ? cleanVAT : vat, countriesList); - const isValid = country ? isVatValid(shouldCleanVat ? cleanVAT : vat, country) : false; - return makeResult(shouldCleanVat ? cleanVAT : vat, isValid, country); + const vatId = shouldRemoveExtraChars ? cleanVAT : vat; + const country = getCountry(vatId, countriesList); + const isValid = country ? isVatValid(vatId, country) : false; + return makeResult(vatId, isValid, country); } From 8580414cf106de76e687f72826af70eb6acde894 Mon Sep 17 00:00:00 2001 From: Zurab Date: Thu, 29 Dec 2022 17:44:50 +0100 Subject: [PATCH 4/5] add tests for checkVat function --- test/germany.spec.js | 22 ++++++++++++++++++++-- test/utils.js | 7 +++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/test/germany.spec.js b/test/germany.spec.js index 273322e..b38ccd7 100644 --- a/test/germany.spec.js +++ b/test/germany.spec.js @@ -1,6 +1,12 @@ -import { germany } from '../index'; +import { checkVAT, germany } from '../index'; import { codes, invalid, name, valid, validOnlyByFormat } from './countries_vat_lists/germany.vat'; -import { addCharsToString, checkInvalidVat, checkOnlyValidFormatVat, checkValidVat } from './utils'; +import { + addCharsToString, + checkInvalidVat, + checkOnlyValidFormatVat, + checkValidVat, + checkValidVatWithoutRemovingExtraChars +} from './utils'; describe('Germany', () => { it('should return "true" result for valid VATs', () => { @@ -15,10 +21,22 @@ describe('Germany', () => { valid.map((vat) => addCharsToString(vat, '-')).forEach((vat) => checkValidVat(vat, [germany], codes, name)); }); + it('should return "false" result for valid VATs with extra dash characters when shouldRemoveExtraChars is set to false', () => { + valid + .map((vat) => addCharsToString(vat, '-')) + .forEach((vat) => checkValidVatWithoutRemovingExtraChars(vat, [germany], codes, name)); + }); + it('should return "true" result for valid VATs with extra space characters', () => { valid.map((vat) => addCharsToString(vat, ' ')).forEach((vat) => checkValidVat(vat, [germany], codes, name)); }); + it('should return "false" result for valid VATs with extra space characters when shouldRemoveExtraChars is set to false', () => { + valid + .map((vat) => addCharsToString(vat, ' ')) + .forEach((vat) => checkValidVatWithoutRemovingExtraChars(vat, [germany], codes, name)); + }); + it('should return "false" result for invalid VATs', () => { invalid.forEach((vat) => checkInvalidVat(vat, [germany])); }); diff --git a/test/utils.js b/test/utils.js index a35c3e5..1fb7edc 100644 --- a/test/utils.js +++ b/test/utils.js @@ -14,6 +14,13 @@ export function checkValidVat(vat, countriesList, codes, name) { expect(result.country.isoCode.numeric).toBe(codes[2]); } +export function checkValidVatWithoutRemovingExtraChars(vat, countriesList, codes, name) { + const result = checkVAT(vat, countriesList, false); + + if (result.isValid) console.info('Following VAT should be invalid:', vat); + expect(result.isValid).toBe(false); +} + export function checkInvalidVat(vat, countriesList) { const result = checkVAT(vat, countriesList); if (result.isValid) console.info('Following VAT should be invalid:', vat); From c34ae873d4f63ed15b47a2e64e13eb9a94f8ab8f Mon Sep 17 00:00:00 2001 From: Zurab Date: Thu, 29 Dec 2022 20:43:50 +0100 Subject: [PATCH 5/5] remove unused import from germany test --- test/germany.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/germany.spec.js b/test/germany.spec.js index b38ccd7..383e103 100644 --- a/test/germany.spec.js +++ b/test/germany.spec.js @@ -1,4 +1,4 @@ -import { checkVAT, germany } from '../index'; +import { germany } from '../index'; import { codes, invalid, name, valid, validOnlyByFormat } from './countries_vat_lists/germany.vat'; import { addCharsToString,