Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't iterate a range in reverse order #68

Closed
EvHaus opened this issue Mar 23, 2015 · 10 comments
Closed

Can't iterate a range in reverse order #68

EvHaus opened this issue Mar 23, 2015 · 10 comments
Assignees
Milestone

Comments

@EvHaus
Copy link

EvHaus commented Mar 23, 2015

I want to iterate over a date range from latest to earliest date, but this doesn't seem to work:

var start = moment("2011-11-27", "YYYY-MM-DD");
var end   = moment("2011-04-15", "YYYY-MM-DD");
var range = moment().range(start, end);
range.by('days', function (day) {
    // Never get's called. :(
    console.log(day)
});
@mickdelaney
Copy link

+1

@gf3 gf3 added the bug label May 29, 2015
@gf3 gf3 self-assigned this May 29, 2015
@gf3
Copy link
Contributor

gf3 commented May 30, 2015

how would you feel if moment-range automatically re-arranged your range so that start was always the min and end was always the max?

@EvHaus
Copy link
Author

EvHaus commented Jun 1, 2015

@gf3 I'm not sure how that would help with my issue.

@gf3
Copy link
Contributor

gf3 commented Jun 1, 2015

@globexdesigns well it would always iterate the correct amount, just not in the reverse order

@atran
Copy link

atran commented Oct 19, 2015

+1

2 similar comments
@NGMarmaduke
Copy link

+1

@nnnnathann
Copy link

+1

@yozef
Copy link

yozef commented Oct 7, 2016

+1
@gf3 Maybe we can add an additional parameter

var start = moment("2011-11-27", "YYYY-MM-DD");
var end   = moment("2011-04-15", "YYYY-MM-DD");
var range = moment().range(start, end);
range.by('d', function (moment) {
   console.log(moment)
}, true, true); // last Param: true is the order in reverse, false would be searching as is now, forward.

Modifying the lib to the below would work:

// line 214
DateRange.prototype.by = function(range, hollaback, exclusive, reverse) {
  if (typeof range === 'string') {
    _byString.call(this, range, hollaback, exclusive); // not needed for my case
  }
  else {
    _byRange.call(this, range, hollaback, exclusive, reverse);
  }
  return this;
};

// then on line 248
function _byRange(interval, hollaback, exclusive, reverse) {
  var div = this / interval;
  var l = Math.floor(div);

  if (l === Infinity) { return; }
  if (l === div && exclusive) {
    l--;
  }

  if (!reverse) {
      for (var i = 0; i <= l; i++) {
        hollaback.call(this, moment(this.start.valueOf() + interval.valueOf() * i));
      }
  } else { // Loops backwards?
      for (var i = l; i >= 0; i--) {
        hollaback.call(this, moment(this.end.valueOf() - interval.valueOf() * i));
      }
  }

}

gf3 added a commit that referenced this issue Dec 8, 2016
* Pass options as object
* New methods use iterators
* Added reversal methods
* Discrete methods for different iterator types
@gf3
Copy link
Contributor

gf3 commented Dec 8, 2016

adding discrete methods for reverse iteration in v3, you can follow the progress here: https://github.com/gf3/moment-range/tree/3-dev

example usage here

thanks for your patience everyone

gf3 added a commit that referenced this issue Dec 8, 2016
* Pass options as object
* New methods use iterators
* Added reversal methods
* Discrete methods for different iterator types
@gf3 gf3 added done-v3 and removed bug labels Dec 8, 2016
@gf3 gf3 modified the milestone: 3.0.0 Jan 9, 2017
gf3 added a commit that referenced this issue Jan 10, 2017
* Go full-blown ES6 via webpack/babel
* Flow typing
* Better documentation
* Remove bower
* Remove grunt in favour of webpack
* Remove jshint in favour of eslint
* Remove mocha/should in favour of karma/mocha/expect.js
* New usage:
    import Moment from 'moment';
    import { DateRange, extendMoment } from 'moment-js';
    const moment = extendMoment(Moment);
* Add `by`, `reverseBy`, `byRange`, `reverseByRange` (#68)
    - Pass options as object
    - New methods use iterators
    - Added reversal methods
    - Discrete methods for different iterator types
* Fix issue with `moment#within` and moment-timezone (#127)
* Pass optional rounded argument in `diff` (#104)
* Add ability to iterate over a range by a step (#93)
* Add `adjacent` method and option to `overlaps` (#92 #112)
* Update short-hand units (#134)
* Add `duration` as an alias of `diff` (#64)
* Update packaging rules and scripts
* Fix typo in comment (#133)
* Update documentation
* Add documentation about range creation (thanks @seanwendt)
@gf3
Copy link
Contributor

gf3 commented Jan 10, 2017

just opened a PR for 3.0.0 which incorporates these changes. feel free to leave feedback or raise any questions you may have. thanks again for your contributions

@gf3 gf3 closed this as completed Jan 10, 2017
gf3 added a commit that referenced this issue Jan 10, 2017
* Go full-blown ES6 via webpack/babel
* Flow typing
* Better documentation
* Remove bower
* Remove grunt in favour of webpack
* Remove jshint in favour of eslint
* Remove mocha/should in favour of karma/mocha/expect.js
* New usage:
    import Moment from 'moment';
    import { DateRange, extendMoment } from 'moment-js';
    const moment = extendMoment(Moment);
* Add `by`, `reverseBy`, `byRange`, `reverseByRange` (#68)
    - Pass options as object
    - New methods use iterators
    - Added reversal methods
    - Discrete methods for different iterator types
* Fix issue with `moment#within` and moment-timezone (#127)
* Pass optional rounded argument in `diff` (#104)
* Add ability to iterate over a range by a step (#93)
* Add `adjacent` method and option to `overlaps` (#92 #112)
* Update short-hand units (#134)
* Add `duration` as an alias of `diff` (#64)
* Update packaging rules and scripts
* Fix typo in comment (#133)
* Update documentation
* Add documentation about range creation (thanks @seanwendt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants