icambron / twix.js

:hourglass::left_right_arrow: A date range plugin for moment.js
https://isaaccambron.com/twix.js/
MIT License
379 stars 54 forks source link

Ability to reset a twix iterator? #85

Closed jamesmorgan closed 8 years ago

jamesmorgan commented 8 years ago

Hi, thanks for a wicked library its saved me lots of time.

Do you know if there is a nice way to reset a twix iterator?

I want to re-use a iterator several times but I need to always create a new one like as follows. (basic example)

var range = moment(startDate).startOf('day').twix(moment(endDate).startOf('day')).iterate('days');

Is there a inbuilt way e.g. range.reset() or is the preferred way to simply create a new one each time?

Many thanks

James

icambron commented 8 years ago

Thanks!

Yeah, you have to recreate it. There wouldn't be anything wrong with adding a reset() function, but they're super cheap to make (all the work is in the iteration itself), so it's probably not necessary. Here's what I'd do:

function makeDayRange(startDate, endDate){
  var t = moment(startDate).startOf('day').twix(moment(endDate).startOf('day'));
  return function(){
    return t.iterate('days');
  };
};

var range = makeDayRange(start, end);

//use it
var r = range();
r.next(); //etc

//reset it
range().next();

If this kind of thing doesn't meet your needs, LMK and I can reopen.

jamesmorgan commented 8 years ago

Thanks, that factory method should suffice. :+1: