-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for 'mondays' like syntax without needing to specify a fr…
…equency or occurence token
- Loading branch information
Showing
4 changed files
with
85 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,4 +98,13 @@ in 2 weeks | |
0 9 18 * * | ||
|
||
in 5 months | ||
0 9 4 7 * | ||
0 9 4 7 * | ||
|
||
Thursdays | ||
0 9 * * 4 | ||
|
||
mon | ||
0 9 * * 1 | ||
|
||
weekdays | ||
0 9 * * 1-5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { Token } from '../tokenize'; | ||
import type { Options } from '../options'; | ||
import { rules } from './rules'; | ||
|
||
export type Crontext = { | ||
minutes: string; | ||
hour: string; | ||
dayOfMonth: string; | ||
dayOfWeek: string; | ||
month: string; | ||
repeat: boolean; | ||
}; | ||
|
||
export const DEFAULT = '*'; | ||
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 defaultParsed: Crontext = { | ||
minutes: INIT, | ||
hour: INIT, | ||
dayOfMonth: INIT, | ||
dayOfWeek: INIT, | ||
month: INIT, | ||
repeat: false, | ||
}; | ||
export const parser = (tokens: Token[], options: Options): Crontext => { | ||
let crontext = { ...defaultParsed }; | ||
// Iterate all tokens | ||
for (let t = 0; t < tokens.length; t++) { | ||
// Check if any of the rules match the given token and forward lookups. | ||
for (let r = 0; r < rules.length; r++) { | ||
const rule = rules[r]; | ||
const ruleLen = rule.match.length; | ||
if (ruleLen <= tokens.length - t) { | ||
const sub = tokens.slice(t, t + ruleLen); | ||
let isMatch = true; | ||
for (let s = 0; s < sub.length; s++) { | ||
if (sub[s].type !== rule.match[s]) { | ||
isMatch = false; | ||
} | ||
} | ||
if (isMatch) { | ||
t = t + sub.length - 1; | ||
crontext = rule.update(crontext, sub, options); | ||
} | ||
} | ||
} | ||
} | ||
return crontext; | ||
}; | ||
|
||
export default parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters