@lou.codes/cron
v2.3.3
Published
⏲️ Cron Quartz and Cron UNIX expression parser
Downloads
362
Maintainers
Readme
⏲️ 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
- 📝 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:
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"