Skip to content

Commit

Permalink
Adds support for every month
Browse files Browse the repository at this point in the history
  • Loading branch information
rautio committed Oct 15, 2023
1 parent 5cb410a commit a478e8e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
4 changes: 4 additions & 0 deletions __tests__/parseText/suites/smoke.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ On Wednesday at 2:13pm
Every week
0 9 * * 1

Every month
0 9 1 * *


Every weekd
* * * * *

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crontext",
"version": "0.2.4",
"version": "0.2.5",
"description": "Simple utility for parsing human text into a cron schedule.",
"files": [
"lib"
Expand Down
49 changes: 41 additions & 8 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ export const INIT = '_'; // Used to know whether the value has been set at all.
export const DEFAULT_DAY_MINUTES = '0';
export const DEFAULT_DAY_HOURS = '9';

const { FREQUENCY, NUMBER, MINUTE, CLOCK, DAY, HOUR, DAYS, RELATIVE_DAY } =
TokenType;
const {
FREQUENCY,
OCCURRENCE,
NUMBER,
MINUTE,
CLOCK,
DAY,
HOUR,
DAYS,
RELATIVE_DAY,
} = TokenType;

const defaultParsed: Parsed = {
minutes: INIT,
Expand All @@ -29,6 +38,19 @@ const defaultParsed: Parsed = {
month: INIT,
};

export const updateDay = (
crontext: Parsed,
tokens: Token[],
options: Options,
): Parsed => {
// If there are no minutes or hour set we use defaults
// 'On monday' -> 9am Monday
if (crontext.minutes === INIT) crontext.minutes = options.defaultMinute;
if (crontext.hour === INIT) crontext.hour = options.defaultHour;
const dayOfWeek = getDayOfWeek(tokens[1].value);
return { ...crontext, dayOfWeek };
};

export const updateDays = (
crontext: Parsed,
tokens: Token[],
Expand Down Expand Up @@ -114,22 +136,33 @@ export const rules = [
},
},
{
match: [FREQUENCY, DAYS],
match: [OCCURRENCE, DAYS],
update: updateDays,
},
{
match: [FREQUENCY, NUMBER, DAYS],
match: [OCCURRENCE, NUMBER, DAYS],
update: updateDays,
},
{
match: [OCCURRENCE, DAY],
update: updateDay,
},
{
match: [FREQUENCY, DAY],
update: updateDay,
},
{
match: [FREQUENCY, DAYS],
update: (crontext: Parsed, tokens: Token[], options: Options): Parsed => {
// If there are no minutes or hour set we use defaults
// 'On monday' -> 9am Monday
if (crontext.minutes === INIT) crontext.minutes = options.defaultMinute;
if (crontext.hour === INIT) crontext.hour = options.defaultHour;
const dayOfWeek = getDayOfWeek(tokens[1].value);
return { ...crontext, dayOfWeek };
if (tokens[1].value.indexOf('month') > -1) {
crontext.dayOfMonth = '1';
}
if (tokens[1].value.indexOf('week') > -1) {
crontext.dayOfWeek = options.startOfWeek;
}
return crontext;
},
},
{
Expand Down
6 changes: 5 additions & 1 deletion src/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum TokenType {
NUMBER = 'number',
EXCLUDE = 'exclude',
FREQUENCY = 'frequency',
OCCURRENCE = 'occurrence',
DAY = 'day',
DAYS = 'days',
RELATIVE_DAY = 'relative day',
Expand Down Expand Up @@ -52,8 +53,11 @@ export const tokens = {
[TokenType.NUMBER]: {
test: `^(\\d+|${numberStringRegexOptions})$|^a$`,
},
[TokenType.OCCURRENCE]: {
test: '^(in|next)$',
},
[TokenType.FREQUENCY]: {
test: '^(every|each|every other|at|on|in|next)$',
test: '^(every|each|every other|at|on)$',
},
[TokenType.CLOCK]: {
test: /^(\d?\d:\d\d|\d)[ ]?(am|pm|AM|PM)|\d\d:\d\d|midnight|noon$/,
Expand Down

0 comments on commit a478e8e

Please sign in to comment.