Skip to content

Commit

Permalink
fix: ensure date-fns#parse is called with a string (#111)
Browse files Browse the repository at this point in the history
* fix: ensure date-fns#parse is called with a string

* add test case for schoolyear

* add comment to wrapper function `parse`
  • Loading branch information
lebalz committed Apr 6, 2024
1 parent 1473c96 commit 7ec4141
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 8 additions & 7 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test('should method logout return true', async () => {

cases(
'should getLatestSchoolyear return object',
async ({ validate }) => {
async ({ validate, dateFormat }) => {
const name = 'testName';
const id = 'testId';
const untis = createInstance();
Expand All @@ -91,14 +91,14 @@ cases(
{
id,
name,
startDate: '20191111',
endDate: '20191211',
startDate: dateFormat === 'string' ? '20191111' : 20191111,
endDate: dateFormat === 'string' ? '20191211' : 20191211,
},
{
id,
name,
startDate: '20191113',
endDate: '20191115',
startDate: dateFormat === 'string' ? '20191113' : 20191113,
endDate: dateFormat === 'string' ? '20191115' : 20191115,
},
],
});
Expand All @@ -111,8 +111,9 @@ cases(
});
},
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
{ name: 'with validate, string date', validate: true, dateFormat: 'string' },
{ name: 'with validate, numeric date', validate: true, dateFormat: 'number' },
{ name: 'without validate, string date', validate: false, dateFormat: 'string' },
]
);

Expand Down
20 changes: 19 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { serialize } from './cookie';
import axios from 'axios';
import { btoa } from './base-64';
import { parse, startOfDay, format } from 'date-fns';
import { parse as fnsParse, startOfDay, format, type ParseOptions } from 'date-fns';
import type { AxiosInstance } from 'axios';
import type {
Absences,
Expand All @@ -25,6 +25,24 @@ import type {
import type { InternalSchoolYear, SessionInformation } from './internal';
import { WebUntisElementType } from './types';

/**
* Ensures that the dateStr is a string when calling {@link fnsParse}.
* This is needed since some WebUntis servers return numbers instead of strings.
* @param dateStr {string | number}
* @param formatStr {string}
* @param referenceDate {DateType | number | string}
* @param options {ParseOptions | undefined}
* @returns
*/
const parse = <DateType extends Date>(
dateStr: string | number,
formatStr: string,
referenceDate: DateType | number | string,
options?: ParseOptions,
) => {
return fnsParse(`${dateStr}`, formatStr, referenceDate, options);
};

export class Base {
school: string;
schoolbase64: string;
Expand Down

0 comments on commit 7ec4141

Please sign in to comment.