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

subtractWorkingTime of "moment.invalid(/* undefined */)" loops infinitely #31

Open
diequeiroz opened this issue Nov 8, 2017 · 2 comments

Comments

@diequeiroz
Copy link

Using 0.7.1

This condition:

var moment = require('moment-business-time');
moment(undefined, 'DD/MM/YYYY 00:00').subtractWorkingTime(1, 'day')

Makes this function loops infinitely.

@lennym
Copy link
Owner

lennym commented Nov 9, 2017

Thanks for opening an issue @diequeiroz - I'll try to get this fixed asap, but it might be a few days before I get a chance to look at it. I'd be happy to merge a PR in the interim.

@mark-monteiro
Copy link

For anyone else who stumbles upon this issue, you can patch this fairly easily from your own code. This Typescript code example patches the functions to return the unmodified provided moment object if it is invalid. You could also throw an error if you want.

import moment, { Duration, Moment, unitOfTime } from "moment";
import "moment-business-time";

const addWorkigTimeOriginal = moment.fn.addWorkingTime;
const subtractWorkingTimeOriginal = moment.fn.subtractWorkingTime;

moment.fn.addWorkingTime = function (this: Moment, ...args: Array<number | unitOfTime.Base>): Moment {
  if (!this.isValid()) return this;
  return addWorkigTimeOriginal.call(this, ...args);
};

moment.fn.subtractWorkingTime = function (this: Moment, ...args: Array<number | unitOfTime.Base>): Moment {
  if (!this.isValid()) return this;
  return subtractWorkingTimeOriginal.call(this, ...args);
};

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

3 participants