Skip to content

Commit

Permalink
Merge pull request #13 from angelxmoreno/fix/YYY-MM-DD
Browse files Browse the repository at this point in the history
fix: #11 capturing "YYYY-MM-DD" formats
  • Loading branch information
angelxmoreno authored Sep 28, 2024
2 parents 2cecb06 + f29e794 commit 39cab52
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ interface DateTransformerConfig<T = any> extends CreateAxiosDefaults<T> {
// 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) {
Expand All @@ -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);
};

Expand Down

0 comments on commit 39cab52

Please sign in to comment.