Skip to content

Cron Reference

Coverage License NPM Version Open Issues Size

⏲️ Standard cron expression tools.

Usage

📦 Node

Install @lou.codes/cron as a dependency:

Terminal window
1
pnpm add @lou.codes/cron
2
# or
3
npm install @lou.codes/cron
4
# or
5
yarn add @lou.codes/cron

Import it and use it:

1
import { parse, stringify } from "@lou.codes/cron";
2
3
const cron = parse("1-2,3,4 * 2 8,9 1");
4
/*
5
{
6
minutes: [{ from: 1, to: 2 }, 3, 4],
7
hours: "*",
8
dayOfMonth: 2,
9
month: [8, 9],
10
dayOfWeek: 1
11
}
12
*/
13
14
stringify(cron); // "1-2,3,4 * 2 8,9 1"
15
16
// Also works with partials:
17
stringify({ hours: 13 }); // "* 13 * * *"
18
19
// Only parses with valid dates:
20
parse("* * 31 2 *"); // undefined because 2/31 is invalid

🦕 Deno

Import @lou.codes/cron using the npm: prefix, and use it directly:

1
import { parse, stringify } from "@lou.codes/cron";
2
3
const cron = parse("1-2,3,4 * 2 8,9 1");
4
/*
5
{
6
minutes: [{ from: 1, to: 2 }, 3, 4],
7
hours: "*",
8
dayOfMonth: 2,
9
month: [8, 9],
10
dayOfWeek: 1
11
}
12
*/
13
14
stringify(cron); // "1-2,3,4 * 2 8,9 1"
15
16
// Also works with partials:
17
stringify({ hours: 13 }); // "* 13 * * *"
18
19
// Only parses with valid dates:
20
parse("* * 31 2 *"); // undefined because 2/31 is invalid

🌎 Browser

Import @lou.codes/cron using esm.sh, and use it directly:

1
<script type="module">
2
import { parse, stringify } from "https://esm.sh/@lou.codes/cron";
3
4
const cron = parse("1-2,3,4 * 2 8,9 1");
5
/*
6
{
7
minutes: [{ from: 1, to: 2 }, 3, 4],
8
hours: "*",
9
dayOfMonth: 2,
10
month: [8, 9],
11
dayOfWeek: 1
12
}
13
*/
14
15
stringify(cron); // "1-2,3,4 * 2 8,9 1"
16
17
// Also works with partials:
18
stringify({ hours: 13 }); // "* 13 * * *"
19
20
// Only parses with valid dates:
21
parse("* * 31 2 *"); // undefined because 2/31 is invalid
22
</script>

To do

Soon a human readable parser will be added, so we can do stuff like:

1
readable("* * * * *"); // "Every minute"
2
readable("5 * * * *"); // "Minute 5 of every hour"
3
readable("* 5 * * *"); // "Every minute at 5 AM"
4
readable("* * 5 * *"); // "Every minute at the 5th day of every month"
5
readable("* * * 5 *"); // "Every minute in May"
6
readable("* * * * 5"); // "Every minute on Friday"
7
readable("5 5 5 5 5"); // "At 5:05 AM, the 5th day of May on Friday"

Cron Object

CronObject

1
type CronObject: object;

Object that represents the 5 cron expression fields.

See

Type declaration

MemberType
dayOfMonthField<DayOfMonth>
dayOfWeekField<DayOfWeek>
hourField<Hours>
minuteField<Minutes>
monthField<MonthValue>

View source


Field<Value>

1
type Field<Value>: AllToken | ListField<Value> | ValueOrRangeField<Value>;

Union of AllToken, ValueOrRangeField and ListField that represents a field in a cron expression.

See

Type parameters

Type parameter
Value extends number

View source


ListField<Value>

1
type ListField<Value>: ReadOnlyArray<ValueOrRangeField<Value>>;

Type that represents a list of values for a cron object field.

See

ValueOrRangeField

Type parameters

Type parameter
Value extends number

View source


MonthValue

1
type MonthValue: Range<1, 12>;

Type that represents values from 1 to 12 for the month cron field.

View source


RangeField<Value>

1
type RangeField<Value>: object;

Type that represents a range of values for a cron object field.

Type parameters

Type parameter
Value extends number

Type declaration

MemberTypeDescription
fromValueStart of the range, must be lower than to.
toValueEnd of the range, must be higher than from.

View source


ValueOrRangeField<Value>

1
type ValueOrRangeField<Value>: RangeField<Value> | Value;

Union of a set of numbers and a RangeField with that same set of numbers.

Type parameters

Type parameter
Value extends number

View source


ALL_TOKEN

1
const ALL_TOKEN: "*" = "*";

Token that represents “all” in a cron expression.

View source


DAY_OF_MONTH_NAME

1
const DAY_OF_MONTH_NAME: "dayOfMonth" = "dayOfMonth";

Name of a day of month field in the object representation of a cron expression.

View source


DAY_OF_WEEK_NAME

1
const DAY_OF_WEEK_NAME: "dayOfWeek" = "dayOfWeek";

Name of a day of week field in the object representation of a cron expression.

View source


FROM_NAME

1
const FROM_NAME: "from" = "from";

Range field from name.

View source


HOUR_NAME

1
const HOUR_NAME: "hour" = "hour";

Name of a hour field in the object representation of a cron expression.

View source


MINUTE_NAME

1
const MINUTE_NAME: "minute" = "minute";

Name of a minute field in the object representation of a cron expression.

View source


MONTH_NAME

1
const MONTH_NAME: "month" = "month";

Name of a month field in the object representation of a cron expression.

View source


TO_NAME

1
const TO_NAME: "to" = "to";

Range field to name.

View source


stringify()

1
function stringify(
2
cron: Partial<CronObject>,
3
): undefined | `${string} ${string} ${string} ${string} ${string}`;

Takes a cron object and returns a sting expression.

Parameters

ParameterTypeDescription
cronPartial<CronObject>Cron object.

Returns

undefined | `${string} ${string} ${string} ${string} ${string}`

Cron string expression.

Example

1
stringify({}); // "* * * * *"
2
stringify({ dayOfMonth: 13, month: 10 }); // "* * 13 10 *"
3
stringify({
4
minute: 5,
5
dayOfMonth: [10, 11, 13],
6
month: { from: 1, to: 10 },
7
}); // "5 * 10,11,13 1-10 *"
8
stringify({ month: 2, dayOfMonth: 31 }); // undefined

See

View source


stringifyField()

1
function stringifyField(field: Field<number>): FieldString;

Takes a cron object and returns a string expression.

Parameters

ParameterTypeDescription
fieldField<number>Cron object field.

Returns

FieldString

Cron string field.

Example

1
stringifyField("*"); // "*"
2
stringifyField(13); // "13"
3
stringifyField([10, 11, 13]); // "10,11,13"
4
stringifyField({ from: 1, to: 10 }); // "1-10"

See

View source


stringifyList()

1
function stringifyList(
2
field: Readonly<Field<number>>,
3
): Maybe<`${string},${number}` | `${string},${number}-${number}`>;

Turns cron list into a string.

Parameters

ParameterTypeDescription
fieldReadonly<Field<number>>List cron object field

Returns

Maybe<`${string},${number}` | `${string},${number}-${number}`>

String list or undefined if it isn’t a list.

Example

1
stringifyList([10, 11, 13]); // "10,11,13"

See

View source


stringifyRange()

1
function stringifyRange<Predicated>(
2
field: Readonly<Field<Predicated>>,
3
): Maybe<`${number}-${number}`>;

Turn a cron range into a string.

Type parameters

Type parameter
Predicated extends number

Parameters

ParameterTypeDescription
fieldReadonly<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

1
parseRange({ from: 1, to: 13 }); // "1-13"

See

isRangeField

View source

Cron String

CronString

1
type CronString: `${string} ${string} ${string} ${string} ${string}`;

String cron expression.

View source


FieldString

1
type FieldString: AllToken | ListString | ValueOrRangeString;

Union of AllToken, ValueOrRangeString and ListString that represents a field in a cron expression.

See

View source


ListString

1
type ListString: `${string}${ListExpressionSeparatorToken}${ValueOrRangeString}`;

Type that represents a list of values for a cron string field.

See

ValueOrRangeString

View source


RangeString

