Socket
Book a DemoInstallSign in
Socket

recurrence

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recurrence

Generates a string based on string recurrence

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
3
-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

recurrence

build status Coverage Status

Calculates the diff between dates and returns a nicely formatted string.

Turns an array of dates into:

  • every 4 days
  • monthly
  • every 10 hours
  • etc

Installation

npm install --save recurrence

Usage

const recurrence = require('recurrence');

const dates = [
  new Date(2016, 1, 1),
  new Date(2016, 1, 2)
  new Date(2016, 1, 3)
];

console.log(recurrence(dates));

Duplicate dates are removed and the order doesn't matter.

Output

{
  "count": 1,
  "unit": "day/week/month/year",
  "recurrence": "Daily"
}

In the output recurrence is set to something that you could show directly to a user.

Values look like this:

  • every 5 seconds
  • every 30 minutes
  • hourly
  • every 8 hours
  • daily
  • every 3 weeks
  • monthly
  • yearly
  • every 10 years

Of course you can use the count and unit for your own output, maybe for other languages or otherwise different results.

Options

You can pass an options object as the second argument.

Defaults:

{
  "numberAsString": false,
  "strict": false,
  "noSingleUnits": false
}

Override any of these by passing in an object:

recurrence(dates, {
  strict: true,
  numberAsString: true
});

numberAsString

Outputs the number as a text:

  • true: 'Every four years'
  • false: 'Every 4 years'

strict

Throw an exception if an invalid date is detected. With strict = false just ignores the date.

noSingleUnits

Doesn't format strings as daily or yearly so it returns Every second/hour/month/year etc instead of daily or weekly

Example

const recurrence = require('recurrence');

const leapyears = [
  new Date(2016, 1, 29),
  new Date(2012, 1, 29)
  new Date(2008, 1, 29)
];

const result = recurrence(leapyears, {
  numberAsString: true  
});

console.log(`Leap year happens ${result.recurrence}!`);
// Leap year happens every four years!

Test

npm run test

Coverage

npm run cover

Keywords

date

FAQs

Package last updated on 05 Feb 2016

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.