From 9fd89c7207a169e53fe3498c575ad219896fa30b Mon Sep 17 00:00:00 2001 From: kbariotis Date: Wed, 5 Jan 2022 20:39:50 +0200 Subject: [PATCH] feat: add month literals --- src/lib/inMonth.spec.ts | 4 ++++ src/lib/inMonth.ts | 35 +++++++++++++++++++++++++++++++++-- src/lib/types.ts | 14 ++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/lib/inMonth.spec.ts b/src/lib/inMonth.spec.ts index 55deb84..0727f30 100644 --- a/src/lib/inMonth.spec.ts +++ b/src/lib/inMonth.spec.ts @@ -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 *'); + }); }); diff --git a/src/lib/inMonth.ts b/src/lib/inMonth.ts index c079860..d6eadb3 100644 --- a/src/lib/inMonth.ts +++ b/src/lib/inMonth.ts @@ -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(','), }); }; diff --git a/src/lib/types.ts b/src/lib/types.ts index 7a15e49..7830d78 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -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 =