From 632bc2dcbb356ad167344f90e289c393693f6cc4 Mon Sep 17 00:00:00 2001 From: Kostas Bariotis Date: Mon, 3 Jan 2022 09:26:53 +0200 Subject: [PATCH] fix: API refactor (#1) * fix it * fix * fix it * add more tests * simplify combine API * refactor between API * refactor combine --- .github/workflows/build.yaml | 29 ++++++++++++++ .github/workflows/semantic-pr-title.yaml | 13 ++++++ .github/workflows/test.yaml | 29 ++++++++++++++ src/lib/at.ts | 30 +------------- src/lib/between.spec.ts | 20 ++++++++-- src/lib/combine.spec.ts | 50 ++++++++---------------- src/lib/combine.ts | 14 +++---- src/lib/every.spec.ts | 22 +++++------ src/lib/expression.spec.ts | 2 +- src/lib/inMonth.spec.ts | 4 +- src/lib/on.spec.ts | 4 +- 11 files changed, 128 insertions(+), 89 deletions(-) create mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/semantic-pr-title.yaml create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..e12c374 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,29 @@ +name: Build +on: + pull_request: + +jobs: + build: + name: Build + runs-on: ubuntu-18.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Setup Node.js + uses: actions/setup-node@v1 + with: + node-version: 16 + + - uses: actions/cache@v2 + with: + path: '**/node_modules' + key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }} + + - name: Install dependencies + run: npm ci + + - name: Build + run: npm run build diff --git a/.github/workflows/semantic-pr-title.yaml b/.github/workflows/semantic-pr-title.yaml new file mode 100644 index 0000000..2d80e8a --- /dev/null +++ b/.github/workflows/semantic-pr-title.yaml @@ -0,0 +1,13 @@ +name: Semantic PR title + +on: + pull_request: + types: [opened, synchronize, reopened, edited] + +jobs: + semantic: + runs-on: ubuntu-latest + steps: + - uses: amannn/action-semantic-pull-request@v3.0.0 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..644191f --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,29 @@ +name: Test +on: + pull_request: + +jobs: + test: + name: Test + runs-on: ubuntu-18.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Setup Node.js + uses: actions/setup-node@v1 + with: + node-version: 16 + + - uses: actions/cache@v2 + with: + path: '**/node_modules' + key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }} + + - name: Install dependencies + run: npm ci + + - name: Test + run: npm run test diff --git a/src/lib/at.ts b/src/lib/at.ts index 4d47560..16c80d9 100644 --- a/src/lib/at.ts +++ b/src/lib/at.ts @@ -1,16 +1,7 @@ -import { - DayOfTheMonthExpression, - DayOfTheWeekExpression, - HourExpression, - MinuteExpression, - MonthExpression, -} from './expression'; +import { HourExpression, MinuteExpression } from './expression'; type AtMinute = Minute | Minute[]; type AtHour = Hour | Hour[]; -type AtMonth = Month | Month[]; -type AtDayOfTheMonth = DayOfTheMonth | DayOfTheMonth[]; -type AtDayOfTheWeek = DayOfTheWeek | DayOfTheWeek[]; export const at = { minute: (minute: AtMinute) => { @@ -23,23 +14,4 @@ export const at = { hour: `${Array.isArray(hour) ? hour.join(',') : hour}`, }); }, - month: (month: AtMonth) => { - return new MonthExpression({ - month: `${Array.isArray(month) ? month.join(',') : month}`, - }); - }, - dayOfTheMonth: (dayOfTheMonth: AtDayOfTheMonth) => { - return new DayOfTheMonthExpression({ - dayOfTheMonth: `${ - Array.isArray(dayOfTheMonth) ? dayOfTheMonth.join(',') : dayOfTheMonth - }`, - }); - }, - dayOfTheWeek: (dayOfTheWeek: AtDayOfTheWeek) => { - return new DayOfTheWeekExpression({ - dayOfTheWeek: `${ - Array.isArray(dayOfTheWeek) ? dayOfTheWeek.join(',') : dayOfTheWeek - }`, - }); - }, }; diff --git a/src/lib/between.spec.ts b/src/lib/between.spec.ts index 4c71d95..93273bc 100644 --- a/src/lib/between.spec.ts +++ b/src/lib/between.spec.ts @@ -1,20 +1,32 @@ import { at } from './at'; import { between } from './between'; +import { inMonth } from './inMonth'; +import { on } from './on'; describe('between', () => { - it('should combine two minutes', () => { + it('should provide minutes range', () => { expect(between(at.minute(23), at.minute(33)).toString()).toBe( '23-33 * * * *' ); }); - it('hour range', () => { + it('should provide hours range', () => { expect(between(at.hour(2), at.hour(11)).toString()).toBe('* 2-11 * * *'); }); - it('monday to friday', () => { - expect(between(at.dayOfTheWeek(0), at.dayOfTheWeek(4)).toString()).toBe( + it('should provide month range', () => { + expect(between(inMonth(2), inMonth(11)).toString()).toBe('* * * 2-11 *'); + }); + + it('should provide days of the week, monday to friday', () => { + expect(between(on.dayOfTheWeek(0), on.dayOfTheWeek(4)).toString()).toBe( '* * * * 0-4' ); }); + + it('should throw an error if different types', () => { + expect(() => + between(on.dayOfTheWeek(0), on.dayOfTheMonth(4)).toString() + ).toThrow(); + }); }); diff --git a/src/lib/combine.spec.ts b/src/lib/combine.spec.ts index f31ff4a..f4dfe09 100644 --- a/src/lib/combine.spec.ts +++ b/src/lib/combine.spec.ts @@ -1,54 +1,38 @@ import { at } from './at'; import { combine } from './combine'; import { every } from './every'; +import { inMonth } from './inMonth'; import { on } from './on'; describe('combine', () => { - it('every hour at 30 minutes', () => { - expect( - combine( - every.dayOfTheWeek(), - every.month(), - every.dayOfTheMonth(), - every.hours(2), - at.minute(30) - ).toString() - ).toBe('30 */2 * * *'); + it('should provide every hour at 30 minutes', () => { + expect(combine(every.hours(2), at.minute(30)).toString()).toBe( + '30 */2 * * *' + ); }); - it('every day at midnight', () => { + it('should provide every day at midnight', () => { + expect(combine(at.hour(0), at.minute(0)).toString()).toBe('0 0 * * *'); + }); + + it('should provide every Thursday at midnight in July', () => { expect( combine( - every.dayOfTheWeek(), - every.month(), - every.dayOfTheMonth(), + on.dayOfTheWeek(5), at.hour(0), + inMonth(7), at.minute(0) ).toString() - ).toBe('0 0 * * *'); + ).toBe('0 0 * 7 5'); }); - it('every day at 2am', () => { - expect( - combine( - every.dayOfTheWeek(), - every.month(), - every.dayOfTheMonth(), - at.hour(2), - at.minute(0) - ).toString() - ).toBe('0 2 * * *'); + it('should provide every day at 2am', () => { + expect(combine(at.hour(2), at.minute(0)).toString()).toBe('0 2 * * *'); }); - it('every sunday at 2am', () => { + it('should provide every sunday at 2am', () => { expect( - combine( - on.dayOfTheWeek(6), - every.month(), - every.dayOfTheMonth(), - at.hour(2), - at.minute(0) - ).toString() + combine(on.dayOfTheWeek(6), at.hour(2), at.minute(0)).toString() ).toBe('0 2 * * 6'); }); }); diff --git a/src/lib/combine.ts b/src/lib/combine.ts index 2ea3c82..d4c95c9 100644 --- a/src/lib/combine.ts +++ b/src/lib/combine.ts @@ -8,13 +8,13 @@ import { } from './expression'; function combine( - ...expressions: [ - DayOfTheWeekExpression, - MonthExpression, - DayOfTheMonthExpression, - HourExpression, - MinuteExpression - ] + ...expressions: Array< + | DayOfTheWeekExpression + | MonthExpression + | DayOfTheMonthExpression + | HourExpression + | MinuteExpression + > ) { return expressions.reduce((initial, current) => { if (current instanceof MinuteExpression) { diff --git a/src/lib/every.spec.ts b/src/lib/every.spec.ts index 03a853f..89cc9cc 100644 --- a/src/lib/every.spec.ts +++ b/src/lib/every.spec.ts @@ -1,47 +1,47 @@ import { every } from './every'; describe('every', () => { - it('every minute', () => { + it('should provide every minute', () => { expect(every.minute().toString()).toBe('* * * * *'); }); - it('every 2 minutes', () => { + it('should provide every 2 minutes', () => { expect(every.minutes(2).toString()).toBe('*/2 * * * *'); }); - it('every even minute', () => { + it('should provide every even minute', () => { expect(every.minutes(2).toString()).toBe('*/2 * * * *'); }); - it('every 5 minutes', () => { + it('should provide every 5 minutes', () => { expect(every.minutes(5).toString()).toBe('*/5 * * * *'); }); - it('every quarter hour', () => { + it('should provide every quarter hour', () => { expect(every.minutes(15).toString()).toBe('*/15 * * * *'); }); - it('every half hour', () => { + it('should provide every half hour', () => { expect(every.minutes(30).toString()).toBe('*/30 * * * *'); }); - it('every hour', () => { + it('should provide every hour', () => { expect(every.hour().toString()).toBe('0 * * * *'); }); - it('every 1 hour', () => { + it('should provide every 1 hour', () => { expect(every.hours(1).toString()).toBe('0 */1 * * *'); }); - it('every 2 hours', () => { + it('should provide every 2 hours', () => { expect(every.hours(2).toString()).toBe('0 */2 * * *'); }); - it('every day', () => { + it('should provide every day', () => { expect(every.dayOfTheWeek().toString()).toBe('0 0 * * *'); }); - it('every sunday', () => { + it('should provide every sunday', () => { expect(every.dayOfTheWeek(6).toString()).toBe('0 0 * * */6'); }); }); diff --git a/src/lib/expression.spec.ts b/src/lib/expression.spec.ts index 960f7e2..6a8ea77 100644 --- a/src/lib/expression.spec.ts +++ b/src/lib/expression.spec.ts @@ -1,7 +1,7 @@ import { Expression } from './expression'; describe('expression', () => { - it('should run every minute', () => { + it('should provide should run every minute', () => { const expression = new Expression(); expect(expression.toString()).toBe('* * * * *'); }); diff --git a/src/lib/inMonth.spec.ts b/src/lib/inMonth.spec.ts index 53e8667..55deb84 100644 --- a/src/lib/inMonth.spec.ts +++ b/src/lib/inMonth.spec.ts @@ -1,11 +1,11 @@ import { inMonth } from './inMonth'; describe('inMonth', () => { - it('in January', () => { + it('should provide in January', () => { expect(inMonth(1).toString()).toBe('* * * 1 *'); }); - it('in January and December', () => { + it('should provide in January and December', () => { expect(inMonth([1, 12]).toString()).toBe('* * * 1,12 *'); }); }); diff --git a/src/lib/on.spec.ts b/src/lib/on.spec.ts index d31f8d6..73e2e53 100644 --- a/src/lib/on.spec.ts +++ b/src/lib/on.spec.ts @@ -1,11 +1,11 @@ import { on } from './on'; describe('on', () => { - it('on Sunday', () => { + it('should provide on Sunday', () => { expect(on.dayOfTheWeek(6).toString()).toBe('0 0 * * 6'); }); - it('on 3rd day of the month', () => { + it('should provide on 3rd day of the month', () => { expect(on.dayOfTheMonth(3).toString()).toBe('0 0 3 * *'); }); });