Skip to content

Commit

Permalink
feat: add month literals
Browse files Browse the repository at this point in the history
  • Loading branch information
kbariotis committed Jan 5, 2022
1 parent f3dd843 commit 9fd89c7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/lib/inMonth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ describe('inMonth', () => {
it('should provide in January and December', () => {
expect(inMonth([1, 12]).toString()).toBe('* * * 1,12 *');
});

it('should provide in January and December using literals', () => {
expect(inMonth(['January', 'December']).toString()).toBe('* * * 1,12 *');
});
});
35 changes: 33 additions & 2 deletions src/lib/inMonth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import { MonthExpression } from './expression';

const map = {
January: 1,
February: 2,
March: 3,
April: 4,
May: 5,
June: 6,
July: 7,
August: 8,
September: 9,
October: 10,
November: 11,
December: 12,
};

/**
* In specific month or multiple specific months
*
* ```
* inMonth(3); // * * * 3 *
* inMonth([3,5]); // * * * 3,5 *
*
* inMonth('January'); // * * * 1 *
* inMonth(['January', 'December']); // * * * 1,12 *
* ```
*
*/
export const inMonth = (month: Month | Month[]) => {
export const inMonth = (
month: Month | Month[] | MonthLiteral | MonthLiteral[]
) => {
const arrayInput = Array.isArray(month) ? month : [month];

if (typeof arrayInput[0] === 'string') {
return new MonthExpression({
month: (arrayInput as MonthLiteral[])
.map((month) => map[month])
.join(','),
});
}

return new MonthExpression({
month: `${Array.isArray(month) ? month.join(',') : month}`,
month: arrayInput.join(','),
});
};
14 changes: 14 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ type Hour =

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Month = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type MonthLiteral =
| 'January'
| 'February'
| 'March'
| 'April'
| 'May'
| 'June'
| 'July'
| 'August'
| 'September'
| 'October'
| 'November'
| 'December';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type DayOfTheMonth =
Expand Down

0 comments on commit 9fd89c7

Please sign in to comment.