Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cron): add toString() method #783

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/cron/src/lib/Cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ export class Cron {
return this.next(new Date(outset.getTime() + Time.Day), false);
}

/**
* Returns the string that represents this cron pattern
*
* @returns The string that represents this cron pattern
*/
public toString() {
return this.cron;
}

/**
* Normalize the pattern
* @param cron The pattern to normalize
Expand Down
66 changes: 22 additions & 44 deletions packages/cron/tests/lib/Cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { Cron } from '../../src';

describe('Cron', () => {
test('pre-defined @hourly', () => {
expect.assertions(6);

const specimen = new Cron('@hourly');

expect(specimen.normalized).toBe('0 * * * *');
Expand All @@ -14,11 +12,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('@hourly');
});

test('pre-defined @daily', () => {
expect.assertions(6);

const specimen = new Cron('@daily');

expect(specimen.normalized).toBe('0 0 * * *');
Expand All @@ -29,11 +26,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('@daily');
});

test('pre-defined @weekly', () => {
expect.assertions(6);

const specimen = new Cron('@weekly');

expect(specimen.normalized).toBe('0 0 * * 0');
Expand All @@ -44,11 +40,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0]);
expect(specimen.toString()).toBe('@weekly');
});

test('pre-defined @monthly', () => {
expect.assertions(6);

const specimen = new Cron('@monthly');

expect(specimen.normalized).toBe('0 0 1 * *');
Expand All @@ -57,11 +52,10 @@ describe('Cron', () => {
expect(specimen.days).toEqual([1]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('@monthly');
});

test('pre-defined @yearly', () => {
expect.assertions(6);

const specimen = new Cron('@yearly');

expect(specimen.normalized).toBe('0 0 1 1 *');
Expand All @@ -70,11 +64,10 @@ describe('Cron', () => {
expect(specimen.days).toEqual([1]);
expect(specimen.months).toEqual([1]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('@yearly');
});

test('pre-defined @annually', () => {
expect.assertions(6);

const specimen = new Cron('@annually');

expect(specimen.normalized).toBe('0 0 1 1 *');
Expand All @@ -83,11 +76,10 @@ describe('Cron', () => {
expect(specimen.days).toEqual([1]);
expect(specimen.months).toEqual([1]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('@annually');
});

test('every minute', () => {
expect.assertions(6);

const specimen = new Cron('* * * * *');

expect(specimen.normalized).toBe('* * * * *');
Expand All @@ -101,11 +93,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* * * * *');
});

test('every other minute', () => {
expect.assertions(6);

const specimen = new Cron('*/2 * * * *');

expect(specimen.normalized).toBe('*/2 * * * *');
Expand All @@ -118,11 +109,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('*/2 * * * *');
});

test('every seventh minute starting at the second', () => {
expect.assertions(6);

const specimen = new Cron('1/7 * * * *');

expect(specimen.normalized).toBe('1/7 * * * *');
Expand All @@ -133,11 +123,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('1/7 * * * *');
});

test('every other hour', () => {
expect.assertions(6);

const specimen = new Cron('* */2 * * *');

expect(specimen.normalized).toBe('* */2 * * *');
Expand All @@ -151,11 +140,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* */2 * * *');
});

test('every seventh hour starting at the second', () => {
expect.assertions(6);

const specimen = new Cron('* 1/7 * * *');

expect(specimen.normalized).toBe('* 1/7 * * *');
Expand All @@ -169,11 +157,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* 1/7 * * *');
});

test('every other day', () => {
expect.assertions(6);

const specimen = new Cron('* * */2 * *');

expect(specimen.normalized).toBe('* * */2 * *');
Expand All @@ -185,11 +172,10 @@ describe('Cron', () => {
expect(specimen.days).toEqual([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* * */2 * *');
});

test('every seventh day starting at the second', () => {
expect.assertions(6);

const specimen = new Cron('* * 1/7 * *');

expect(specimen.normalized).toBe('* * 1/7 * *');
Expand All @@ -201,11 +187,10 @@ describe('Cron', () => {
expect(specimen.days).toEqual([1, 8, 15, 22, 29]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* * 1/7 * *');
});

test('every other month', () => {
expect.assertions(6);

const specimen = new Cron('* * * */2 *');

expect(specimen.normalized).toBe('* * * */2 *');
Expand All @@ -219,11 +204,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 3, 5, 7, 9, 11]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* * * */2 *');
});

test('every seventh month starting at the second', () => {
expect.assertions(6);

const specimen = new Cron('* * * 1/7 *');

expect(specimen.normalized).toBe('* * * 1/7 *');
Expand All @@ -237,11 +221,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 8]);
expect(specimen.dows).toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(specimen.toString()).toBe('* * * 1/7 *');
});

test('every other day of week', () => {
expect.assertions(6);

const specimen = new Cron('* * * * */2');

expect(specimen.normalized).toBe('* * * * */2');
Expand All @@ -255,11 +238,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([0, 2, 4, 6]);
expect(specimen.toString()).toBe('* * * * */2');
});

test('every other day of week starting at the second', () => {
expect.assertions(6);

const specimen = new Cron('* * * * 1/2');

expect(specimen.normalized).toBe('* * * * 1/2');
Expand All @@ -273,11 +255,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([1, 3, 5]);
expect(specimen.toString()).toBe('* * * * 1/2');
});

test('every monday through friday token at midnight', () => {
expect.assertions(6);

const specimen = new Cron('0 0 * * mon-fri');

expect(specimen.normalized).toBe('0 0 * * 1-5');
Expand All @@ -288,11 +269,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
expect(specimen.dows).toEqual([1, 2, 3, 4, 5]);
expect(specimen.toString()).toBe('0 0 * * mon-fri');
});

test('every friday in july at midnight', () => {
expect.assertions(6);

const specimen = new Cron('0 0 * jul fri');

expect(specimen.normalized).toBe('0 0 * 7 5');
Expand All @@ -303,11 +283,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([7]);
expect(specimen.dows).toEqual([5]);
expect(specimen.toString()).toBe('0 0 * jul fri');
});

test('every friday in july,august at midnight', () => {
expect.assertions(6);

const specimen = new Cron('0 0 * jul,aug fri');

expect(specimen.normalized).toBe('0 0 * 7,8 5');
Expand All @@ -318,11 +297,10 @@ describe('Cron', () => {
]);
expect(specimen.months).toEqual([7, 8]);
expect(specimen.dows).toEqual([5]);
expect(specimen.toString()).toBe('0 0 * jul,aug fri');
});

test('this instant', () => {
expect.assertions(6);

const now = new Date();
const [min, hour, day, month, dow] = [now.getUTCMinutes(), now.getUTCHours(), now.getUTCDate(), now.getUTCMonth(), now.getUTCDay()];
const specimen = new Cron('? ? ? ? ?');
Expand All @@ -333,11 +311,10 @@ describe('Cron', () => {
expect(specimen.days).toEqual([day]);
expect(specimen.months).toEqual([month]);
expect(specimen.dows).toEqual([dow]);
expect(specimen.toString()).toBe('? ? ? ? ?');
});

test('any instant', () => {
expect.assertions(5);

const specimen = new Cron('h h h h h');
const [minutes, hours, date, month, day] = specimen.normalized.split(' ').map(Number);

Expand All @@ -346,6 +323,7 @@ describe('Cron', () => {
expect(date >= 1 && date <= 31).toBe(true);
expect(month >= 1 && month <= 12).toBe(true);
expect(day >= 0 && day <= 6).toBe(true);
expect(specimen.toString()).toBe('h h h h h');
});

// Saturday 9th March 2019, at 16:20:35:500
Expand Down