Skip to content

Commit

Permalink
feat: [435] add support for the 'L' mark
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkrum committed Apr 9, 2024
1 parent e9f8b78 commit 2d3daae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const ALIASES = Object.freeze({
wed: 3,
thu: 4,
fri: 5,
sat: 6
sat: 6,
l: 'l'
} as const);
export const TIME_UNITS_MAP = Object.freeze({
SECOND: 'second',
Expand Down Expand Up @@ -80,3 +81,4 @@ export const PRESETS = Object.freeze({
export const RE_WILDCARDS = /\*/g;
export const RE_RANGE = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g;
export const RE_QUESTIONMARK = /\?/g;
export const RE_L = /[lL]/g;
9 changes: 9 additions & 0 deletions src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MONTH_CONSTRAINTS,
PARSE_DEFAULTS,
PRESETS,
RE_L,
RE_QUESTIONMARK,
RE_RANGE,
RE_WILDCARDS,
Expand Down Expand Up @@ -766,6 +767,9 @@ export class CronTime {
}
});

// "L" is a shortcut for the last day of the month
value = value.replace(RE_L, this._getLastDayOf(now, unit));

// "?" will be replaced with current timestamp value
value = value.replace(RE_QUESTIONMARK, this._getTimeUnit(now, unit));

Expand Down Expand Up @@ -852,4 +856,9 @@ export class CronTime {
throw new CronError('Invalid time unit');
}
}

private _getLastDayOf(now: DateTime, unit: TimeUnit) {
if (unit === 'dayOfMonth') return now.endOf('month').day.toString();
return '7';
}
}
37 changes: 23 additions & 14 deletions tests/crontime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,20 +350,6 @@ describe('crontime', () => {
});
});

describe('should throw an exception because `L` not supported', () => {
it('(* * * L * *)', () => {
expect(() => {
new CronTime('* * * L * *');
}).toThrow();
});

it('(* * * * * L)', () => {
expect(() => {
new CronTime('* * * * * L');
}).toThrow();
});
});

it('should strip off millisecond', () => {
const cronTime = new CronTime('0 */10 * * * *');
const x = cronTime.getNextDateFrom(new Date('2018-08-10T02:20:00.999Z'));
Expand Down Expand Up @@ -843,4 +829,27 @@ describe('crontime', () => {
clock.restore();
});
});

describe("should support 'L' for last day of the month", () => {
it('should substitute last day of the month for "* * L * *"', () => {
const clock = sinon.useFakeTimers();

const now = DateTime.local();
const ct = new CronTime('* * L * *');
const day = ct.sendAt().get('day');
expect(day).toBe(now.endOf('month').get('day'));

clock.restore();
});

it('should substitute last day of the week for "* * * * * L', () => {
const clock = sinon.useFakeTimers();

const ct = new CronTime('* * * * L');
const day = ct.sendAt().get('weekday');
expect(day).toBe(7);

clock.restore();
});
});
});

0 comments on commit 2d3daae

Please sign in to comment.