Cron Reference
⏲️ Standard cron expression tools.
Usage
📦 Node
Install @lou.codes/cron
as a dependency:
Import it and use it:
🦕 Deno
Import @lou.codes/cron
using the npm:
prefix, and use it directly:
🌎 Browser
Import @lou.codes/cron
using esm.sh, and use it directly:
Useful links
- 📝 Documentation: TypeDoc generated documentation.
- ⏳ Changelog: List of changes between versions.
- ✅ Tests Coverage: Coveralls page with tests coverage.
To do
Soon a human readable parser will be added, so we can do stuff like:
Cron Object
CronObject
Object that represents the 5 cron expression fields.
See
Type declaration
Member | Type |
---|---|
dayOfMonth | Field <DayOfMonth > |
dayOfWeek | Field <DayOfWeek > |
hour | Field <Hours > |
minute | Field <Minutes > |
month | Field <MonthValue > |
Field<Value>
Union of AllToken, ValueOrRangeField and ListField that represents a field in a cron expression.
See
Type parameters
Type parameter |
---|
Value extends number |
ListField<Value>
Type that represents a list of values for a cron object field.
See
Type parameters
Type parameter |
---|
Value extends number |
MonthValue
Type that represents values from 1
to 12
for the month cron field.
RangeField<Value>
Type that represents a range of values for a cron object field.
Type parameters
Type parameter |
---|
Value extends number |
Type declaration
Member | Type | Description |
---|---|---|
from | Value | Start of the range, must be lower than to . |
to | Value | End of the range, must be higher than from . |
ValueOrRangeField<Value>
Union of a set of numbers and a RangeField with that same set of numbers.
Type parameters
Type parameter |
---|
Value extends number |
ALL_TOKEN
Token that represents “all” in a cron expression.
DAY_OF_MONTH_NAME
Name of a day of month field in the object representation of a cron expression.
DAY_OF_WEEK_NAME
Name of a day of week field in the object representation of a cron expression.
FROM_NAME
Range field from
name.
HOUR_NAME
Name of a hour field in the object representation of a cron expression.
MINUTE_NAME
Name of a minute field in the object representation of a cron expression.
MONTH_NAME
Name of a month field in the object representation of a cron expression.
TO_NAME
Range field to
name.
stringify()
Takes a cron object and returns a sting expression.
Parameters
Parameter | Type | Description |
---|---|---|
cron | Partial <CronObject > | Cron object. |
Returns
undefined
| `${string} ${string} ${string} ${string} ${string}`
Cron string expression.
Example
See
stringifyField()
Takes a cron object and returns a string expression.
Parameters
Parameter | Type | Description |
---|---|---|
field | Field <number > | Cron object field. |
Returns
Cron string field.
Example
See
stringifyList()
Turns cron list into a string.
Parameters
Parameter | Type | Description |
---|---|---|
field | Readonly <Field <number >> | List cron object field |
Returns
Maybe
<`${string},${number}` |
`${string},${number}-${number}`>
String list or undefined
if it isn’t a list.
Example
See
stringifyRange()
Turn a cron range into a string.
Type parameters
Type parameter |
---|
Predicated extends number |
Parameters
Parameter | Type | Description |
---|---|---|
field | Readonly <Field <Predicated >> | Cron field to turn into a string. |
Returns
Maybe
<`${number}-${number}`>
String ranged of undefined
if it isn’t a range object.
Example
See
Cron String
CronString
String cron expression.
FieldString
Union of AllToken, ValueOrRangeString and ListString that represents a field in a cron expression.
See
ListString
Type that represents a list of values for a cron string field.
See
RangeString
Type that represents a range of values for a cron string field.
ValueOrRangeString
Union of a set any number and a RangeString.
ALL_TOKEN
Token that represents “all” in a cron expression.
LIST_EXPRESSION_SEPARATOR_TOKEN
Token to separate list items in a cron expression.
RANGE_EXPRESSION_SEPARATOR_TOKEN
Token to separate range values in a cron expression.
normalizeMap
Map from 3 letter aliases to their respective number representations.
Type declaration
Member | Type | Value | Description |
---|---|---|---|
apr | 4 | 4 | April number representation |
aug | 8 | 8 | August number representation |
dec | 12 | 12 | December number representation |
feb | 2 | 2 | February number representation |
fri | 5 | 5 | Friday number representation |
jan | 1 | 1 | January number representation |
jul | 7 | 7 | July number representation |
jun | 6 | 6 | June number representation |
mar | 3 | 3 | March number representation |
may | 5 | 5 | May number representation |
mon | 1 | 1 | Monday number representation |
nov | 11 | 11 | November number representation |
oct | 10 | 10 | October number representation |
sat | 6 | 6 | Saturday number representation |
sep | 9 | 9 | September number representation |
sun | 0 | 0 | Sunday number representation |
thu | 4 | 4 | Thursday number representation |
tue | 2 | 2 | Tuesday number representation |
wed | 3 | 3 | Wednesday number representation |
isValidExpression()
Validates if a string is a cron expression.
Parameters
Parameter | Type |
---|---|
string | string |
Returns
string is `${string} ${string} ${string} ${string} ${string}`
See
normalizeAliases()
Normalizes day and month 3 letter aliases into their number counterparts.
Parameters
Parameter | Type | Description |
---|---|---|
expression | string | String expression. |
Returns
Normalized expression
Example
parse()
Parses a cron expression into an object representation.
Parameters
Parameter | Type | Description |
---|---|---|
expression | `${string} ${string} ${string} ${string} ${string}` | Cron expression to be parsed. |
Returns
Maybe
<CronObject
>
Object representing that expression or undefined
if expression is invalid.
Example
See
parseField()
Parses a cron field.
Parameters
Parameter | Type | Description |
---|---|---|
field | string | Cron field value (should be validated before this). |
Returns
"*"
| RangeField
<number
>
| ListField
<number
> |
Maybe
<number
>
Parsed field.
Example
See
parseFieldTuplesMap()
Given an iterable of tuples with the name of a field and a field value, run each field through parseField.
Parameters
Parameter | Type |
---|---|
iterable | Readonly <Iterable <readonly [keyof CronObject , string ]>> |
Returns
Readonly
<IterableIterator
<readonly [ | "minute"
| "hour"
|
"dayOfMonth"
| "month"
| "dayOfWeek"
, "*"
|
RangeField
<number
> |
ListField
<number
> |
Maybe
<number
>]>>
Example
See
parseList()
Parses a cron list into an array.
Type parameters
Type parameter |
---|
Predicated extends number |
Parameters
Parameter | Type | Description |
---|---|---|
value | string | String that might be a list. |
Returns
Maybe
<ListField
<Predicated
>>
Parsed list of undefined
if it isn’t a list string.
Example
See
parseListMap()
Map to parse cron list items (can be either a range or a number).
Parameters
Parameter | Type |
---|---|
iterable | Readonly <Iterable <string >> |
Returns
Readonly
<IterableIterator
<RangeField
<number
>
| Maybe
<number
>>>
Example
See
parseNumber()
Parses a cron list into an array.
Type parameters
Type parameter |
---|
Predicated extends number |
Parameters
Parameter | Type | Description |
---|---|---|
value | string | String that might be a list. |
Returns
Maybe
<Predicated
>
Parsed list of undefined
if it isn’t a list string.
Example
parseNumberMap()
Maps given iterable through parseNumber.
Type parameters
Type parameter |
---|
Predicated extends number |
Parameters
Parameter | Type |
---|---|
iterable | Readonly <Iterable <string >> |
Returns
Readonly
<IterableIterator
<Maybe
<Predicated
>>>
Example
See
parseNumberMatch()
Matches only valid number values for a cron expression (from 0
or 00
to
59
).
Parameters
Parameter | Type |
---|---|
text | string |
Returns
boolean
Example
See
parseRange()
Parses a cron range into an object.
Type parameters
Type parameter |
---|
Predicated extends number |
Parameters
Parameter | Type | Description |
---|---|---|
value | string | String that might be a range. |
Returns
Maybe
<Predicated
|
RangeField
<Predicated
>>
Parsed ranged of undefined
if it isn’t a range string.
Example
See
Internal
fieldNamesTuple
Field names in order.
compareField()
Checks if given value is included in given field.
Parameters
Parameter | Type | Description |
---|---|---|
value | number | Value to compare. |
field | ValueOrRangeField <number > | ListField <number > | Field to compare. |
Returns
boolean
true
if value is included in the given field
, false
if it isn’t.
Example
compareRangeOrValue()
Compares value
to a
ValueOrRangeField.
Parameters
Parameter | Type | Description |
---|---|---|
value | number | Value to be compared. |
Returns
Function
Curried function expecting a ValueOrRangeField.
Parameters
Parameter | Type |
---|---|
valueOrRange | ValueOrRangeField <number > |
Returns
boolean
Example
dateInCron()
Check if given cron expression object includes given date.
Parameters
Parameter | Type | Description |
---|---|---|
cron | CronObject | Cron object. |
Returns
Function
Curried function with cron
in context.
Parameters
Parameter | Type |
---|---|
date | Readonly <Date > |
Returns
boolean
zipRangeNames()
Zips “from” and “to”.
Type parameters
Type parameter |
---|
ItemSecond |
Parameters
Parameter | Type |
---|---|
iterableSecond | Readonly <Iterable <ItemSecond >> |
Returns
Readonly
<IterableIterator
<readonly ["from"
| "to"
, ItemSecond
]>>
Other
parseDecimalMap()
Type parameters
Type parameter |
---|
Predicated extends number |
Parameters
Parameter | Type |
---|---|
iterable | Readonly <Iterable <string >> |
Returns
Readonly
<IterableIterator
<Maybe
<Predicated
>>>
Deprecated
Use parseNumberMap instead.
Predicate
isAllToken()
Predicate to check if the given value is "*"
.
Parameters
Parameter | Type |
---|---|
actual | unknown |
Returns
actual is "*"
isListField()
Predicate checking if given value is a ListField.
Parameters
Parameter | Type |
---|---|
value | Field <number > |
Returns
value is ListField<number>
See
isListString()
Predicate checking if given value is a ListString.
Parameters
Parameter | Type |
---|---|
value | string |
Returns
value is `${string},${number}` | `${string},${number}-${number}`
See
isNumberString()
Predicate checking if given value is a number.
Type parameters
Type parameter |
---|
Value extends number |
Parameters
Parameter | Type |
---|---|
input | string |
Returns
input is `${Value}`
isRangeField()
Predicate checking if given value is a cron object range (RangeField).
Parameters
Parameter | Type |
---|---|
value | unknown |
Returns
value is RangeField<number>
See
isRangeString()
Predicate checking if given value is a cron string range (RangeString).
Parameters
Parameter | Type |
---|---|
value | string |
Returns
value is `${number}-${number}`
See
Regular Expression
cronRegExp
Regular expression to test for valid cron expressions.
Remarks
- We allow 0 or more whitespace before and after the expression.
- Negative lookahead to avoid impossible matches:
- 30 or 31 of February (Any combination like 30,31; 30-31; 30-30; etc.)
- 31 of February, April, Jun, September or November (also any combination).
- Then we start capturing each field in a named group (separated with any
amount of whitespace): -
minute
: Digits from0
to59
(including padded like05
). -hour
: Digits from0
to23
(also including padded). -dayOfMonth
: Digits from0
to31
(also including padded). -month
: Digits from1
to12
(also including padded, and 3 letter aliases likeoct
). -dayOfWeek
: Digits from0
to6
(all padded, and also including 3 letter aliases likefri
).
Example
fieldRegExp()
Regular expression to match a cron expression field.
Type parameters
Type parameter |
---|
Name extends string |
Value extends string |
Parameters
Parameter | Type | Description |
---|---|---|
name | Name | Named group name. |
value | Value | Possible values the expression can have. |
Returns
(?<${Name}>\*|(?:${Value}(?:-${Value})?|(?:(?:${Value}(?:-${Value})?,)+${Value}(?:-${Value})?)))
Named group capturing the given value by itself, in a list or range.
Example
paddedRegExp()
Regular expression to match field with an optional 0
to it’s left.
Type parameters
Type parameter |
---|
Value extends string | number |
Parameters
Parameter | Type | Description |
---|---|---|
value | Value | Value to pad. |
Returns
`0?${Value}`
RegExp to match value with padded 0
.
Example
rangeStringMatch()
Regular expression to test if given string is a range.
Parameters
Parameter | Type |
---|---|
text | string |
Returns
boolean
valueOrListRegExp()
Regular expression to match lists.
Type parameters
Type parameter |
---|
Value extends string | number |
Parameters
Parameter | Type | Description |
---|---|---|
value | Value | Value to match by itself or as a list. |
Returns
`(?:${Value}|(?:(?:${Value},)+${Value}))`
RegExp to match value or list.
Example
valueOrRangeRegExp()
Regular expression to match values or ranges.
Type parameters
Type parameter |
---|
Value extends string | number |
Parameters
Parameter | Type | Description |
---|---|---|
value | Value | Value to match by itself or as a range. |
Returns
`${Value}(?:-${Value})?`
RegExp to match value or range.
Example
valueRangeOrListRegExp()
Regular expression to match values, ranges or lists.
Type parameters
Type parameter |
---|
Value extends string | number |
Parameters
Parameter | Type | Description |
---|---|---|
value | Value | Value to match by itself, as a range or as a list. |
Returns
`(?:${Value}(?:-${Value})?|(?:(?:${Value}(?:-${Value})?,)+${Value}(?:-${Value})?))`
RegExp to match value, range or list.
Example
Token
AllToken
Type to represent the “all” token ("*"
).
ListExpressionSeparatorToken
Type to represent the “list expression separator” token (","
).
RangeExpressionSeparatorToken
Type to represent the “range expression separator” token ("-"
).
Util
nextDate()
Get next ISO date string for the given date and the given cron expression.
Parameters
Parameter | Type | Description |
---|---|---|
date | Readonly <Date > | Base date to get the next date from. |
Returns
Function
Curried function with date set.
Parameters
Parameter | Type |
---|---|
cron | `${string} ${string} ${string} ${string} ${string}` | Partial <CronObject > |
Returns
Maybe
<Date
>
Example
nextDates()
Get next ISO date iterator for the given date and the given cron expression.
Parameters
Parameter | Type | Description |
---|---|---|
date | Readonly <Date > | Base date to get the next date from. |
Returns
Function
Curried function with date set.
Parameters
Parameter | Type |
---|---|
cron | `${string} ${string} ${string} ${string} ${string}` | Partial <CronObject > |
Returns
Readonly
<IterableIterator
<Date
>>