@lou.codes/cron
TypeScript icon, indicating that this package has built-in type declarations

2.3.3 • Public • Published

Coverage License NPM Version Open Issues Size

⏲️ Standard cron expression tools.

Usage

📦 Node

Install @lou.codes/cron as a dependency:

pnpm add @lou.codes/cron
# or
npm install @lou.codes/cron
# or
yarn add @lou.codes/cron

Import it and use it:

import { parse, stringify } from "@lou.codes/cron";

const cron = parse("1-2,3,4 * 2 8,9 1");
/*
	{
		minutes: [{ from: 1, to: 2 }, 3, 4],
		hours: "*",
		dayOfMonth: 2,
		month: [8, 9],
		dayOfWeek: 1
	}
*/

stringify(cron); // "1-2,3,4 * 2 8,9 1"

// Also works with partials:
stringify({ hours: 13 }); // "* 13 * * *"

// Only parses with valid dates:
parse("* * 31 2 *"); // undefined because 2/31 is invalid

🦕 Deno

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

import { parse, stringify } from "@lou.codes/cron";

const cron = parse("1-2,3,4 * 2 8,9 1");
/*
	{
		minutes: [{ from: 1, to: 2 }, 3, 4],
		hours: "*",
		dayOfMonth: 2,
		month: [8, 9],
		dayOfWeek: 1
	}
*/

stringify(cron); // "1-2,3,4 * 2 8,9 1"

// Also works with partials:
stringify({ hours: 13 }); // "* 13 * * *"

// Only parses with valid dates:
parse("* * 31 2 *"); // undefined because 2/31 is invalid

🌎 Browser

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

<script type="module">
	import { parse, stringify } from "https://esm.sh/@lou.codes/cron";

	const cron = parse("1-2,3,4 * 2 8,9 1");
	/*
	{
		minutes: [{ from: 1, to: 2 }, 3, 4],
		hours: "*",
		dayOfMonth: 2,
		month: [8, 9],
		dayOfWeek: 1
	}
*/

	stringify(cron); // "1-2,3,4 * 2 8,9 1"

	// Also works with partials:
	stringify({ hours: 13 }); // "* 13 * * *"

	// Only parses with valid dates:
	parse("* * 31 2 *"); // undefined because 2/31 is invalid
</script>

Useful links

To do

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

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

Package Sidebar

Install

npm i @lou.codes/cron

Weekly Downloads

35

Version

2.3.3

License

MIT

Unpacked Size

65.4 kB

Total Files

113

Last publish

Collaborators

  • loucyx