Skip to content

Commit

Permalink
Add support for hours
Browse files Browse the repository at this point in the history
  • Loading branch information
rautio committed Oct 13, 2023
1 parent bbfd4f7 commit f6a82cc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
11 changes: 10 additions & 1 deletion __tests__/parseText/suites/smoke.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ Every week
0 9 * * 1

Every weekd
* * * * *
* * * * *

Every two Hours
0 */2 * * *

Every hour
0 * * * *

Every 12 hrs
0 */12 * * *
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.1",
"version": "0.2.2",
"description": "Simple utility for parsing human text into a cron schedule.",
"files": [
"lib"
Expand Down
18 changes: 17 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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 } = TokenType;
const { FREQUENCY, NUMBER, MINUTE, CLOCK, DAY, HOUR } = TokenType;

const defaultParsed: Parsed = {
minutes: INIT,
Expand Down Expand Up @@ -51,6 +51,22 @@ export const rules = [
return crontext;
},
},
{
match: [FREQUENCY, NUMBER, HOUR],
update: (crontext: Parsed, tokens: Token[]): Parsed => {
if (crontext.minutes === INIT) crontext.minutes = '0';
crontext.hour = '*/' + getNumber(tokens[1].value);
return crontext;
},
},
{
match: [FREQUENCY, HOUR],
update: (crontext: Parsed): Parsed => {
crontext.minutes = '0';
crontext.hour = DEFAULT;
return crontext;
},
},
{
match: [FREQUENCY, DAY],
update: (crontext: Parsed, tokens: Token[], options: Options): Parsed => {
Expand Down
16 changes: 16 additions & 0 deletions src/tokens/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ describe('Number token should', () => {
expect(re.test('q8a03')).toBe(false);
});
});

describe('Hour token should', () => {
const re = new RegExp(tokens[TokenType.HOUR].test);
test('match on different formats', () => {
expect(re.test('hours')).toBe(true);
expect(re.test('hour')).toBe(true);
expect(re.test('hrs')).toBe(true);
expect(re.test('hr')).toBe(true);
});
test('not match in incorrect format', () => {
expect(re.test('h')).toBe(false);
expect(re.test('hors')).toBe(false);
expect(re.test('house')).toBe(false);
expect(re.test('hers')).toBe(false);
});
});
4 changes: 4 additions & 0 deletions src/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type TokenDef = {

export enum TokenType {
MINUTE = 'minutes',
HOUR = 'hours',
NUMBER = 'number',
EXCLUDE = 'exclude',
FREQUENCY = 'frequency',
Expand Down Expand Up @@ -37,6 +38,9 @@ export const tokens = {
[TokenType.MINUTE]: {
test: '^(minutes|minute|mins|min)$',
},
[TokenType.HOUR]: {
test: '^(hours|hour|hrs|hr)$',
},
[TokenType.NUMBER]: {
test: `^(\\d+|${numberStringRegexOptions})$`,
},
Expand Down

0 comments on commit f6a82cc

Please sign in to comment.