Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosError } from 'axios';

Check warning on line 1 in src/lib/utils.ts

View workflow job for this annotation

GitHub Actions / eslint

'AxiosError' is defined but never used

import { IModule } from '../interfaces/module';
import { GenericFunction } from '../types';
Expand Down Expand Up @@ -44,7 +44,8 @@
* @returns A boolean that represents if `rawDate` is parseable as a Date object
*/
export function isISODate(rawDate: string) {
return !Number.isNaN(Date.parse(rawDate));
const isoDateRegex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})?)?$/;
return isoDateRegex.test(rawDate);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/spec/utils/getResourceClass.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ test('"getResourceClass" string resource', async (t) => {
t.is(klass, String);
});

test('"getResourceClass" ambiguous string that Date.parse accepts should remain String', async (t) => {
const resource = 'any_resource';
t.is(await getResourceClass(resource, 'PS 2'), String);
t.is(await getResourceClass(resource, 'test 1'), String);
});

test('"getResourceClass" number resource', async (t) => {
const resource = 'any_resource';
const klass = await getResourceClass(resource, 15);
Expand Down
15 changes: 15 additions & 0 deletions src/spec/utils/isISODate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ test('"isISODate" valid ISO format', (t) => {
t.assert(isISODate(validISODaTetimeString));
});

test('"isISODate" valid ISO date without time', (t) => {
t.assert(isISODate('2021-08-13'));
});

test('"isISODate" valid ISO date with timezone offset', (t) => {
t.assert(isISODate('2021-08-13T13:40:40.811+05:00'));
});

test('"isISODate" invalid ISO format', (t) => {
const invalidISODaTetimeString = 'This is not a date';
t.assert(!isISODate(invalidISODaTetimeString));
});

test('"isISODate" should not parse ambiguous strings as dates', (t) => {
t.assert(!isISODate('PS 2'));
t.assert(!isISODate('test 1'));
t.assert(!isISODate('Mar 5'));
t.assert(!isISODate('Jan 2024'));
});
Loading