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

feature request: iterate over x, not just 1 #93

Closed
jduhls opened this issue Sep 11, 2015 · 2 comments
Closed

feature request: iterate over x, not just 1 #93

jduhls opened this issue Sep 11, 2015 · 2 comments
Milestone

Comments

@jduhls
Copy link

jduhls commented Sep 11, 2015

I made an adjustment to the iterate feature allowing programmer to control integer in the moment "add" method. Added a "step" argument. Not sure if best way to implement. Starting around line 198 in moment-range.js:

/**
 * Iterate over the date range by a given date range, executing a function
 * for each sub-range.
 *
 * @param {(!DateRange|String)} range Date range to be used for iteration or
 *                                    shorthand string (shorthands:
 *                                    http://momentjs.com/docs/#/manipulating/add/)
 * @param {!DateRange~by} hollaback Callback
 * @param {!boolean} exclusive Indicate that the end of the range should not
 *                             be included in the iter.
 * @param {!Integer} step Indicate number of moment shorthand strings to moment.add
 *
 * @return {DateRange} `this`
 */
DateRange.prototype.by = function(range, hollaback, exclusive, step) {
  if (typeof range === 'string') {
    _byString.call(this, range, hollaback, exclusive, step);
  }
  else {
    _byRange.call(this, range, hollaback, exclusive);
  }
  return this;
};


/**
 * Callback executed for each sub-range.
 *
 * @callback DateRange~by
 *
 * @param {!Moment} current Current moment object for iteration
 */

/**
 * @private
 */
function _byString(interval, hollaback, exclusive, step) {
  var current = moment(this.start);

  while (this.contains(current, exclusive)) {
    hollaback.call(this, current.clone());
    if((typeof step === 'number') && Math.floor(step) === step){
      current.add(step, interval);
    } else {
      current.add(1, interval);
    }

  }
}

...p.s. I like "hollaback".

@gf3
Copy link
Contributor

gf3 commented Dec 8, 2016

awesome suggestion—i've added this functionality to the upcoming v3 release (see the commit above)

@gf3 gf3 added the done-v3 label 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

2 participants