From 440943896aa4abccd7b38b8a1b6e6bf549667afe Mon Sep 17 00:00:00 2001 From: Jye Cusch Date: Thu, 8 Jun 2023 16:53:57 +1000 Subject: [PATCH] convert singular frequencies with a value --- src/resources/schedule.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/resources/schedule.ts b/src/resources/schedule.ts index 11b9fb47..495f48ce 100644 --- a/src/resources/schedule.ts +++ b/src/resources/schedule.ts @@ -13,14 +13,7 @@ // limitations under the License. import { EventMiddleware, Faas, ScheduleMiddleware } from '../faas'; -const Frequencies = [ - 'day', - 'days', - 'hour', - 'hours', - 'minute', - 'minutes', -] as const; +const Frequencies = ['days', 'hours', 'minutes'] as const; export type Frequency = (typeof Frequencies)[number]; @@ -139,12 +132,20 @@ class Schedule { rate: string, ...middleware: ScheduleMiddleware[] ): Promise => { - // handle singular frequencies. e.g. schedule('something').every('day') + // handle singular frequencies without a value, e.g. schedule('something').every('day') if (Frequencies.indexOf(`${rate}s` as Frequency) !== -1) { rate = `1 ${rate}s`; // 'day' becomes '1 days' } - const r = new Rate(this, rate, ...middleware); + // handle singular frequencies with a value, e.g. schedule('something').every('1 day') + const rateParts = rate.split(' '); + const value = rateParts[0]; + let unit = rateParts[1]; + if (Frequencies.indexOf(`${unit}s` as Frequency) !== -1) { + unit = `${unit}s`; // 'day' becomes 'days' + } + + const r = new Rate(this, `${value} ${unit}`, ...middleware); // Start the new rate immediately return r['start'](); };