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

time-periods

v1.1.0

Published

An utility to handle time period operation.

Downloads

21

Readme

Time Periods

Docs | npm

A utility library for working with periods of time in JavaScript. It allows you to create time periods, divide them into smaller periods, subtract time periods, and perform other common operations on time periods.

Installation

The library is available as an npm package. To install the package use:

npm install time-periods --save 

Usage

Creating Time Periods

const { TimePeriod, TimePeriodList } = require('time-periods');

// Create a time period from two dates
const start = new Date('2024-01-01T10:00:00');
const end = new Date('2024-01-01T12:00:00');
const timePeriod = new TimePeriod(start, end);

Subtracting Time Periods

// Subtract a time period
const timePeriodToSubtract = new TimePeriod(new Date('2024-01-01T10:30:00'), new Date('2024-01-01T11:00:00'));
const remainingPeriods = timePeriod.subtract(timePeriodToSubtract);
console.log(remainingPeriods); 
// => 
// [
//     TimePeriod {
//         startDateTime: 2024-01-01T10:00:00.000Z,
//         endDateTime: 2024-01-01T10:30:00.000Z,
//     },
//     TimePeriod {
//         startDateTime: 2024-01-01T11:00:00.000Z,
//         endDateTime: 2024-01-01T12:00:00.000Z,
//     }
// ]

Working with TimePeriodList

const timePeriodList = new TimePeriodList([
  new TimePeriod(new Date('2024-01-01T08:00:00'), new Date('2024-01-01T09:00:00')),
  new TimePeriod(new Date('2024-01-01T10:00:00'), new Date('2024-01-01T12:00:00'))
]);

const periodsToSubtract = [
  new TimePeriod(new Date('2024-01-01T08:30:00'), new Date('2024-01-01T10:30:00'))
];

const subtractedTimePeriodList = timePeriodList.mergeMultiple().subtractMultiple(periodsToSubtract);
console.log(subtractedTimePeriodList);
// => 
// TimePeriodList {
//     timePeriodList: [
//         TimePeriod {
//             startDateTime: 2024-01-01T08:00:00.000Z,
//             endDateTime: 2024-01-01T08:30:00.000Z,
//         },
//         TimePeriod {
//             startDateTime: 2024-01-01T10:30:00.000Z,
//             endDateTime: 2024-01-01T12:00:00.000Z,
//         }
//     ]
// }

Splitting a Time Period

const splitPeriods = timePeriod.divideByLength(15);
console.log(splitPeriods); // Splits the time period into 8 equal parts, with duration = 15mins.

Iterating over TimePeriodList

splitPeriods.forEach((period, index) => {
  console.log(`Period ${index}: ${period.startDateTime} to ${period.endDateTime}`);
});

TimePeriod API Reference

Constructor

new TimePeriod(startDateTime, endDateTime)

Creates a new TimePeriod object.

startDateTime (Date): The start of the time period. endDateTime (Date): The end of the time period. Throws an TypeError if the startDateTime or endDateTime is not a valid Date object. Throws an RangeError if startDateTime is greater than endDateTime.

Instance properties

TimePeriod.prototype.divideByLength(divisorInMins):

Divides a timePeriod into periods with same duration provided.

TimePeriod.prototype.merge(timePeriod):

Merges two timePeriod into one period if no overlap bewteen two timePeriod, this timePeriod will be return.

TimePeriod.prototype.subtract(timePeriod, minimumDurationInMins):

Subtracts the overlapped time period bewteen this timePeriod and provided timePeriod from this timePeriod.

TimePeriod.prototype.trimEnd(durationInMins):

Trims the end of this timePeriod by the provided duration.

TimePeriod.prototype.contains(timePeriod):

Returns true if the provided timePeriod is fully contained within this timePeriod.

Static methods

TimePeriod.isNotLessThanDuration(startDateTime, endDateTime, duration):

Compares the duration of a pair of start time and end time.

TimePeriodList API Reference

Constructor

new TimePeriodList(timePeriodList);

Creates a new TimePeriodList object with an array of TimePeriod objects.

Instance properties

TimePeriodList.prototype.divideAllByLength(divisorInMins):

Divides all the time period in this timePeriodList into periods with same duration provided.

TimePeriodList.prototype.forEach(callback):

Iterates over each time period in the list and applies the provided callback function. The callback is invoked with three arguments: the current time period, the index, and the entire timePeriodList.

TimePeriodList.prototype.get(index):

Gets the first (index + 1) timePeriod in timePeriodList.

TimePeriodList.prototype.getAll():

Gets an array contains all the timePeriod in this timePeriodList.

TimePeriodList.prototype.mergeMultiple(timePeriodList):

Merges all timePeriod, until no overlap bewteen every timePeriod.

TimePeriodList.prototype.subtractMultiple(timePeriodToSubtractList, minimumDurationInMins):

Subtracts the overlapped time period between provided timePeriodList and this timePeriodList from this timePeriodList.

TimePeriodList.prototype.contains(timePeriod):

Returns true if the provided timePeriod is fully contained within any timePeriod of this timePeriodList.

TimePeriodList.prototype.trimAllEnd(durationInMins):

Trims the end of all timePeriod in this timePeriodList by the provided duration.

License

MIT © Stanley Wang (You Hao Wang)