1
type RangeString: `${number}${RangeExpressionSeparatorToken}${number}`;

Type that represents a range of values for a cron string field.

View source


ValueOrRangeString

1
type ValueOrRangeString: RangeString | `${number}`;

Union of a set any number and a RangeString.

View source


ALL_TOKEN

1
const ALL_TOKEN: "*" = "*";

Token that represents “all” in a cron expression.

View source


LIST_EXPRESSION_SEPARATOR_TOKEN

1
const LIST_EXPRESSION_SEPARATOR_TOKEN: "," = ",";

Token to separate list items in a cron expression.

View source


RANGE_EXPRESSION_SEPARATOR_TOKEN

1
const RANGE_EXPRESSION_SEPARATOR_TOKEN: "-" = "-";

Token to separate range values in a cron expression.

View source


normalizeMap

1
const normalizeMap: object;

Map from 3 letter aliases to their respective number representations.

Type declaration

MemberTypeValueDescription
apr44April number representation
aug88August number representation
dec1212December number representation
feb22February number representation
fri55Friday number representation
jan11January number representation
jul77July number representation
jun66June number representation
mar33March number representation
may55May number representation
mon11Monday number representation
nov1111November number representation
oct1010October number representation
sat66Saturday number representation
sep99September number representation
sun00Sunday number representation
thu44Thursday number representation
tue22Tuesday number representation
wed33Wednesday number representation

View source


isValidExpression()

1
function isValidExpression(
2
string: string,
3
): string is `${string} ${string} ${string} ${string} ${string}`;

Validates if a string is a cron expression.

Parameters

ParameterType
stringstring

Returns

string is `${string} ${string} ${string} ${string} ${string}`

See

View source


normalizeAliases()

1
function normalizeAliases(expression: string): FieldString;

Normalizes day and month 3 letter aliases into their number counterparts.

Parameters

ParameterTypeDescription
expressionstringString expression.

Returns

FieldString

Normalized expression

Example

1
normalizeAliases("* * 13 oct fri"); // "* * 13 10 5"

View source


parse()

1
function parse(
2
expression: `${string} ${string} ${string} ${string} ${string}`,
3
): Maybe<CronObject>;

Parses a cron expression into an object representation.

Parameters

ParameterTypeDescription
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

1
parse("* * * * *"); // { minute: "*", hour: "*", dayOfMonth: "*", month: "*", dayOfWeek: "*" }
2
parse("* * 13 10 *"); // { minute: "*", hour: "*", dayOfMonth: 13, month: 10, dayOfWeek: "*" }
3
parse("5 * 10,11,13 1-10 *"); // { minute: 5, hour: "*", dayOfMonth: [10, 11, 13], month: { from: 1, to: 10 }, dayOfWeek: "*" }
4
parse("* * 31 2 *"); // undefined
5
parse("nope"); // undefined

See

View source


parseField()

1
function parseField(
2
field: string,
3
): "*" | RangeField<number> | ListField<number> | Maybe<number>;

Parses a cron field.

Parameters

ParameterTypeDescription
fieldstringCron field value (should be validated before this).

Returns

"*" | RangeField<number> | ListField<number> | Maybe<number>

Parsed field.

Example

1
parseField("*"); // "*"
2
parseField("13"); // 13
3
parseField("10,11,13"); // [10, 11, 13]
4
parseField("1-10"); // { from: 1, to: 10 }

See

View source


parseFieldTuplesMap()

1
function parseFieldTuplesMap(
2
iterable: Readonly<Iterable<readonly [keyof CronObject, string]>>,
3
): Readonly<
4
IterableIterator<
5
readonly [
6
"minute" | "hour" | "dayOfMonth" | "month" | "dayOfWeek",
7
"*" | RangeField<number> | ListField<number> | Maybe<number>,
8
]
9
>
10
>;

Given an iterable of tuples with the name of a field and a field value, run each field through parseField.

Parameters

ParameterType
iterableReadonly<Iterable<readonly [keyof CronObject, string]>>

Returns

Readonly<IterableIterator<readonly [ | "minute" | "hour" | "dayOfMonth" | "month" | "dayOfWeek", "*" | RangeField<number> | ListField<number> | Maybe<number>]>>

