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

date-streaks

v1.2.1

Published

Find a variety of streak metrics from a list of dates.

Downloads

1,881

Readme

🗓⚡️ Date Streaks

A package to find streaks from a list of dates.

Quick example:

import { summary } from 'date-streaks';

const dates = [
  new Date('01/01/2018'),
  new Date('01/02/2018'),
  new Date('01/04/2018')
];

summary({ dates }) // or summary(dates) -> accepts an array as well;

Returns:

{
  currentStreak: 0,
  longestStreak: 2,
  streaks: [ 2, 1 ],
  todayInStreak: false,
  withinCurrentStreak: false
}

Installation

# with npm:
npm install date-streaks --save

# or with yarn:
yarn add date-streaks

Usage

date-streaks comes with three functions to get various information about streaks throughout a list of dates.

summary

Summary finds streaks within a list of dates and returns the current streak, the longest streak, whether today is in the streak, and whether the current streak is still valid.

The logic for withinCurrentStreak is whether today or yesterday appears in the list. Since a user would still be in a streak if they completed an event yesterday and still had yet to complete it today.

Example

Let's pretend today is 01/07/2018 for this example:

import { summary } from 'date-streaks';

const dates = [
  new Date('01/01/2018'),
  new Date('01/02/2018'),
  new Date('01/03/2018'),
  new Date('01/06/2018')
];

summary({ dates }); // or summary(dates) -> accepts an array as well

Returns:

{
  currentStreak: 1,
  longestStreak: 3,
  streaks: [ 3, 1 ],
  todayInStreak: false,
  withinCurrentStreak: true
}

streakRanges

Streak ranges finds the start and end dates of each streak, in addition to the duration of each of the streaks.

Example

import { streakRanges } from 'date-streaks';

const dates = [
  new Date('01/01/2018'),
  new Date('01/02/2018'),
  new Date('01/03/2018'),
  new Date('01/06/2018')
];

streakRanges({ dates }); // or streakRanges(dates) -> accepts an array as well

Returns:

[
  {
    start: 'Sat Jan 06 2018 00:00:00 GMT-0500',
    end: null,
    duration: 1
  },
  {
    start: 'Mon Jan 01 2018 00:00:00 GMT-0500',
    end: 'Wed Jan 03 2018 00:00:00 GMT-0500',
    duration: 3
  }
];

trackRecord

Track record returns a list of dates from a specified date into the past with the provided dates marked as true. This is especially helpful for features where you want to show users a calendar of dates where they completed some task.

Example

Let's get a track record for the days preceeding 1/13/2018.

import { trackRecord } from 'date-streaks';

const dates = [
  new Date('01/04/2018'),
  new Date('01/05/2018'),
  new Date('01/11/2018'),
  new Date('01/12/2018')
];

// defaults to 7 days
const length = 10;

// defaults to today's date
const endDate = new Date('01/13/2018');


trackRecord({ dates, length, endDate });

Returns:

{
  'Wed Jan 13 2018 00:00:00 GMT-0400': false,
  'Tue Jan 12 2018 00:00:00 GMT-0400': true,
  'Mon Jan 11 2018 00:00:00 GMT-0400': true,
  'Sun Jan 10 2018 00:00:00 GMT-0400': false,
  'Sat Jan 09 2018 00:00:00 GMT-0400': false,
  'Fri Jan 08 2018 00:00:00 GMT-0400': false,
  'Thu Jan 07 2018 00:00:00 GMT-0400': false,
  'Wed Jan 06 2018 00:00:00 GMT-0400': false,
  'Tue Jan 05 2018 00:00:00 GMT-0400': true,
  'Mon Jan 04 2018 00:00:00 GMT-0400': true
}

Tests

Tests require mocha to be installed globally. In the parent of the package, run

npm run test

Authors

  • Jon Samp

License

This project is licensed under the MIT License - see the LICENSE.md file for details.