Skip to main content

@coven/cron@0.3.3
Built and signed on GitHub Actions

⏳ Fantastic cron parser and constructor.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.3.3)
Package root>nextDates.ts
import { EMPTY_STRING } from "jsr:@coven/constants@^0.3.3"; import { buildUnicode, DIGIT, escape, join, quantity } from "jsr:@coven/expression@^0.3.3"; import { iteratorFunctionToIterableIterator } from "jsr:@coven/iterables@^0.3.3"; import { isString, isUndefined } from "jsr:@coven/predicates@^0.3.3"; import type { CronObject } from "./CronObject.ts"; import type { CronString } from "./CronString.ts"; import { dateInCron } from "./dateInCron.ts"; import { parse } from "./parse.ts"; import { stringify } from "./stringify.ts"; const dateReplace = join(quantity(2)(DIGIT), escape("."), quantity(3)(DIGIT)); /** * Get next ISO date iterator for the given date and the given cron expression. * * @example * ```typescript * import { take } from "@coven/iterables"; * * take(2)(nextDates(new Date("1989-10-13T10:15:00.000Z"))("* * * * *")); * // [Date("1989-10-13T10:16:00.000"), Date("1989-10-13T10:17:00.000Z")] * ``` * @param date Base date to get the next date from. * @returns Curried function with date set. */ export const nextDates = ( date: Readonly<Date>, ): (cron: CronString | Partial<CronObject>) => IterableIterator<Date> => (cron) => iteratorFunctionToIterableIterator(function* (): Generator<Date> { const cronObject = parse( isString(cron) ? cron : ( stringify(cron) ?? (EMPTY_STRING as CronString) ), ); if (!isUndefined(cronObject)) { const validDate = dateInCron(cronObject); const now = new Date( date .toISOString() .replace( buildUnicode(dateReplace), "00.000", ), ); for (;;) { validDate((now.setMinutes(now.getMinutes() + 1), now)) ? yield new Date(now) : undefined; } } });