diff --git a/CHANGELOG.md b/CHANGELOG.md index ee249c877..79a29a98f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Accounting data is now built each night and saved in database - OpenAPI endpoint to fetch accounting data - Fix a bug: providing an array of attributes to filter OpenApi data, results in error +- Fix a bug: unable to manage stocks on new products - [TODO DEPLOY] `rails fablab:setup:build_accounting_lines` - Add reservation deadline parameter (#414) diff --git a/app/frontend/src/javascript/lib/format.ts b/app/frontend/src/javascript/lib/format.ts index ddf199aeb..adb6ee777 100644 --- a/app/frontend/src/javascript/lib/format.ts +++ b/app/frontend/src/javascript/lib/format.ts @@ -5,6 +5,29 @@ import { TDateISO, TDateISODate, THours, TMinutes } from '../typings/date-iso'; declare let Fablab: IFablab; export default class FormatLib { + /** + * Check if the provided variable is a JS Date oject + */ + static isDate = (value: unknown): boolean => { + return (value != null) && !isNaN(value as number) && (typeof (value as Date).getDate !== 'undefined'); + }; + + /** + * Check if the provided variable is an ISO 8601 representation of a date + */ + static isDateISO = (value: string): boolean => { + if (typeof value !== 'string') return false; + return !!value?.match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\d/); + }; + + /** + * Check if the provided variable is string representing a short time, according to ISO 8601 (e.g. 14:21) + */ + static isShortTimeISO = (value: string): boolean => { + if (typeof value !== 'string') return false; + return !!value?.match(/^\d\d:\d\d$/); + }; + /** * Return the formatted localized date for the given date */ @@ -17,8 +40,8 @@ export default class FormatLib { */ static time = (date: Date|TDateISO|`${THours}:${TMinutes}`): string => { let tempDate: Date; - const isoTimeMatch = (date as string)?.match(/^(\d\d):(\d\d)$/); - if (isoTimeMatch) { + if (FormatLib.isShortTimeISO(date as string)) { + const isoTimeMatch = (date as string)?.match(/^(\d\d):(\d\d)$/); tempDate = new Date(); tempDate.setHours(parseInt(isoTimeMatch[1], 10)); tempDate.setMinutes(parseInt(isoTimeMatch[2], 10));