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 = 2012 6 6 2012 6 7 2012 6 8 ;var currentStreak = current; // 3var datesWithoutToday = 2012 6 6 2012 6 7 ;var currentStreak = current; // 2 - the streak isn't broken until the end of the dayvar datesWithoutTodayOrYesterday = 2012 6 6 ;var currentStreak = 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 = 2012 6 6 2012 6 7 2012 6 8 ;var currentStreak = 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 = 2012 6 6 ; // last fridayvar currentStreak = 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 Sundayvar dates = 2012 5 26 2012 6 1 2012 6 8 ;var currentStreak = current; // 3
Monthly:
// Assuming the date is currently new Date(2012, 6, 8) :var dates = 2012 4 8 2012 5 8 2012 6 8 ;var currentStreak = 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