diff --git a/src/index.test.ts b/src/index.test.ts index 3ff3178..142e03d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -4,15 +4,15 @@ import { createAxiosDateTransformer } from './index'; describe('axios-date-transformer', () => { test('transforms date strings to Date objects', async () => { - const response = { + const originalObject = { name: 'John Doe', - dob: new Date('1980-01-25T14:00:00.000Z'), + dob: '1980-01-25', issues: { alpha: new Date('2022-01-25T12:30:00.000Z'), beta: new Date('2022-01-26T09:45:00.000Z'), }, }; - const jsonResponse = JSON.stringify(response); + const jsonResponse = JSON.stringify(originalObject); const axiosInstance = createAxiosDateTransformer({ baseURL: 'https://example.org', }); @@ -30,19 +30,19 @@ describe('axios-date-transformer', () => { expect(data.dob).toBeInstanceOf(Date); // Assert that the date value of 'data.dob' is the same as 'response.dob' - expect(data.dob.toISOString()).toEqual(response.dob.toISOString()); + expect(data.dob.toISOString()).toEqual(new Date(originalObject.dob).toISOString()); // Assert that the 'issues.alpha' property is an instance of Date expect(data.issues.alpha).toBeInstanceOf(Date); // Assert that the date value of 'data.issues.alpha' is the same as 'response.issues.alpha' - expect(data.issues.alpha.toISOString()).toEqual(response.issues.alpha.toISOString()); + expect(data.issues.alpha.toISOString()).toEqual(originalObject.issues.alpha.toISOString()); // Assert that the 'issues.beta' property is an instance of Date expect(data.issues.beta).toBeInstanceOf(Date); // Assert that the date value of 'data.issues.beta' is the same as 'response.issues.beta' - expect(data.issues.beta.toISOString()).toEqual(response.issues.beta.toISOString()); + expect(data.issues.beta.toISOString()).toEqual(originalObject.issues.beta.toISOString()); // Restore the mock adapter mock.restore(); diff --git a/src/index.ts b/src/index.ts index 8a0ad32..ae4b8f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,8 @@ interface DateTransformerConfig extends CreateAxiosDefaults { // placeholder interface for future configuration } +const dateRegex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?)?$/; + const recursiveDateConversion = (data: any): any => { if (typeof data === 'object') { for (const key in data) { @@ -19,7 +21,6 @@ const recursiveDateConversion = (data: any): any => { }; const isDateString = (value: any): boolean => { - const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/; return dateRegex.test(value); };