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

$add is not compliant with dates #73

Closed
javiercbk opened this issue Oct 18, 2017 · 0 comments
Closed

$add is not compliant with dates #73

javiercbk opened this issue Oct 18, 2017 · 0 comments
Labels

Comments

@javiercbk
Copy link

javiercbk commented Oct 18, 2017

From mongo documentation:

"Adds numbers together or adds numbers and a date. If one of the arguments is a date, $add treats the other arguments as milliseconds to add to the date." documentation link.

The current implementation just sums values, this change would make it work as intended

/**
   * Computes the sum of an array of numbers.
   *
   * @param obj
   * @param expr
   * @returns {Object}
   */
  $add (obj, expr) {
    var args = computeValue(obj, expr)
    var date = args.find(function(argument) {
      return argument instanceof Date;
    })
    if (date) {
      args = args.map(function(argument) {
        if (argument instanceof Date) {
          return argument.getTime()
        }
        return argument
      });
    }
    var reduced = reduce(args, function (acc, num) {
      return acc + num
    }, 0);
    if (date) {
      return new Date(reduced);
    }
    return reduced;
  },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants