Skip to content

Cron

18 exports Added in v2.0.0 Source

Constructors

make

Added in v2.0.0 Source

Creates a Cron instance.

Signature

declare function make(values: {
  readonly and?: boolean;
  readonly days: Iterable<number>;
  readonly hours: Iterable<number>;
  readonly minutes: Iterable<number>;
  readonly months: Iterable<number>;
  readonly seconds?: Iterable<number, any, any>;
  readonly tz?: TimeZone;
  readonly weekdays: Iterable<number>;
}): Cron;

parse

Added in v2.0.0 Source

Parses a cron expression into a Cron instance.

Signature

declare function parse(cron: string, tz?: string | TimeZone): Either<Cron, ParseError>;

unsafeParse

Added in v2.0.0 Source

Parses a cron expression into a Cron instance.

Details

This function takes a cron expression as a string and attempts to parse it into a Cron instance. If the expression is valid, the resulting Cron instance will represent the schedule defined by the cron expression.

If the expression is invalid, the function throws a ParseError.

You can optionally provide a time zone (tz) to interpret the cron expression in a specific time zone. If no time zone is provided, the cron expression will use the default time zone.

Signature

declare function unsafeParse(cron: string, tz?: string | TimeZone): Cron;

Guards

isCron

Added in v2.0.0 Source

Checks if a given value is a Cron instance.

Signature

declare function isCron(u: unknown): u is Cron;

isParseError

Added in v2.0.0 Source

Returns true if the specified value is an ParseError, false otherwise.

Signature

declare function isParseError(u: unknown): u is ParseError;

Instances

Equivalence

Added in v2.0.0 Source

Signature

declare const Equivalence: equivalence.Equivalence<Cron>;

Models

Cron interface

Added in v2.0.0 Source

Signature

interface Cron extends Pipeable, Equal, Inspectable {
  readonly [TypeId]: typeof TypeId;
  readonly days: ReadonlySet<number>;
  readonly hours: ReadonlySet<number>;
  readonly minutes: ReadonlySet<number>;
  readonly months: ReadonlySet<number>;
  readonly seconds: ReadonlySet<number>;
  readonly tz: Option<TimeZone>;
  readonly weekdays: ReadonlySet<number>;
}

ParseError

Added in v2.0.0 Source

Represents a checked exception which occurs when decoding fails.

Signature

declare class ParseError extends YieldableError<this> & {
  readonly _tag: "CronParseError";
} & Readonly<{
  readonly input?: string;
  readonly message: string;
}> {
  constructor(args: {
    readonly input?: string;
    readonly message: string;
  });
  readonly [ParseErrorTypeId]: symbol;
}

Other

match

Added in v2.0.0 Source

Checks if a given Date falls within an active Cron time window.

Signature

declare function match(cron: Cron, date: Input): boolean;

next

Added in v2.0.0 Source

Returns the next run Date for the given Cron instance.

Uses the current time as a starting point if no value is provided for startFrom.

Signature

declare function next(cron: Cron, startFrom?: Input): Date;

prev

Added in v3.20.0 Source

Returns the previous run Date for the given Cron instance.

Uses the current time as a starting point if no value is provided for startFrom.

Signature

declare function prev(cron: Cron, startFrom?: Input): Date;

sequence

Added in v2.0.0 Source

Returns an IterableIterator which yields the sequence of Dates that match the Cron instance.

Signature

declare function sequence(cron: Cron, startFrom?: Input): IterableIterator<Date>;

Returns an IterableIterator which yields the sequence of Dates that match the Cron instance, in reverse direction.

Signature

declare function sequenceReverse(cron: Cron, startFrom?: Input): IterableIterator<Date>;

Predicates

equals

Added in v2.0.0 Source

Checks if two Crons are equal.

Signature

declare const equals: {
  (that: Cron): (self: Cron) => boolean;
  (self: Cron, that: Cron): boolean;
};

Symbol

Signature

declare const ParseErrorTypeId: unique symbol;

TypeId type

Added in v2.0.0 Source

Signature

type TypeId = typeof TypeId;

Symbols

ParseErrorTypeId type

Added in v2.0.0 Source

Signature

type ParseErrorTypeId = typeof ParseErrorTypeId;

TypeId

Added in v2.0.0 Source

Signature

declare const TypeId: unique symbol;