From bbfd4f7d75c63b4600076a82494040bea43e20c6 Mon Sep 17 00:00:00 2001 From: Oskari Rautiainen Date: Thu, 12 Oct 2023 07:47:16 -0700 Subject: [PATCH] Add new options for start date and start of week --- README.md | 12 +++--- src/options/index.test.ts | 13 +++++- src/options/index.ts | 29 ++++++++++--- src/options/types.d.ts | 89 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 src/options/types.d.ts diff --git a/README.md b/README.md index bab2ba5..7c66578 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,12 @@ parseText('Every weekday', { preset: 'system' }); All configurations are optional. The default start of day is 9am. A 'weekday' is considered Monday-Friday and 'weekend' is Saturday or Sunday. -| Option | Description | Default | -| ------------- | ------------------------------------------------------------------------------------------------------------------ | ------- | -| preset | `'system'` as a preset will use 00:00 as the start of the day. | n/a | -| defaultHour | What hour should each day start on. This is the default used when using non specific times like `'Every Monday'` | `'9'` | -| defaultMinute | What minute should each day start on. This is the default used when using non specific times like `'Every Monday'` | `'0'` | +| Option | Description | Default | +| --------------- | ------------------------------------------------------------------------------------------------------------------ | ---------------------------- | +| `preset` | `'system'` as a preset will use 00:00 as the start of the day. | n/a | +| `defaultHour` | What hour should each day start on. This is the default used when using non specific times like `'Every Monday'` | `'9'` | +| `defaultMinute` | What minute should each day start on. This is the default used when using non specific times like `'Every Monday'` | `'0'` | +| `startDate` | What starting date to use for relative schedules like `"tomorrow"`, `"next month"`, etc. | Right now - aka `new Date()` | +| `startOfWeek` | Which day starts the week. Used for relative schedules like `"next week"` | `'1'` | Interested in Contributing? Check out the [Contribution](./CONTRIBUTING.md) guide. diff --git a/src/options/index.test.ts b/src/options/index.test.ts index 7a04347..722fc02 100644 --- a/src/options/index.test.ts +++ b/src/options/index.test.ts @@ -2,18 +2,27 @@ import { parseOptions } from './index'; describe('parseOptions() should', () => { test('use default options if none passed', () => { - expect(parseOptions()).toEqual({ defaultHour: '9', defaultMinute: '0' }); + expect(parseOptions()).toEqual({ + defaultHour: '9', + defaultMinute: '0', + startDate: new Date(), + startOfWeek: '1', + }); }); test('honor presets', () => { expect(parseOptions({ preset: 'system' })).toEqual({ defaultHour: '0', defaultMinute: '0', + startDate: new Date(), + startOfWeek: '1', }); }); test('use custom start of day', () => { expect(parseOptions({ defaultHour: '7', defaultMinute: '45' })).toEqual({ defaultHour: '7', defaultMinute: '45', + startDate: new Date(), + startOfWeek: '1', }); // Custom values should override default expect( @@ -21,6 +30,8 @@ describe('parseOptions() should', () => { ).toEqual({ defaultHour: '7', defaultMinute: '45', + startDate: new Date(), + startOfWeek: '1', }); }); }); diff --git a/src/options/index.ts b/src/options/index.ts index adac95b..da3da46 100644 --- a/src/options/index.ts +++ b/src/options/index.ts @@ -1,20 +1,29 @@ +import type { WeekDays, Hours, Minutes } from './types'; + export type InputOptions = { preset?: 'system'; - defaultHour?: string; - defaultMinute?: string; + defaultHour?: Hours; + defaultMinute?: Minutes; + startOfWeek?: WeekDays; + startDate?: Date; }; export type Options = { - defaultHour: string; - defaultMinute: string; + defaultHour: Hours; + defaultMinute: Minutes; + startDate: Date; + startOfWeek: WeekDays; }; -export const DEFAULT_DAY_MINUTES = '0'; -export const DEFAULT_DAY_HOURS = '9'; +export const DEFAULT_DAY_MINUTES: Minutes = '0' as Minutes; +export const DEFAULT_DAY_HOURS: Hours = '9' as Hours; +export const DEFAULT_DAY_OF_WEEK: WeekDays = '1' as WeekDays; export const parseOptions = (options?: InputOptions): Options => { let defaultHour = DEFAULT_DAY_HOURS; let defaultMinute = DEFAULT_DAY_MINUTES; + let startDate = new Date(); + let startOfWeek = DEFAULT_DAY_OF_WEEK; if (options?.preset === 'system') { defaultHour = '0'; defaultMinute = '0'; @@ -25,5 +34,11 @@ export const parseOptions = (options?: InputOptions): Options => { if (options?.defaultMinute) { defaultMinute = options.defaultMinute; } - return { defaultHour, defaultMinute }; + if (options?.startDate) { + startDate = options.startDate; + } + if (options?.startOfWeek) { + startOfWeek = options.startOfWeek; + } + return { defaultHour, defaultMinute, startDate, startOfWeek }; }; diff --git a/src/options/types.d.ts b/src/options/types.d.ts new file mode 100644 index 0000000..e0ab30d --- /dev/null +++ b/src/options/types.d.ts @@ -0,0 +1,89 @@ +export type WeekDays = '0' | '1' | '2' | '3' | '4' | '5' | '6'; + +export type Hours = + | '0' + | '1' + | '2' + | '3' + | '4' + | '5' + | '6' + | '7' + | '8' + | '9' + | '10' + | '11' + | '12' + | '13' + | '14' + | '15' + | '16' + | '17' + | '18' + | '19' + | '20' + | '21' + | '22' + | '23'; + +export type Minutes = + | '0' + | '1' + | '2' + | '3' + | '4' + | '5' + | '6' + | '7' + | '8' + | '9' + | '10' + | '11' + | '12' + | '13' + | '14' + | '15' + | '16' + | '17' + | '18' + | '19' + | '20' + | '21' + | '22' + | '23' + | '24' + | '25' + | '26' + | '27' + | '28' + | '29' + | '30' + | '31' + | '32' + | '33' + | '34' + | '35' + | '36' + | '37' + | '38' + | '39' + | '40' + | '41' + | '42' + | '43' + | '44' + | '45' + | '46' + | '47' + | '48' + | '49' + | '50' + | '51' + | '52' + | '53' + | '54' + | '55' + | '56' + | '57' + | '58' + | '59';