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

@biblebytes/streak

v1.0.1

Published

The Streak Library is a tool for analyzing a list of dates to track activity patterns.

Downloads

148

Readme


Overview

The Streak Library is a tool for analyzing a list of dates to track activity patterns. It calculates total unique days, checks for streaks today and yesterday, and determines current, latest, and longest streaks. This library is useful for applications focused on habit tracking or monitoring consecutive days of activity.

Installation

To install the package via npm, run:

npm i @biblebytes/streak

Get Status

Calculates streak status on a list of dates.

function Process(dates:DateString[]): Status

Examples:

import { GetStatus, type DateString, GetDate } from "@biblebytes/streak";

const dates1:DateString[] = [
    "2022-12-21", "2022-12-22", "2022-12-23",
    "2022-12-24", "2022-12-25"
];

GetStatus(dates1);
// {
//     totalDays: 5,
//     hasStreakToday: false,
//     hasStreakYesterday: false,
//     currentStreak: 0,
//     latestStreak: 5,
//     longestStreak: 5,
// }

const dates2:DateString[] = [
    "2024-09-29", "2024-09-30", "2024-10-01",
    "2024-10-02", "2024-10-03", "2024-10-04",
    getDateDaysAgo(3), getDateDaysAgo(2), getDateDaysAgo(1)
];
GetStatus(dates2);
// {
//     totalDays: 9,
//     hasStreakToday: false,
//     hasStreakYesterday: true,
//     currentStreak: 3,
//     latestStreak: 3,
//     longestStreak: 6,
// }

const dates3:DateString[] = [
    "2024-09-29", "2024-09-30", "2024-10-01",
    "2024-10-02", "2024-10-03", "2024-10-04",
    getDateDaysAgo(3), getDateDaysAgo(2), getDateDaysAgo(1),
    GetDate()
];
GetStatus(dates3);
// {
//     totalDays: 10,
//     hasStreakToday: true,
//     hasStreakYesterday: true,
//     currentStreak: 4,
//     latestStreak: 4,
//     longestStreak: 6,
// }

Get Date

Get current date as a date string

function GetDate(): DateString

Examples:

import { GetStatus, type DateString, GetDate } from "@biblebytes/streak";

// On September, 1st 2024
GetDate();
// "2024-09-01"

Date String

The date string is a format that is utilized by the library to store dates as strings. The date format is YYYY-MM-DD with zero padding on the month and day identifier.

Examples:

import { type DateString } from "@biblebytes/streak";

let date1:DateString = "2001-12-29";
let date2:DateString = "2001-01-01";

Status Structure

The status structure types, is an object that stores all the streak information for a list of dates.

  • totalDays - total amount of days provided
  • hasStreakToday - true if the list contains todays date
  • hasStreakYesterday - true if the list contains yesterdays date
  • currentStreak - length of current streak
  • latestStreak - length of most recent streak
  • longestStreak - length of longest streak over all dates

Streaks are reset when a full day ellipses from the last recorded activity. This means hasStreakToday could be false, but the streak will still be held until the end of the day, assuming hasStreakYesterday is true.

export type Status = {
    totalDays: number;
    hasStreakToday: boolean;
    hasStreakYesterday: boolean;
    currentStreak: number;
    latestStreak: number;
    longestStreak: number;
};