From f6a82cc662bd26d0ed143ed2a3fef3660f665d22 Mon Sep 17 00:00:00 2001 From: Oskari Rautiainen Date: Fri, 13 Oct 2023 14:47:31 -0700 Subject: [PATCH] Add support for hours --- __tests__/parseText/suites/smoke.txt | 11 ++++++++++- package.json | 2 +- src/parser.ts | 18 +++++++++++++++++- src/tokens/index.test.ts | 16 ++++++++++++++++ src/tokens/index.ts | 4 ++++ 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/__tests__/parseText/suites/smoke.txt b/__tests__/parseText/suites/smoke.txt index 0e2aad6..9782d6a 100644 --- a/__tests__/parseText/suites/smoke.txt +++ b/__tests__/parseText/suites/smoke.txt @@ -44,4 +44,13 @@ Every week 0 9 * * 1 Every weekd -* * * * * \ No newline at end of file +* * * * * + +Every two Hours +0 */2 * * * + +Every hour +0 * * * * + +Every 12 hrs +0 */12 * * * \ No newline at end of file diff --git a/package.json b/package.json index 6fd4f88..99767dd 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/parser.ts b/src/parser.ts index e92e6ca..6d20971 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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, @@ -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 => { diff --git a/src/tokens/index.test.ts b/src/tokens/index.test.ts index 68aa812..4053659 100644 --- a/src/tokens/index.test.ts +++ b/src/tokens/index.test.ts @@ -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); + }); +}); diff --git a/src/tokens/index.ts b/src/tokens/index.ts index 6812caf..05d7202 100644 --- a/src/tokens/index.ts +++ b/src/tokens/index.ts @@ -7,6 +7,7 @@ export type TokenDef = { export enum TokenType { MINUTE = 'minutes', + HOUR = 'hours', NUMBER = 'number', EXCLUDE = 'exclude', FREQUENCY = 'frequency', @@ -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})$`, },