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

granularity

v1.0.0

Published

Timebuckets for dates.

Downloads

7

Readme

granularity

Timebuckets for dates.

Group date/timestamps/other date objects into timebuckets that you can traverse.

Really helpful for storing and querying dates from data bases.

Usage:

var bucket = Granularity(duration, ?origin)

Creates a timebucket.

Args:

  • duration - (Optional, Default=1ms). The width of each timebucket (ms). 3600000 means 1 hour. 0 (Zero) means all time passed. If duration is negative, it'll start from the previous Timebucket.
  • origin - (Optional, Date|timestamp|iso date, default=now). The first timebucket. Any date string that Date object can read is acceptable here.

Return value:

The return value is an extended Date object, which will respond to all methods that are associated with Date, along with several additional methods.

Example:

console.log('\nCreate a bucket for the next 24hr');
var next24 = Granularity(1000 * 3600 * 24);

console.log('\nCreate a bucket for the last 30 days')
var last30Days = Granularity(0 - 1000 * 3600 * 24 * 30); // Note the duration is negative

console.log('\nCreate a bucket for all time possible');
var allTime = Granularity(0);

console.log('\nEvery Two Hour Bucket');
var everyTwoHourBucket = Granularity(7200000, '2014-08-19T00:00:00.000Z'); // Origins at today

console.log('\nEvery hour on the half-hour')
var everyHourOnTheHalf = Granularity(3600000, '2014-08-07T00:30:00Z') // Origins at 2014-08-07T00:30:00Z

bucket.start()

Returns the Date() of the bucket itself.

Example

console.log('\nCreate a bucket for the last 30 days')
var last30Days = Granularity(0 - 1000 * 3600 * 24 * 30); // Note the duration is negative
console.log(last30Days.start().toISOString()) // Should be 30 days ago

bucket.end()

Returns a Date() that indicates when the bucket should end. Basically it's the next bucket minus 1 ms.

Example

console.log('\nCreate a bucket for the last 30 days')
var last30Days = Granularity(0 - 1000 * 3600 * 24 * 30); // Note the duration is negative
console.log(last30Days.start().toISOString()) // Should be 30 days ago
console.log(last30Days.end().toISOString()) // Should be now - 1 ms

bucket.skip(?interval=1) || bucket.next(?interval=1)

Skips interval timebuckets forward, or backwards (in case interval is negative).

Example

console.log('\nSkip to the next Timebucket');
// 2014-08-20T02:00:00.000Z
console.log(everyTwoHourBucket.skip().toISOString());
console.log(everyTwoHourBucket.next().toISOString());

console.log('\nSkip to the Timebucket after the next one');
// 2014-08-20T04:00:00.000Z
console.log(everyTwoHourBucket.skip(2).toISOString());
console.log(everyTwoHourBucket.next(2).toISOString());

bucket.previous(?interval=1)

Same as bucket.skip, just as negative value, or in other words: bucket.skip(-1)===bucket.previous(1).

Example

console.log('\nSkip to the previous Timebucket');
// 2014-08-19T22:00:00.000Z
console.log(everyTwoHourBucket.skip(-1).toISOString());
console.log(everyTwoHourBucket.previous().toISOString());

console.log('\nSkip to the Timebucket before the previous one');
// 2014-08-19T20:00:00.000Z
console.log(everyTwoHourBucket.skip(-2).toISOString());
console.log(everyTwoHourBucket.previous(2).toISOString());

bucket.floor(?date=now)

Create a new Timebucket that contains date.

date - Whether Date object or any type of string that is readable by Date object. Default is now.

Example

console.log('\nEvery Two Hour Bucket');
var everyTwoHourBucket = Granularity(7200000, today.toISOString()); // Origins at today
console.log(everyTwoHourBucket.floor('2014-08-08T12:35:00Z').toISOString()) // 2014-08-08T12:00:00.000Z
console.log(everyTwoHourBucket.floor('2014-08-08T13:35:00Z').toISOString()) // 2014-08-08T12:00:00.000Z
console.log(everyTwoHourBucket.floor('2014-08-08T14:35:00Z').toISOString()) // 2014-08-08T14:00:00.000Z

