Skip to content

Commit

Permalink
Add new options for start date and start of week
Browse files Browse the repository at this point in the history
  • Loading branch information
rautio committed Oct 12, 2023
1 parent 7a8837a commit bbfd4f7
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 12 additions & 1 deletion src/options/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,36 @@ 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(
parseOptions({ preset: 'system', defaultHour: '7', defaultMinute: '45' }),
).toEqual({
defaultHour: '7',
defaultMinute: '45',
startDate: new Date(),
startOfWeek: '1',
});
});
});
29 changes: 22 additions & 7 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 };
};
89 changes: 89 additions & 0 deletions src/options/types.d.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit bbfd4f7

Please sign in to comment.