Example

1
parseFieldTuplesMap([["minute", "*"]]); // [["minute", "*"]]
2
parseFieldTuplesMap([["minute", "13"]]); // [["minute", 13]]
3
parseFieldTuplesMap([["minute", "10,11,13"]]); // [["minute", [10, 11, 13]]]
4
parseFieldTuplesMap([["minute", "1-10"]]); // [["minute", { from: 1, to: 10 }]]

See

parseField

View source


parseList()

1
function parseList<Predicated>(value: string): Maybe<ListField<Predicated>>;

Parses a cron list into an array.

Type parameters

Type parameter
Predicated extends number

Parameters

ParameterTypeDescription
valuestringString that might be a list.

Returns

Maybe<ListField<Predicated>>

Parsed list of undefined if it isn’t a list string.

Example

1
parseList("10,11,13"); // [10, 11, 13]

See

View source


parseListMap()

1
function parseListMap(
2
iterable: Readonly<Iterable<string>>,
3
): Readonly<IterableIterator<RangeField<number> | Maybe<number>>>;

Map to parse cron list items (can be either a range or a number).

Parameters

ParameterType
iterableReadonly<Iterable<string>>

Returns

Readonly<IterableIterator<RangeField<number> | Maybe<number>>>

Example

1
parseListMap(["1", "05", "13", "5-13", "13-13", "13-5", "99"]);
2
// [1, 5, 13, { from: 5, to: 13 }, 13, undefined, undefined]

See

View source


parseNumber()

1
function parseNumber<Predicated>(value: string): Maybe<Predicated>;

Parses a cron list into an array.

Type parameters

Type parameter
Predicated extends number

Parameters

ParameterTypeDescription
valuestringString that might be a list.

Returns

Maybe<Predicated>

Parsed list of undefined if it isn’t a list string.

Example

1
parseNumber("5"); // 5
2
parseNumber("13"); // 13
3
parseNumber("59"); // 59
4
parseNumber("60"); // undefined (60 isn't valid for any cron field)

View source


parseNumberMap()

1
function parseNumberMap<Predicated>(
2
iterable: Readonly<Iterable<string>>,
3
): Readonly<IterableIterator<Maybe<Predicated>>>;

Maps given iterable through parseNumber.

Type parameters

Type parameter
Predicated extends number

Parameters

ParameterType
iterableReadonly<Iterable<string>>

Returns

Readonly<IterableIterator<Maybe<Predicated>>>

Example

1
parseNumberMap(["05"]); // [5]
2
parseNumberMap(["13"]); // [13]
3
parseNumberMap(["59"]); // [59]
4
parseNumberMap(["60"]); // [undefined] (60 isn't valid for any cron field)

See

parseNumber

View source


parseNumberMatch()

1
function parseNumberMatch(text: string): boolean;

Matches only valid number values for a cron expression (from 0 or 00 to 59).

Parameters

ParameterType
textstring

Returns

boolean

Example

1
parseNumberMatch("13"); // true
2
parseNumberMatch("05"); // true
3
parseNumberMatch("60"); // false
4
parseNumberMatch("foo"); // false

See

paddedRegExp

View source


parseRange()

1
function parseRange<Predicated>(
2
value: string,
3
): Maybe<Predicated | RangeField<Predicated>>;

Parses a cron range into an object.

Type parameters

Type parameter
Predicated extends number

Parameters

ParameterTypeDescription
valuestringString that might be a range.

Returns

Maybe<Predicated | RangeField<Predicated>>

Parsed ranged of undefined if it isn’t a range string.

Example

1
parseRange("1-13"); // { from: 1, to: 13 }
2
parseRange("13-13"); // 13 (normalized)
3
parseRange("13-1"); // undefined

See

View source

Internal

fieldNamesTuple

1
const fieldNamesTuple: readonly [
2
"minute",
3
"hour",
4
"dayOfMonth",
5
"month",
6
"dayOfWeek",
7
];

Field names in order.

View source


compareField()

1
function compareField(
2
value: number,
3
field: ValueOrRangeField<number> | ListField<number>,
4
): boolean;

Checks if given value is included in given field.

Parameters

