Skip to content

Commit

Permalink
fix eslint issues & replace regex by string in tests. (#4)
Browse files Browse the repository at this point in the history
* fix eslint issues & replace regex by string in tests.

* update test.yaml file.
  • Loading branch information
satyajitnayk committed Nov 5, 2023
1 parent a45a38f commit 20beae1
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 117 deletions.
49 changes: 16 additions & 33 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
name: Publish Package to npmjs

on:
release:
types:
- published
pull_request:
branches: ['**']
types: [synchronize, opened, reopened, ready_for_review]
push:
branches: [main]

jobs:
build:
lint:
runs-on: ubuntu-latest

if: github.event.pull_request.draft == false # run this job only if the PR is not in a draft state
strategy:
matrix:
node: [ '18' ]
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18

- name: Update version
run: |
# update version in package.json with release version
sed -i 's/"version": "[0-9]*\.[0-9]*\.[0-9]*"/"version": "'$VERSION'"/g' package.json
env:
VERSION: ${{ github.event.release.tag_name }}

- name: Install dependencies
run: npm ci

node-version: ${{ matrix.node }}
- run: npm i
- name: lint
run: ./node_modules/.bin/prettier --check **/*.ts

- run: tsc

- run: npm run test

- name: Build
run: npm run build

- name: Publish to npm
run: npm publish
- name: test
run: npm run test
32 changes: 21 additions & 11 deletions src/cron-expression-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {Schedule, ScheduleUnit, CronTimeUnit, ScheduleValue} from "./interfaces";
import {CronValidators} from "./cron-validators";
import {CronUtils} from "./cron-utils";
import {
Schedule,
ScheduleUnit,
CronTimeUnit,
ScheduleValue,
} from './interfaces';
import { CronValidators } from './cron-validators';
import { CronUtils } from './cron-utils';

/**
* Class to construct a cron expression with a fluent API.
Expand Down Expand Up @@ -29,14 +34,13 @@ export class CronExpressionBuilder {
CronUtils.setDefault(this.schedule, 'dayOfWeek', '*');
}


/**
* Sets the job to run at specific minutes.
* @param {Array<number>} minutes - Minute(s) at which the job should run.
* @returns {this} The instance of CronExpressionBuilder for chaining.
*/
atMinutes(minutes: Array<number>): this {
minutes.forEach(minute => CronValidators.validateMinute(minute));
minutes.forEach((minute) => CronValidators.validateMinute(minute));
this.schedule.minute = CronUtils.formatCronPart(minutes);
return this;
}
Expand All @@ -47,7 +51,7 @@ export class CronExpressionBuilder {
* @returns {this} The instance of CronExpressionBuilder for chaining.
*/
atHours(hours: number[]): this {
hours.forEach(hour => CronValidators.validateHour(hour));
hours.forEach((hour) => CronValidators.validateHour(hour));
this.schedule.hour = CronUtils.formatCronPart(hours);
// Set the minutes to '0' if they haven't been defined yet
if (this.schedule.minute === undefined) {
Expand Down Expand Up @@ -181,7 +185,7 @@ export class CronExpressionBuilder {
* @param {Array<number>} days - Day(s) of the week.
*/
onWeekDays(days: Array<number>): this {
days.forEach(day => CronValidators.validateDayOfWeek(day));
days.forEach((day) => CronValidators.validateDayOfWeek(day));
this.schedule.dayOfWeek = CronUtils.formatCronPart(days);
return this;
}
Expand All @@ -192,7 +196,7 @@ export class CronExpressionBuilder {
* @returns {this} The instance of CronExpressionBuilder for chaining.
*/
onDaysOfMonth(days: Array<number>): this {
days.forEach(day => CronValidators.validateDayOfMonth(day));
days.forEach((day) => CronValidators.validateDayOfMonth(day));
this.schedule.dayOfMonth = CronUtils.formatCronPart(days);
return this;
}
Expand All @@ -203,7 +207,7 @@ export class CronExpressionBuilder {
* @returns {this} The instance of CronExpressionBuilder for chaining.
*/
duringMonths(months: Array<number>): this {
months.forEach(month => CronValidators.validateMonth(month));
months.forEach((month) => CronValidators.validateMonth(month));
this.schedule.month = CronUtils.formatCronPart(months);
return this;
}
Expand All @@ -226,9 +230,15 @@ export class CronExpressionBuilder {
// Construct the cron expression using the defined schedule parts
const minute = this.getCronPart('minute', this.schedule.minute || '*');
const hour = this.getCronPart('hour', this.schedule.hour || '*');
const dayOfMonth = this.getCronPart('dayOfMonth', this.schedule.dayOfMonth || '*');
const dayOfMonth = this.getCronPart(
'dayOfMonth',
this.schedule.dayOfMonth || '*',
);
const month = this.getCronPart('month', this.schedule.month || '*');
const dayOfWeek = this.getCronPart('dayOfWeek', this.schedule.dayOfWeek || '*');
const dayOfWeek = this.getCronPart(
'dayOfWeek',
this.schedule.dayOfWeek || '*',
);

// Assemble the cron expression
return `${minute} ${hour} ${dayOfMonth} ${month} ${dayOfWeek}`;
Expand Down
8 changes: 6 additions & 2 deletions src/cron-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Schedule} from "./interfaces";
import { Schedule } from './interfaces';

export class CronUtils {
/**
Expand Down Expand Up @@ -41,7 +41,11 @@ export class CronUtils {
* @param {string} field - The key to check in the schedule.
* @param {string | number} defaultValue - The default value to set if the key is undefined.
*/
static setDefault(schedule: Schedule, field: string, defaultValue: string | number): void {
static setDefault(
schedule: Schedule,
field: string,
defaultValue: string | number,
): void {
if (schedule[field] === undefined) {
schedule[field] = defaultValue;
}
Expand Down
20 changes: 15 additions & 5 deletions src/cron-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export class CronValidators {
*/
static validateMinute(minute: number): void {
if (!Number.isInteger(minute) || minute < 0 || minute > 59) {
throw new Error(`Invalid minute: ${minute}. Minute should be between 0 and 59.`);
throw new Error(
`Invalid minute: ${minute}. Minute should be between 0 and 59.`,
);
}
}

Expand All @@ -17,7 +19,9 @@ export class CronValidators {
*/
static validateHour(hour: number): void {
if (!Number.isInteger(hour) || hour < 0 || hour > 23) {
throw new Error(`Invalid hour: ${hour}. Hour should be between 0 and 23.`);
throw new Error(
`Invalid hour: ${hour}. Hour should be between 0 and 23.`,
);
}
}

Expand All @@ -28,7 +32,9 @@ export class CronValidators {
*/
static validateDayOfMonth(day: number): void {
if (!Number.isInteger(day) || day < 1 || day > 31) {
throw new Error(`Invalid day of month: ${day}. Day should be between 1 and 31.`);
throw new Error(
`Invalid day of month: ${day}. Day should be between 1 and 31.`,
);
}
}

Expand All @@ -39,7 +45,9 @@ export class CronValidators {
*/
static validateMonth(month: number): void {
if (!Number.isInteger(month) || month < 1 || month > 12) {
throw new Error(`Invalid month: ${month}. Month should be between 1 and 12.`);
throw new Error(
`Invalid month: ${month}. Month should be between 1 and 12.`,
);
}
}

Expand All @@ -50,7 +58,9 @@ export class CronValidators {
*/
static validateDayOfWeek(day: number): void {
if (!Number.isInteger(day) || day < 0 || day > 6) {
throw new Error(`Invalid day of week: ${day}. Day should be between 0 (Sunday) and 6 (Saturday).`);
throw new Error(
`Invalid day of week: ${day}. Day should be between 0 (Sunday) and 6 (Saturday).`,
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { CronExpressionBuilder} from './cron-expression-builder';
export { CronValidators} from './cron-validators';
export { CronExpressionBuilder } from './cron-expression-builder';
export { CronValidators } from './cron-validators';
9 changes: 7 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export type ScheduleUnit = 'minute' | 'hour' | 'dayOfMonth' | 'month' | 'dayOfWeek' ;
export type ScheduleUnit =
| 'minute'
| 'hour'
| 'dayOfMonth'
| 'month'
| 'dayOfWeek';
export type ScheduleValue = string | number;

export interface Schedule {
Expand All @@ -15,5 +20,5 @@ export enum CronTimeUnit {
Hour = 'hour',
DayOfMonth = 'dayOfMonth',
Month = 'month',
DayOfWeek = 'dayOfWeek'
DayOfWeek = 'dayOfWeek',
}
91 changes: 71 additions & 20 deletions test/cron-expression-builder.full.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CronExpressionBuilder} from '../src';
import {CronTimeUnit} from "../src/interfaces";
import { CronExpressionBuilder } from '../src';
import { CronTimeUnit } from '../src/interfaces';

describe('CronExpressionBuilder - FULL', () => {
let cronExpBuilder: CronExpressionBuilder;
Expand All @@ -9,74 +9,125 @@ describe('CronExpressionBuilder - FULL', () => {
});

test('Run a job at 5:30 PM every day', () => {
expect(cronExpBuilder.atTime('17:30').compile()).toStrictEqual(`30 17 * * *`)
expect(cronExpBuilder.atTime('17:30').compile()).toStrictEqual(
`30 17 * * *`,
);
});

test('Run at 9 AM on weekdays', () => {
expect(cronExpBuilder.atTime('09:00').onWeekDays([1,2,3,4,5]).compile()).toStrictEqual(`0 9 * * 1-5`)
expect(
cronExpBuilder.atTime('09:00').onWeekDays([1, 2, 3, 4, 5]).compile(),
).toStrictEqual(`0 9 * * 1-5`);
});

test('Run at noon on the 1st and 15th of the month', () => {
expect(cronExpBuilder.atTime('12:00').onDaysOfMonth([1, 15]).compile()).toStrictEqual(`0 12 1,15 * *`)
expect(
cronExpBuilder.atTime('12:00').onDaysOfMonth([1, 15]).compile(),
).toStrictEqual(`0 12 1,15 * *`);
});

test('Run at midnight during January and July', () => {
expect(cronExpBuilder.atTime('00:00').duringMonths([1, 7]).compile()).toStrictEqual(`0 0 * 1,7 *`)
expect(
cronExpBuilder.atTime('00:00').duringMonths([1, 7]).compile(),
).toStrictEqual(`0 0 * 1,7 *`);
});

test('Run every 15 minutes', () => {
expect(cronExpBuilder.everyX(15, CronTimeUnit.Minute).compile()).toStrictEqual(`*/15 * * * *`)
expect(
cronExpBuilder.everyX(15, CronTimeUnit.Minute).compile(),
).toStrictEqual(`*/15 * * * *`);
});

test('Run every day at noon', () => {
expect(cronExpBuilder.every('day').atHours([12]).compile()).toStrictEqual(`0 12 * * *`)
expect(cronExpBuilder.every('day').atHours([12]).compile()).toStrictEqual(
`0 12 * * *`,
);
});

test('Run every Sunday at 5 PM', () => {
expect(cronExpBuilder.onWeekDays([0]).atHours([17]).compile()).toStrictEqual(`0 17 * * 0`)
expect(
cronExpBuilder.onWeekDays([0]).atHours([17]).compile(),
).toStrictEqual(`0 17 * * 0`);
});

test('Run the 1st day of every month at 1 AM', () => {
expect(cronExpBuilder.onDaysOfMonth([1]).every('month').atHours([1]).compile()).toStrictEqual(`0 1 1 * *`)
expect(
cronExpBuilder.onDaysOfMonth([1]).every('month').atHours([1]).compile(),
).toStrictEqual(`0 1 1 * *`);
});

test('Run every weekday at 8:30 AM', () => {
expect(cronExpBuilder.atTime("08:30").onWeekDays([1, 2, 3, 4, 5]).compile()).toStrictEqual(`30 8 * * 1-5`)
expect(
cronExpBuilder.atTime('08:30').onWeekDays([1, 2, 3, 4, 5]).compile(),
).toStrictEqual(`30 8 * * 1-5`);
});

test('Run every 6 hours', () => {
expect(cronExpBuilder.everyX(6,CronTimeUnit.Hour).compile()).toStrictEqual(`0 */6 * * *`)
expect(cronExpBuilder.everyX(6, CronTimeUnit.Hour).compile()).toStrictEqual(
`0 */6 * * *`,
);
});

test('Run every quarter at midnight', () => {
expect(cronExpBuilder.duringMonths([1, 4, 7, 10]).onDaysOfMonth([1]).atTime("00:00").compile()).toStrictEqual(`0 0 1 1,4,7,10 *`)
expect(
cronExpBuilder
.duringMonths([1, 4, 7, 10])
.onDaysOfMonth([1])
.atTime('00:00')
.compile(),
).toStrictEqual(`0 0 1 1,4,7,10 *`);
});

test('Run every Saturday and Sunday at 10:15 AM', () => {
expect(cronExpBuilder.atTime("10:15").onWeekDays([6, 0]).compile()).toStrictEqual(`15 10 * * 0,6`)
expect(
cronExpBuilder.atTime('10:15').onWeekDays([6, 0]).compile(),
).toStrictEqual(`15 10 * * 0,6`);
});

test('Run at 9 AM, 12 PM, and 3 PM every day', () => {
expect(cronExpBuilder.every('day').atHours([9, 12, 15]).compile()).toStrictEqual(`0 9,12,15 * * *`)
expect(
cronExpBuilder.every('day').atHours([9, 12, 15]).compile(),
).toStrictEqual(`0 9,12,15 * * *`);
});

test('Run at 7 AM, 2 PM, and 10 PM on Tuesdays', () => {
expect(cronExpBuilder.atHours([7, 14, 22]).onWeekDays([2]).compile()).toStrictEqual(`0 7,14,22 * * 2`)
expect(
cronExpBuilder.atHours([7, 14, 22]).onWeekDays([2]).compile(),
).toStrictEqual(`0 7,14,22 * * 2`);
});

test('Run at 20 past every hour on the 5th of July', () => {
expect(cronExpBuilder.atMinutes([20]).onDaysOfMonth([5]).duringMonths([7]).compile()).toStrictEqual(`20 * 5 7 *`)
expect(
cronExpBuilder
.atMinutes([20])
.onDaysOfMonth([5])
.duringMonths([7])
.compile(),
).toStrictEqual(`20 * 5 7 *`);
});

test('Run every 5 minutes during office hours - using every()', () => {
expect(cronExpBuilder.every('minute').atMinutes([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]).atHours([9, 10, 11, 12, 13, 14, 15, 16]).compile()).toStrictEqual(`0,5,10,15,20,25,30,35,40,45,50,55 9-16 * * *`)
expect(
cronExpBuilder
.every('minute')
.atMinutes([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55])
.atHours([9, 10, 11, 12, 13, 14, 15, 16])
.compile(),
).toStrictEqual(`0,5,10,15,20,25,30,35,40,45,50,55 9-16 * * *`);
});

test('Run every 5 minutes during office hours - using everyX()', () => {
expect(cronExpBuilder.everyX(5,CronTimeUnit.Minute).atHours([9, 10, 11, 12, 13, 14, 15, 16]).compile()).toStrictEqual(`*/5 9-16 * * *`)
expect(
cronExpBuilder
.everyX(5, CronTimeUnit.Minute)
.atHours([9, 10, 11, 12, 13, 14, 15, 16])
.compile(),
).toStrictEqual(`*/5 9-16 * * *`);
});

test('Run at quarter past and quarter to every hour', () => {
expect(cronExpBuilder.every('hour').atMinutes([15, 45]).compile()).toStrictEqual(`15,45 * * * *`)
expect(
cronExpBuilder.every('hour').atMinutes([15, 45]).compile(),
).toStrictEqual(`15,45 * * * *`);
});
});
Loading

0 comments on commit 20beae1

Please sign in to comment.