-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from FaberVitale/fix/august-event-in-upcomng-e…
…vents-page-79 fix: august event placeholder visible event in upcoming events page
- Loading branch information
Showing
9 changed files
with
228 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { parseMonthsList, type ParseMonthsListOutput } from '../time'; | ||
|
||
describe('time', () => { | ||
describe('parseMonthsWithoutGeneratedEvents', () => { | ||
const createEmpyOutput = (): ParseMonthsListOutput => ({ | ||
parsedMonths: new Set(), | ||
errors: [], | ||
}); | ||
|
||
it.each([undefined, null, '', ' ', '\n'])( | ||
'returns empty output (0 months & 0 errors) when input=%j', | ||
(input) => { | ||
expect(parseMonthsList(input)).toEqual(createEmpyOutput()); | ||
} | ||
); | ||
|
||
it.each([ | ||
{ | ||
input: '08', | ||
parsedMonths: new Set([7]), | ||
}, | ||
{ | ||
input: '8', | ||
parsedMonths: new Set([7]), | ||
}, | ||
{ | ||
input: '08,01,3,4', | ||
parsedMonths: new Set([0, 2, 3, 7]), | ||
}, | ||
{ | ||
input: ', 08 ,01,3,4,4,8,, , ,', | ||
parsedMonths: new Set([0, 2, 3, 7]), | ||
}, | ||
])( | ||
'parses input=$input without errors and parsedMonths=$parsedMonths', | ||
({ input, parsedMonths }) => { | ||
const output = parseMonthsList(input); | ||
expect(output.parsedMonths).toEqual(parsedMonths); | ||
expect(output.errors).toHaveLength(0); | ||
} | ||
); | ||
|
||
it.each([ | ||
{ | ||
input: '01,0,5,13,NaN', | ||
expected: { | ||
parsedMonths: new Set([0, 4]), | ||
invalidMonths: ['0', '13', 'NaN'], | ||
}, | ||
}, | ||
{ | ||
input: '-1', | ||
expected: { | ||
parsedMonths: new Set([]), | ||
invalidMonths: ['-1'], | ||
}, | ||
}, | ||
])( | ||
'parses input=$input correctly when there is invalid input', | ||
({ input, expected }) => { | ||
const output = parseMonthsList(input); | ||
expect(output.parsedMonths).toEqual(expected.parsedMonths); | ||
expect(output.errors.map((r) => r.input).sort()).toEqual( | ||
expected.invalidMonths.slice().sort() | ||
); | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { parse as parseDate } from 'date-fns'; | ||
|
||
const monthFormat = 'LL'; | ||
|
||
export type ParseMonthsListOutput = { | ||
/** | ||
* Set of **0-based** index months. | ||
*/ | ||
readonly parsedMonths: Set<number>; | ||
readonly errors: { input: string; error: unknown }[]; | ||
}; | ||
|
||
/** | ||
* Parses a comma separated list of months. | ||
* | ||
* Returns an output object containing a set of **0-based** index months. | ||
* | ||
* @param months a comma separated list of months. | ||
* @returns {ParseMonthsListOutput} an output object | ||
*/ | ||
export const parseMonthsList = ( | ||
months: string | undefined | null | ||
): ParseMonthsListOutput => { | ||
const formattedMonths = | ||
months | ||
?.trim() | ||
.split(/\s*,\s*/) | ||
.filter(Boolean) ?? []; | ||
const referenceDate = new Date(new Date().getFullYear(), 0, 4); // A reference date with timezone-safe date of month. | ||
|
||
const output: ParseMonthsListOutput = { | ||
parsedMonths: new Set<number>(), | ||
errors: [], | ||
}; | ||
|
||
for (const formattedMonth of formattedMonths) { | ||
try { | ||
const parsedDate = parseDate(formattedMonth, monthFormat, referenceDate); | ||
|
||
if (Number.isFinite(parsedDate.getTime())) { | ||
output.parsedMonths.add(parsedDate.getMonth()); | ||
} else { | ||
throw new Error( | ||
`parseMonthsList: cannot parse date using format=${monthFormat}, input=${formattedMonth}` | ||
); | ||
} | ||
} catch (err) { | ||
output.errors.push({ | ||
input: formattedMonth, | ||
error: err, | ||
}); | ||
} | ||
} | ||
|
||
return output; | ||
}; |