console.log('\nEvery hour on the half-hour')
var everyHourOnTheHalf = Granularity(3600000, '2014-08-07T00:30:00Z') // Origins at 2014-08-07T00:30:00Z

console.log(everyHourOnTheHalf.floor('2014-08-08T12:35:00Z').toISOString()) // 2014-08-08T12:30:00.000Z
console.log(everyHourOnTheHalf.floor('2014-08-08T13:15:00Z').toISOString()) // 2014-08-08T12:30:00.000Z
console.log(everyHourOnTheHalf.floor('2014-08-08T14:35:00Z').toISOString()) // 2014-08-08T14:30:00.000Z

bucket.ceil(?date=now)

Similar to bucket.floor, with one difference. It creates a Timebucket after bucket.floor. An alternative way of achieving this, would be: bucket.floor(date).next()

Example

console.log('\nFloor and Ceil');

console.log(everyTwoHourBucket.floor('2014-08-08T13:35:00Z').toISOString()) // 2014-08-08T12:00:00.000Z
console.log(everyTwoHourBucket.ceil('2014-08-08T13:35:00Z').toISOString()) // 2014-08-08T14:00:00.000Z

bucket.serialize()

Serializes the timebucket into a string.

Example

console.log('\nSerialize a Timebucket');
// 7200000~0
console.log(everyTwoHourBucket.serialize());

bucket.unserialize(str)

Unserializes a Timebucket string, by using the origin of the current Timebucket.

Example

console.log('\nUnserialize a Timebucket that origins form another Timebucket');
// 2014-08-19T00:00:00.000Z
console.log(everyTwoHourBucket.unserialize('7200000~0').start().toISOString());

Granularity.unserialize(str, ?origin=now)

Unserializes a Timebucket string, by using an origin of your choice.

origin - (Default=now). Whether Date object or any Date string that is acceptable by Date object.

Example

console.log('\nUnserialize a Timebucket that origins at the current date');
// 2014-08-20T00:00:00.000Z
console.log(Granularity.unserialize('7200000~0').start().toISOString());

console.log('\nUnserialize a Timebucket that origins at 0 epoch time');
// 1970-01-01T00:00:00.000Z
console.log(Granularity.unserialize('7200000~0', 0).start().toISOString());

Granularity.today()

Creates a daily Timebucket, that starts from today.

Example

console.log('Today:')
var today = Granularity.today();
console.log(today.toISOString());

Complete Example

var Granularity = require('granularity');

console.log('Today:')
var today = Granularity.today();
console.log(today.toISOString());

console.log('\nCreate a bucket for the next 24hr');
var next24 = Granularity(1000 * 3600 * 24);
console.log(next24.start().toISOString()); // Should be now
console.log(next24.end().toISOString()); // Should be tomorrow - 1 ms

console.log('\nCreate a bucket for the last 30 days')
var last30Days = Granularity(0 - 1000 * 3600 * 24 * 30); // Note the duration is negative
console.log(last30Days.start().toISOString()) // Should be 30 days ago
console.log(last30Days.end().toISOString()) // Should be now - 1 ms

console.log('\nCreate a bucket for the previous 30 day period');
console.log(last30Days.previous().start().toISOString()) // Should be 60 days ago
console.log(last30Days.previous().end().toISOString()) // Should be 30 days ago - 1 ms

console.log('\nCreate a bucket for all time possible');
var allTime = Granularity(0);
console.log(allTime.start().toISOString()) // Should be 0 in Epoch time
console.log(allTime.end().toISOString()) // Should now - 1 ms