ParameterTypeDescription
valuenumberValue to compare.
fieldValueOrRangeField<number> | ListField<number>Field to compare.

Returns

boolean

true if value is included in the given field, false if it isn’t.

Example

1
compareField(13, 13); // true
2
compareField(13, 99); // false
3
compareField(13, { from: 0, to: 99 }); // true
4
compareField(13, { from: 0, to: 10 }); // false
5
compareField(13, [10, 13]); // true
6
compareField(13, [10, 11, 12]); // false
7
compareField(13, [10, { from: 11, to: 99 }]); // true
8
compareField(13, [5, { from: 10, to: 12 }]); // false

View source


compareRangeOrValue()

1
function compareRangeOrValue(
2
value: number,
3
): (valueOrRange: ValueOrRangeField<number>) => boolean;

Compares value to a ValueOrRangeField.

Parameters

ParameterTypeDescription
valuenumberValue to be compared.

Returns

Function

Curried function expecting a ValueOrRangeField.

Parameters
ParameterType
valueOrRangeValueOrRangeField<number>
Returns

boolean

Example

1
compareRangeOrValue(13)({ from: 0, to: 99 }); // true
2
compareRangeOrValue(13)({ from: 0, to: 10 }); // false
3
compareRangeOrValue(13)(13); // true
4
compareRangeOrValue(13)(14); // false

View source


dateInCron()

1
function dateInCron(cron: CronObject): (date: Readonly<Date>) => boolean;

Check if given cron expression object includes given date.

Parameters

ParameterTypeDescription
cronCronObjectCron object.

Returns

Function

Curried function with cron in context.

Parameters
ParameterType
dateReadonly<Date>
Returns

boolean

View source


zipRangeNames()

1
function zipRangeNames<ItemSecond>(
2
iterableSecond: Readonly<Iterable<ItemSecond>>,
3
): Readonly<IterableIterator<readonly ["from" | "to", ItemSecond]>>;

Zips “from” and “to”.

Type parameters

Type parameter
ItemSecond

Parameters

ParameterType
iterableSecondReadonly<Iterable<ItemSecond>>

Returns

Readonly<IterableIterator<readonly ["from" | "to", ItemSecond]>>

View source

Other

parseDecimalMap()

1
function parseDecimalMap<Predicated>(
2
iterable: Readonly<Iterable<string>>,
3
): Readonly<IterableIterator<Maybe<Predicated>>>;

Type parameters

Type parameter
Predicated extends number

Parameters

ParameterType
iterableReadonly<Iterable<string>>

Returns

Readonly<IterableIterator<Maybe<Predicated>>>

Deprecated

Use parseNumberMap instead.

View source

Predicate

isAllToken()

1
function isAllToken(actual: unknown): actual is "*";

Predicate to check if the given value is "*".

Parameters

ParameterType
actualunknown

Returns

actual is "*"

View source


isListField()

1
function isListField(value: Field<number>): value is ListField<number>;

Predicate checking if given value is a ListField.

Parameters

ParameterType
valueField<number>

Returns

value is ListField<number>

See

View source


isListString()

1
function isListString(
2
value: string,
3
): value is `${string},${number}` | `${string},${number}-${number}`;

Predicate checking if given value is a ListString.

Parameters

ParameterType
valuestring

Returns

value is `${string},${number}` | `${string},${number}-${number}`

See

View source


isNumberString()

1
function isNumberString<Value>(input: string): input is `${Value}`;

Predicate checking if given value is a number.

Type parameters

Type parameter
Value extends number

Parameters

ParameterType
inputstring

Returns

input is `${Value}`

View source


isRangeField()

1
function isRangeField(value: unknown): value is RangeField<number>;

Predicate checking if given value is a cron object range (RangeField).

Parameters

ParameterType
valueunknown

Returns

value is RangeField<number>

See

RangeField

View source


isRangeString()

1
function isRangeString(value: string): value is `${number}-${number}`;

Predicate checking if given value is a cron string range (RangeString).

Parameters

ParameterType
valuestring

Returns

value is `${number}-${number}`

See

View source

Regular Expression

cronRegExp

1
const cronRegExp: "^s(?!(?:S+s+){2}(?:(?:(?:301?|(?:(?:301?,)+301?))s+(?:(?:0?2|feb)(?:-(?:0?2|feb))?|(?:(?:(?:0?2|feb)(?:-(?:0?2|feb))?,)+(?:0?2|feb)(?:-(?:0?2|feb))?)))|(?:(?:31(?:-31)?|(?:(?:31(?:-31)?,)+31(?:-31)?))s+(?:(?:0?[2469]|11|feb|apr|jun|sep|nov)|(?:(?:(?:0?[2469]|11|feb|apr|jun|sep|nov),)+(?:0?[2469]|11|feb|apr|jun|sep|nov)))))s+S+)(?<minute>*|(?:(?:0?d|[1-5]d)(?:-(?:0?d|[1-5]d))?|(?:(?:(?:0?d|[1-5]d)(?:-(?:0?d|[1-5]d))?,)+(?:0?d|[1-5]d)(?:-(?:0?d|[1-5]d))?)))s+(?<hour>*|(?:(?:0?d|1d|20-3)(?:-(?:0?d|1d|20-3))?|(?:(?:(?:0?d|1d|20-3)(?:-(?:0?d|1d|20-3))?,)+(?:0?d|1d|20-3)(?:-(?:0?d|1d|20-3))?)))s+(?<dayOfMonth>*|(?:(?:0?[1-9]|[12]d|3[01])(?:-(?:0?[1-9]|[12]d|3[01]))?|(?:(?:(?:0?[1-9]|[12]d|3[01])(?:-(?:0?[1-9]|[12]d|3[01]))?,)+(?:0?[1-9]|[12]d|3[01])(?:-(?:0?[1-9]|[12]d|3[01]))?)))s+(?<month>*|(?:(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|0?[1-9]|1[0-2])(?:-(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|0?[1-9]|1[0-2]))?|(?:(?:(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|0?[1-9]|1[0-2])(?:-(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|0?[1-9]|1[0-2]))?,)+(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|0?[1-9]|1[0-2])(?:-(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|0?[1-9]|1[0-2]))?)))s+(?<dayOfWeek>*|(?:(?:sun|mon|tue|wed|thu|fri|sat|0?[0-6])(?:-(?:sun|mon|tue|wed|thu|fri|sat|0?[0-6]))?|(?:(?:(?:sun|mon|tue|wed|thu|fri|sat|0?[0-6])(?:-(?:sun|mon|tue|wed|thu|fri|sat|0?[0-6]))?,)+(?:sun|mon|tue|wed|thu|fri|sat|0?[0-6])(?:-(?:sun|mon|tue|wed|thu|fri|sat|0?[0-6]))?)))s$";

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 from 0 to 59 (including padded like 05). - hour: Digits from 0 to 23 (also including padded). - dayOfMonth: Digits from 0 to 31 (also including padded). - month: Digits from 1 to 12 (also including padded, and 3 letter aliases like oct). - dayOfWeek: Digits from 0 to 6 (all padded, and also including 3 letter aliases like fri).

Example

1
new RegExp(cronRegExp).test("* * * * *"); // true
2
new RegExp(cronRegExp).test("nope"); // false
3
new RegExp(cronRegExp).test("* * 31 2 *"); // false
4
new RegExp(cronRegExp).test("* * 31 feb,4,6 *"); // false
5
new RegExp(cronRegExp).test("* * 31 feb,mar *"); // true
6
new RegExp(cronRegExp).test("1 2 3 4 5"); // true

View source


fieldRegExp()

1
function fieldRegExp<Name, Value>(name: Name, value: Value): (?<${Name}>*|(?:${Value}(?:-${Value})?|(?:(?:${Value}(?:-${Value})?,)+${Value}(?:-${Value})?)))

Regular expression to match a cron expression field.

Type parameters

Type parameter
Name extends string
Value extends string

Parameters

ParameterTypeDescription
nameNameNamed group name.
valueValuePossible 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

1
fieldRegExp("example", 13); // "(?<example>\\*|(?:13(?:-13)?|(?:(?:13(?:-13)?,)+13(?:-13)?)))"

View source


paddedRegExp()

1
function paddedRegExp<Value>(value: Value): `0?${Value}`;

