npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

natural-cron

v1.0.2

Published

Natural Cron is a easy-to-use Node.js library for creating and validating cron expressions with natural, human-readable APIs.

Downloads

3,198

Readme

Natural Cron ⏰⏰

Natural Cron is a easy-to-use Node.js library for creating and validating cron expressions with natural, human-readable APIs.

Features

  • Chainable methods for constructing cron expressions.
  • Validation of cron components (minute, hour, day of the month, month, day of the week).
  • Helpful error messages for invalid input.
  • TypeScript support for strong typing and intellisense.

Installation

Install using npm:

npm install natural-cron

Or using yarn:

yarn add natural-cron

Usages

Utilize CronExpressionBuilder to generate cron expressions for various scheduling scenarios.

const { CronExpressionBuilder, CronValidators } = require('natural-cron');
const schedule = new  CronExpressionBuilder();

📋 Examples Table

| 📅 Description | 💻 Code Example | ⏰ Cron Expression | |----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|------------------------| | Run a job at 5:30 PM every day | schedule.atTime('17:30').compile(); | 30 17 * * * | | Run at 9 AM on weekdays | schedule.atTime('09:00').onWeekDays([1,2,3,4,5]).compile(); | 0 9 * * 1-5 | | Run at noon on the 1st and 15th of the month | schedule.atTime('12:00').onDaysOfMonth([1, 15]).compile(); | 0 12 1,15 * * | | Run at midnight during January and July | schedule.atTime('00:00').duringMonths([1, 7]).compile(); | 0 0 * 1,7 * | | Run every 15 minutes | schedule.everyX(15, CronTimeUnit.Minute).compile(); | */15 * * * * | | Run every day at noon | schedule.every('day').atHours([12]).compile(); | 0 12 * * * | | Run every Sunday at 5 PM | schedule.onWeekDays([0]).atHours([17]).compile(); | 0 17 * * 0 | | Run the 1st day of every month at 1 AM | schedule.onDaysOfMonth([1]).every('month').atHours([1]).compile(); | 0 1 1 * * | | Run every weekday at 8:30 AM | schedule.atTime("08:30").onWeekDays([1, 2, 3, 4, 5]).compile(); | 30 8 * * 1-5 | | Run every 6 hours | schedule.everyX(6, CronTimeUnit.Hour).compile(); | 0 */6 * * * | | Run every quarter at midnight | schedule.duringMonths([1, 4, 7, 10]).onDaysOfMonth([1]).atTime("00:00").compile(); | 0 0 1 1,4,7,10 * | | Run every Saturday and Sunday at 10:15 AM | schedule.atTime("10:15").onWeekDays([6, 0]).compile(); | 15 10 * * 6,0 | | Run at 9 AM, 12 PM, and 3 PM every day | schedule.every('day').atHours([9, 12, 15]).compile(); | 0 9,12,15 * * * | | Run at 7 AM, 2 PM, and 10 PM on Tuesdays | schedule.atHours([7, 14, 22]).onWeekDays([2]).compile(); | 0 7,14,22 * * 2 | | Run at 20 past every hour on the 5th of July | schedule.atMinutes([20]).onDaysOfMonth([5]).duringMonths([7]).compile(); | 20 * 5 7 * | | Run every 5 minutes during office hours - every() | schedule.every('minute').atMinutes([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]).atHours([9, 10, 11, 12, 13, 14, 15, 16]).compile(); | 0,5,10,15,20,25,30,35,40,45,50,55 9-16 * * * | | Run every 5 minutes during office hours - everyX() | schedule.everyX(5,CronTimeUnit.Minute).atHours([9, 10, 11, 12, 13, 14, 15, 16]).compile() | */5 9-16 * * * | | Run at quarter past and quarter to every hour | schedule.every('hour').atMinutes([15, 45]).compile(); | 15,45 * * * * |

Note: schedule is an instance of CronExpressionBuilder utilized for generating the cron expressions.

API Reference

CronExpressionBuilder

  • atMinutes(minutes: Array<number>): this
  • atHours(hours: Array<number>): this
  • atTime(time: string): this
  • every(unit: string): this
  • everyX(interval: number, unit: ScheduleUnit): this
  • onWeekdays(days: Array<number>): this
  • onDaysOfMonth(days: Array<number>): this
  • duringMonths(months: Array<number>): this
  • compile(): string - Get the cron expression

Use CronTimeUnit enum for ScheduleUnit

export enum CronTimeUnit {
  Minute = 'minute',
  Hour = 'hour',
  DayOfMonth = 'dayOfMonth',
  Month = 'month',
  DayOfWeek = 'dayOfWeek',
}

CronValidators

Static methods for validating cron expression components.

  • validateMinute(minute: number)
  • validateHour(hour: number)
  • validateDayOfMonth(day: number)
  • validateMonth(month: number)
  • validateDayOfWeek(day: number)
  • validateTime(time: string)

Contributing

Contributions are welcome! Please feel free to submit a pull request.

License

This project is licensed under the MIT License.