Skip to content

Commit

Permalink
fix: API refactor (#1)
Browse files Browse the repository at this point in the history
* fix it

* fix

* fix it

* add more tests

* simplify combine API

* refactor between API

* refactor combine
  • Loading branch information
kbariotis authored Jan 3, 2022
1 parent f6b0d31 commit 632bc2d
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 89 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions .github/workflows/semantic-pr-title.yaml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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
30 changes: 1 addition & 29 deletions src/lib/at.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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
}`,
});
},
};
20 changes: 16 additions & 4 deletions src/lib/between.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
50 changes: 17 additions & 33 deletions src/lib/combine.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
14 changes: 7 additions & 7 deletions src/lib/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions src/lib/every.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
2 changes: 1 addition & 1 deletion src/lib/expression.spec.ts
Original file line number Diff line number Diff line change
@@ -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('* * * * *');
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/inMonth.spec.ts
Original file line number Diff line number Diff line change
@@ -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 *');
});
});
4 changes: 2 additions & 2 deletions src/lib/on.spec.ts
Original file line number Diff line number Diff line change
@@ -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 * *');
});
});

0 comments on commit 632bc2d

Please sign in to comment.