Regular expression to match field with an optional 0 to it’s left.

Type parameters

Type parameter
Value extends string | number

Parameters

ParameterTypeDescription
valueValueValue to pad.

Returns

`0?${Value}`

RegExp to match value with padded 0.

Example

1
paddedRegExp(5); // "0?5"

View source


rangeStringMatch()

1
function rangeStringMatch(text: string): boolean;

Regular expression to test if given string is a range.

Parameters

ParameterType
textstring

Returns

boolean

View source


valueOrListRegExp()

1
function valueOrListRegExp<Value>(
2
value: Value,
3
): `(?:${Value}|(?:(?:${Value},)+${Value}))`;

Regular expression to match lists.

Type parameters

Type parameter
Value extends string | number

Parameters

ParameterTypeDescription
valueValueValue to match by itself or as a list.

Returns

`(?:${Value}|(?:(?:${Value},)+${Value}))`

RegExp to match value or list.

Example

1
valueOrListRegExp(13); // "(?:13|(?:(?:13,)+13))"

View source


valueOrRangeRegExp()

1
function valueOrRangeRegExp<Value>(value: Value): `${Value}(?:-${Value})?`;

Regular expression to match values or ranges.

Type parameters

Type parameter
Value extends string | number

Parameters

ParameterTypeDescription
valueValueValue to match by itself or as a range.

Returns

`${Value}(?:-${Value})?`

RegExp to match value or range.

Example

1
valueOrRangeRegExp(13); // "13(?:-13)?"

View source


valueRangeOrListRegExp()

1
function valueRangeOrListRegExp<Value>(
2
value: Value,
3
): `(?:${Value}(?:-${Value})?|(?:(?:${Value}(?:-${Value})?,)+${Value}(?:-${Value})?))`;

Regular expression to match values, ranges or lists.

Type parameters

Type parameter
Value extends string | number

Parameters

ParameterTypeDescription
valueValueValue 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

1
valueRangeOrListRegExp(13); // "(?:13(?:-13)?|(?:(?:13(?:-13)?,)+13(?:-13)?))"

View source

Token

AllToken

1
type AllToken: typeof ALL_TOKEN;

Type to represent the “all” token ("*").

View source


ListExpressionSeparatorToken

1
type ListExpressionSeparatorToken: typeof LIST_EXPRESSION_SEPARATOR_TOKEN;

Type to represent the “list expression separator” token (",").

View source


RangeExpressionSeparatorToken

1
type RangeExpressionSeparatorToken: typeof RANGE_EXPRESSION_SEPARATOR_TOKEN;

Type to represent the “range expression separator” token ("-").

View source

Util

nextDate()

1
function nextDate(
2
date: Readonly<Date>,
3
): (
4
cron:
5
| `${string} ${string} ${string} ${string} ${string}`
6
| Partial<CronObject>,
7
) => Maybe<Date>;

Get next ISO date string for the given date and the given cron expression.

Parameters

ParameterTypeDescription
dateReadonly<Date>Base date to get the next date from.

Returns

Function

Curried function with date set.

Parameters
ParameterType
cron`${string} ${string} ${string} ${string} ${string}` | Partial<CronObject>
Returns

Maybe<Date>

Example

1
nextDate(new Date("1989-10-13T10:15:00.000"))("* * * * *"); // Date("1989-10-13T10:16:00.000")

View source


nextDates()

1
function nextDates(
2
date: Readonly<Date>,
3
): (
4
cron:
5
| `${string} ${string} ${string} ${string} ${string}`
6
| Partial<CronObject>,
7
) => Readonly<IterableIterator<Date>>;

Get next ISO date iterator for the given date and the given cron expression.

Parameters

ParameterTypeDescription
dateReadonly<Date>Base date to get the next date from.

Returns

Function

Curried function with date set.

Parameters
ParameterType
cron`${string} ${string} ${string} ${string} ${string}` | Partial<CronObject>
Returns

Readonly<IterableIterator<Date>>

Example

1
take(2)(nextDates(new Date("1989-10-13T10:15:00.000Z"))("* * * * *"));
2
// [Date("1989-10-13T10:16:00.000"), Date("1989-10-13T10:17:00.000Z")]

View source