Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

natural-cron

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

natural-cron

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

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
726
decreased by-15.48%
Maintainers
1
Weekly downloads
 
Created
Source

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 dayschedule.atTime('17:30').compile();30 17 * * *
Run at 9 AM on weekdaysschedule.atTime('09:00').onWeekDays([1,2,3,4,5]).compile();0 9 * * 1-5
Run at noon on the 1st and 15th of the monthschedule.atTime('12:00').onDaysOfMonth([1, 15]).compile();0 12 1,15 * *
Run at midnight during January and Julyschedule.atTime('00:00').duringMonths([1, 7]).compile();0 0 * 1,7 *
Run every 15 minutesschedule.everyX(15, CronTimeUnit.Minute).compile();*/15 * * * *
Run every day at noonschedule.every('day').atHours([12]).compile();0 12 * * *
Run every Sunday at 5 PMschedule.onWeekDays([0]).atHours([17]).compile();0 17 * * 0
Run the 1st day of every month at 1 AMschedule.onDaysOfMonth([1]).every('month').atHours([1]).compile();0 1 1 * *
Run every weekday at 8:30 AMschedule.atTime("08:30").onWeekDays([1, 2, 3, 4, 5]).compile();30 8 * * 1-5
Run every 6 hoursschedule.everyX(6, CronTimeUnit.Hour).compile();0 */6 * * *
Run every quarter at midnightschedule.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 AMschedule.atTime("10:15").onWeekDays([6, 0]).compile();15 10 * * 6,0
Run at 9 AM, 12 PM, and 3 PM every dayschedule.every('day').atHours([9, 12, 15]).compile();0 9,12,15 * * *
Run at 7 AM, 2 PM, and 10 PM on Tuesdaysschedule.atHours([7, 14, 22]).onWeekDays([2]).compile();0 7,14,22 * * 2
Run at 20 past every hour on the 5th of Julyschedule.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 hourschedule.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.

Keywords

FAQs

Package last updated on 29 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc