diff --git a/src/components/ui/VnLinkPhone.vue b/src/components/ui/VnLinkPhone.vue index 4c045968f..4068498cd 100644 --- a/src/components/ui/VnLinkPhone.vue +++ b/src/components/ui/VnLinkPhone.vue @@ -1,6 +1,7 @@ diff --git a/src/filters/index.js b/src/filters/index.js index ce5c44706..1f7ac77dd 100644 --- a/src/filters/index.js +++ b/src/filters/index.js @@ -12,10 +12,12 @@ import dateRange from './dateRange'; import toHour from './toHour'; import dashOrCurrency from './dashOrCurrency'; import getParamWhere from './getParamWhere'; +import parsePhone from './parsePhone'; import isDialogOpened from './isDialogOpened'; export { isDialogOpened, + parsePhone, toLowerCase, toLowerCamel, toDate, diff --git a/src/filters/parsePhone.js b/src/filters/parsePhone.js new file mode 100644 index 000000000..696f55007 --- /dev/null +++ b/src/filters/parsePhone.js @@ -0,0 +1,12 @@ +export default function (phone, prefix = 34) { + if (phone.startsWith('+')) { + return `${phone.slice(1)}`; + } + if (phone.startsWith('00')) { + return `${phone.slice(2)}`; + } + if (phone.startsWith(prefix) && phone.length === prefix.length + 9) { + return `${prefix}${phone.slice(prefix.length)}`; + } + return `${prefix}${phone}`; +} diff --git a/test/vitest/__tests__/components/common/VnLinkPhone.spec.js b/test/vitest/__tests__/components/common/VnLinkPhone.spec.js new file mode 100644 index 000000000..e460ab2fc --- /dev/null +++ b/test/vitest/__tests__/components/common/VnLinkPhone.spec.js @@ -0,0 +1,29 @@ +import { describe, it, expect } from 'vitest'; +import parsePhone from 'src/filters/parsePhone'; + +describe('parsePhone filter', () => { + it("adds prefix +34 if it doesn't have one", () => { + const resultado = parsePhone('123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('maintains prefix +34 if it is already correct', () => { + const resultado = parsePhone('+34123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('converts prefix 0034 to +34', () => { + const resultado = parsePhone('0034123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('converts prefix 34 without symbol to +34', () => { + const resultado = parsePhone('34123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('replaces incorrect prefix with the correct one', () => { + const resultado = parsePhone('+44123456789', '34'); + expect(resultado).toBe('44123456789'); + }); +});