Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Calculate daily, weekly, and monthly streaks from a list of times.
JavaScript CoffeeScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
src
.gitignore
Cakefile
README.md describe day of the week whitelist in readme
example.html streaker.js now works in browser. need to rework tests
package.json tests passing
streaker.js

README.md

streaker-js

Calculate daily, weekly, and monthly streaks from a list of Javscript Date objects.

Examples

Determine the current streak (daily by default):

// Assuming the date is currently new Date(2012, 6, 8) :
var dates = [ new Date(2012, 6, 6), new Date(2012, 6, 7), new Date(2012, 6, 8) ];
var currentStreak = streaker(dates).current(); // 3

var datesWithoutToday = [ new Date(2012, 6, 6), new Date(2012, 6, 7) ];
var currentStreak = streaker(datesWithoutToday).current(); // 2 - the streak isn't broken until the end of the day

var datesWithoutTodayOrYesterday = [ new Date(2012, 6, 6) ];
var currentStreak = streaker(datesWithoutTodayOrYesterday).current(); // 0 - the streak is broken as of today

Optionally pass in a whitelist for days of the week to consider. Days not in the list will be ignored when calculating the streak.

// Assuming the date is currently new Date(2012, 6, 8), which is a Sunday :
var weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
var dates = [ new Date(2012, 6, 6), new Date(2012, 6, 7), new Date(2012, 6, 8) ];
var currentStreak = streaker(dates, 'daily', weekdays).current(); // 1, because today and yesterday don't count

// Assuming the date is currently new Date(2012, 6, 9), which is a Monday :
var weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']
var dates = [ new Date(2012, 6, 6) ]; // last friday
var currentStreak = streaker(dates, 'daily', weekdays).current(); // 1, because the weekend didn't count

Weekly:

// Assuming the date is currently new Date(2012, 6, 8), which is a Sunday :
// Week starts on Sunday
var dates = [ new Date(2012, 5, 26), new Date(2012, 6, 1), new Date(2012, 6, 8) ];
var currentStreak = streaker(dates, 'weekly').current(); // 3

Monthly:

// Assuming the date is currently new Date(2012, 6, 8) :
var dates = [ new Date(2012, 4, 8), new Date(2012, 5, 8), new Date(2012, 6, 8) ];
var currentStreak = streaker(dates, 'monthly').current(); // 3

Installation

Node

Install via npm:

npm install streaker-js

Running Tests

Clone this repo:

$ git clone https://github.com/iancanderson/streaker-js.git

Install the dependencies:

$ npm install

Run the tests:

$ cake test
Something went wrong with that request. Please try again.