Skip to content

Commit

Permalink
Merge branch 'master' into chore/update-package-lock
Browse files Browse the repository at this point in the history
  • Loading branch information
eduolalo authored Apr 16, 2020
2 parents 04339ed + 01e3e6c commit 2f4292e
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 48 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Continuous Integration

on: [push]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macOS-10.14, windows-2016, ubuntu-latest]
node: [4, 6, 8, 10, 12]
steps:
- name: Checkout
uses: actions/checkout@v1
- run: npm install
- run: npm test
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# moment-business-days

<a href="https://github.com/kalmecak/moment-business-days/actions" target="_blank">
<img alt="Version" src="https://github.com/kalmecak/moment-business-days/workflows/Continuous%20Integration/badge.svg?branch=master">
</a>

This is a [Moment.js](https://github.com/moment/moment/) plugin that allows you to work with only business days
(Monday to Friday). You can customize the working week, and also set custom dates for holidays to exclude them from
being counted as business days, for example **national holidays**.
Expand All @@ -7,7 +12,7 @@ being counted as business days, for example **national holidays**.
* This plugin works on both server and client side.
* This plugin is based on [momentjs-business](https://github.com/leonardosantos/momentjs-business).
* All contributions are welcome.
* **Thaks to the contributors for making this plugin better!!**
* **Thanks to the contributors for making this plugin better!!**

## Usage

Expand Down Expand Up @@ -89,6 +94,15 @@ var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','
// diff = 5
```

Note that the default behavior of `businessDiff` is to return an **absolute** value,
which is a departure from moment's `diff`. To match the behavior of `diff` pass
`true` as the second argument to `businessDiff`:

```javascript
var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(moment('05-15-2017','MM-DD-YYYY'), true);
// diff = -5
```

#### `.businessAdd(days)` => Moment

Will add the given number of days skipping non-business days, returning a **Moment.js** object:
Expand Down Expand Up @@ -119,6 +133,16 @@ moment('30-01-2015', 'DD-MM-YYYY').nextBusinessDay()._d // Mon Feb 02 2015 00:00
moment('02-02-2015', 'DD-MM-YYYY').nextBusinessDay()._d //Tue Feb 03 2015 00:00:00 GMT-0600 (CST)
```

By default only 7 days into the future are checked for the next business day. To search beyond 7 days
set the nextBusinessDayLimit (as a number) higher.
````javascript
var moment = require('moment-business-days');

moment.updateLocale('us', {
nextBusinessDayLimit: 31
});
````

#### `.prevBusinessDay()` => Moment

Will retrieve the previous business date as a **Moment.js** object:
Expand All @@ -131,6 +155,16 @@ moment('02-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d // Fri Jan 30 2015 00:00
moment('03-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d //Mon Feb 02 2015 00:00:00 GMT-0600 (CST)
```

By default only the last 7 days are checked for the previous business day. To search beyond 7 days
set the prevBusinessDayLimit (as a number) higher.
````javascript
var moment = require('moment-business-days');

moment.updateLocale('us', {
prevBusinessDayLimit: 31
});
````

#### `.monthBusinessDays()` => Moment[]

Retrieve an array of the business days in the month, each one is a **Moment.js** object.
Expand Down
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';

declare module 'moment' {
interface Moment {
Expand All @@ -18,11 +18,14 @@ declare module 'moment' {
monthNaturalDays(fromToday?: boolean): Moment[];
monthBusinessWeeks(fromToday?: boolean): Moment[][];
monthNaturalWeeks(fromToday?: boolean): Moment[][];
businessWeeksBetween(endDate: Moment): Moment[][];
}

interface LocaleSpecification {
holidays?: string[];
holidayFormat?: string;
nextBusinessDayLimit?: number;
prevBusinessDayLimit?: number;
workingWeekdays?: number[];
}
}
Expand Down
40 changes: 32 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ moment.fn.businessDaysIntoMonth = function () {
return businessDaysIntoMonth;
};

moment.fn.businessDiff = function (param) {
moment.fn.businessDiff = function (param, relative) {
var d1 = this.clone();
var d2 = param.clone();
var positive = d1 >= d2;
var start = d1 < d2 ? d1 : d2;
var end = d2 > d1 ? d2 : d1;

Expand All @@ -67,6 +68,10 @@ moment.fn.businessDiff = function (param) {
start.add(1, 'd');
}

if (relative) {
return positive ? daysBetween : -daysBetween;
}

return daysBetween;
};

Expand Down Expand Up @@ -102,8 +107,11 @@ moment.fn.businessSubtract = function (number, period) {
};

moment.fn.nextBusinessDay = function () {
var locale = this.localeData();

var loop = 1;
var limit = 7;
var defaultNextBusinessDayLimit = 7;
var limit = locale._nextBusinessDayLimit || defaultNextBusinessDayLimit;
while (loop < limit) {
if (this.add(1, 'd').isBusinessDay()) {
break;
Expand All @@ -114,8 +122,11 @@ moment.fn.nextBusinessDay = function () {
};

moment.fn.prevBusinessDay = function () {
var locale = this.localeData();

var loop = 1;
var limit = 7;
var defaultPrevBusinessDayLimit = 7;
var limit = locale._prevBusinessDayLimit || defaultPrevBusinessDayLimit;
while (loop < limit) {
if (this.subtract(1, 'd').isBusinessDay()) {
break;
Expand Down Expand Up @@ -164,12 +175,25 @@ moment.fn.monthNaturalDays = function (fromToday) {
};

moment.fn.monthBusinessWeeks = function (fromToday) {
if (!this.isValid()) {
fromToday = fromToday || false;
var me = this.clone();
var startDate = fromToday ? me.clone() : me.clone().startOf('month');
return getBusinessWeeks(this, fromToday, null, startDate);
};

moment.fn.businessWeeksBetween = function (endDate) {
var me = this.clone();
var startDate = me.clone();
return getBusinessWeeks(this, false, endDate, startDate);
};

var getBusinessWeeks = function (self, fromToday, endDate, startDate) {
if (!self.isValid()) {
return [];
}
var me = this.clone();
var day = fromToday ? me.clone() : me.clone().startOf('month');
var end = me.clone().endOf('month');
var me = self.clone();
var day = startDate;
var end = endDate ? moment(endDate).clone() : me.clone().endOf('month');
var weeksArr = [];
var daysArr = [];
var done = false;
Expand All @@ -190,7 +214,7 @@ moment.fn.monthBusinessWeeks = function (fromToday) {
}
}
return weeksArr;
};
}

moment.fn.monthNaturalWeeks = function (fromToday) {
if (!this.isValid()) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 35 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
{
"name": "moment-business-days",
"version": "1.1.3",
"description": "MomentJS plugin to use business days",
"main": "index.js",
"types": "./index.d.ts",
"scripts": {
"test": "mocha tests/*",
"node-test": "node ./test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/kalmecak/moment-business-days.git"
},
"keywords": [
"moment",
"momentjs",
"business",
"days",
"plugin"
],
"peerDependencies": {
"moment": "2.x.x"
},
"engine": "node >= 0.10.26",
"author": "Christian Rodriguez Cisneros <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/kalmecak/moment-business-days/issues"
},
"homepage": "https://github.com/kalmecak/moment-business-days",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^5.2.0",
"moment": "2.x.x"
}
}
"name": "moment-business-days",
"version": "1.3.0",
"description": "MomentJS plugin to use business days",
"main": "index.js",
"types": "./index.d.ts",
"scripts": {
"test": "mocha tests/*"
},
"repository": {
"type": "git",
"url": "https://github.com/kalmecak/moment-business-days.git"
},
"keywords": [
"moment",
"momentjs",
"business",
"days",
"plugin"
],
"peerDependencies": {
"moment": "2.x.x"
},
"engine": "node >= 0.10.26",
"author": "Christian Rodriguez Cisneros <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/kalmecak/moment-business-days/issues"
},
"homepage": "https://github.com/kalmecak/moment-business-days",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^5.2.0",
"moment": "2.x.x"
}
}
61 changes: 60 additions & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ var expect = require('chai').expect;
var holidayFormat = 'MM-DD-YYYY';

var resetLocale = function (done) {
moment.updateLocale('us', {});
moment.updateLocale('us', {
holidays: [],
holidayFormat: '',
workingWeekdays: [1,2,3,4,5],
});
done();
};

Expand All @@ -21,6 +25,25 @@ describe('Moment Business Days', function () {
done();
});
});
describe('On April 10th, 2019', function () {
beforeEach(function (done) {
moment.updateLocale('us', {
holidays: ['04-05-2019', '04-06-2019', '04-07-2019', '04-08-2019', '04-09-2019'],
holidayFormat: 'MM-DD-YYYY',
workingWeekdays: [1],
prevBusinessDayLimit: 31,
});
done();
});

afterEach(resetLocale);

it('should be 1st when considering holidays and custom working days', function (done) {
var first = moment('04-10-2019', 'MM-DD-YYYY').prevBusinessDay();
expect(first.format('D')).to.eql('1');
done();
});
});
});
describe('.isBusinessDay', function () {
describe('When today is a regular weekday', function () {
Expand Down Expand Up @@ -150,6 +173,13 @@ describe('Moment Business Days', function () {
expect(newBusinessDay.isValid()).to.be.false;
});
});
describe('On Thursday, January 3rd 2019', function () {
it('adds one business day, then converts to string with toISOString()', function (done) {
var newBusinessDay = moment('2019-01-03T12:00:00.000Z').businessAdd(1, 'days');
expect(newBusinessDay.toISOString()).to.eql('2019-01-04T12:00:00.000Z');
done();
});
});
describe('On Tuesday, November 3rd 2015', function () {
it('adds business days only, excluding weekends, even over 2 weeks', function (done) {
var newBusinessDay = moment('11-03-2015', 'MM-DD-YYYY').businessAdd(5);
Expand Down Expand Up @@ -228,6 +258,19 @@ describe('Moment Business Days', function () {
);
expect(diff).to.eql(5);
});
it('Should be negative if start is after end and relative is true', function () {
var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(
moment('05-15-2017', 'MM-DD-YYYY'),
true
);
expect(diff).to.eql(-5);
});
it('Should be positive if start is after end and relative is false', function () {
var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(
moment('05-15-2017', 'MM-DD-YYYY')
);
expect(diff).to.eql(5);
});
it('Should calculate nr of business days with custom workingdays', function () {
moment.updateLocale('us', {
workingWeekdays: [1, 2, 3, 4, 5, 6]
Expand Down Expand Up @@ -259,6 +302,18 @@ describe('Moment Business Days', function () {
expect(diff).to.eql(0);
});
});
describe('Business Weeks', function () {
afterEach(resetLocale);
it('Should return array of business weeks on .monthBusinessWeeks', function () {
var monthBusinessWeeks = moment('2019-02-02').monthBusinessWeeks();
expect(monthBusinessWeeks).to.be.an('array').with.length(5);
});
it('Should return array of business weeks on .businessWeeksBetween', function () {
var businessWeeksBetween = moment('2019-02-02').businessWeeksBetween(moment('2019-04-02'));
expect(businessWeeksBetween).to.be.an('array').with.length(9);
});

});
describe('Aggregate functions return empty array on invalid object', function () {
afterEach(resetLocale);
it('Should return empty array on .monthBusinessDays', function () {
Expand All @@ -277,5 +332,9 @@ describe('Moment Business Days', function () {
var monthNaturalWeeks = moment(null).monthNaturalWeeks();
expect(monthNaturalWeeks).to.be.an('array').that.is.empty;
});
it('Should return empty array on .businessWeeksBetween', function () {
var businessWeeksBetween = moment(null).businessWeeksBetween();
expect(businessWeeksBetween).to.be.an('array').that.is.empty;
});
});
});

0 comments on commit 2f4292e

Please sign in to comment.