console.log('\nEvery Two Hour Bucket');
var everyTwoHourBucket = Granularity(7200000, today.toISOString()); // Origins at today
console.log(everyTwoHourBucket.floor('2014-08-08T12:35:00Z').toISOString()) // 2014-08-08T12:00:00.000Z
console.log(everyTwoHourBucket.floor('2014-08-08T13:35:00Z').toISOString()) // 2014-08-08T12:00:00.000Z
console.log(everyTwoHourBucket.floor('2014-08-08T14:35:00Z').toISOString()) // 2014-08-08T14:00:00.000Z

console.log('\nEvery hour on the half-hour')
var everyHourOnTheHalf = Granularity(3600000, '2014-08-07T00:30:00Z') // Origins at 2014-08-07T00:30:00Z

console.log(everyHourOnTheHalf.floor('2014-08-08T12:35:00Z').toISOString()) // 2014-08-08T12:30:00.000Z
console.log(everyHourOnTheHalf.floor('2014-08-08T13:15:00Z').toISOString()) // 2014-08-08T12:30:00.000Z
console.log(everyHourOnTheHalf.floor('2014-08-08T14:35:00Z').toISOString()) // 2014-08-08T14:30:00.000Z

console.log('\nFloor and Ceil');

console.log(everyTwoHourBucket.floor('2014-08-08T13:35:00Z').toISOString()) // 2014-08-08T12:00:00.000Z
console.log(everyTwoHourBucket.ceil('2014-08-08T13:35:00Z').toISOString()) // 2014-08-08T14:00:00.000Z

console.log('\nStart of the Timebucket');
// Let's recreate the every to hour bucket for a fixed date
everyTwoHourBucket = Granularity(1000 * 3600 * 2, '2014-08-19T00:00:00.000Z');
// 2014-08-20T00:00:00.000Z
console.log(everyTwoHourBucket.start().toISOString());

console.log('\nEnd of the Timebucket');
// 2014-08-20T01:59:59.999Z
console.log(everyTwoHourBucket.end().toISOString());

console.log('\nSkip to the next Timebucket');
// 2014-08-20T02:00:00.000Z
console.log(everyTwoHourBucket.skip().toISOString());
console.log(everyTwoHourBucket.next().toISOString());

console.log('\nSkip to the Timebucket after the next one');
// 2014-08-20T04:00:00.000Z
console.log(everyTwoHourBucket.skip(2).toISOString());
console.log(everyTwoHourBucket.next(2).toISOString());

console.log('\nSkip to the previous Timebucket');
// 2014-08-19T22:00:00.000Z
console.log(everyTwoHourBucket.skip(-1).toISOString());
console.log(everyTwoHourBucket.previous().toISOString());

console.log('\nSkip to the Timebucket before the previous one');
// 2014-08-19T20:00:00.000Z
console.log(everyTwoHourBucket.skip(-2).toISOString());
console.log(everyTwoHourBucket.previous(2).toISOString());

console.log('\nMS diff between two dates');
// -3600
console.log(everyTwoHourBucket.diff(new Date(), new Date().getTime()+3600));
console.log(Granularity.diff(new Date(), new Date().getTime()+3600));

console.log('\nSerialize a Timebucket');
// 7200000~0
console.log(everyTwoHourBucket.serialize());

console.log('\nUnserialize a Timebucket that origins at the current date');
// 2014-08-20T00:00:00.000Z
console.log(Granularity.unserialize('7200000~0').start().toISOString());

console.log('\nUnserialize a Timebucket that origins at 0 epoch time');
// 1970-01-01T00:00:00.000Z
console.log(Granularity.unserialize('7200000~0', 0).start().toISOString());

console.log('\nUnserialize a Timebucket that origins form another Timebucket');
// 2014-08-19T00:00:00.000Z
console.log(everyTwoHourBucket.unserialize('7200000~0').start().toISOString());

install

With npm do:

npm install granularity

license

MIT