New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

moment-business-days

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moment-business-days - npm Package Compare versions

Comparing version 0.0.10 to 1.0.0

100

index.js
'use strict';
var moment = require('moment');
if (typeof window === 'undefined') {
var moment = require('moment');
}
moment.fn.isHoliday = function () {

@@ -11,2 +14,10 @@ var locale = this.localeData();

if (locale.holiday) {
if (locale.holiday(this)) {
return true;
} else {
return false;
}
}
return false;

@@ -16,5 +27,10 @@ };

moment.fn.isBusinessDay = function() {
if (this.day() === 0 || this.day() === 6) return false;
var locale = this.localeData();
var defaultWorkingWeekdays = [1,2,3,4,5];
var workingWeekdays = locale._workingWeekdays || defaultWorkingWeekdays;
if (this.isHoliday()) return false;
return true;
if (workingWeekdays.indexOf(this.day())>=0) return true;
return false;
};

@@ -27,6 +43,6 @@

monthBusinessDays.map(function (day, index) {
if (day.format('M/DD/YY') === businessDay.format('M/DD/YY'))
if (day.format('M/DD/YY') === businessDay.format('M/DD/YY')) {
businessDaysIntoMonth = index + 1;
});
}
})
return businessDaysIntoMonth;

@@ -36,44 +52,38 @@ };

moment.fn.businessDiff = function(param) {
param = moment(param);
var signal = param.unix() < this.unix()?1:-1;
var start = moment.min(param, this).clone();
var end = moment.max(param, this).clone();
var start_offset = start.day() - 7;
var end_offset = end.day();
var end = this.clone();
var start = moment(param);
var daysBetween = 0;
var end_sunday = end.clone().subtract('d', end_offset);
var start_sunday = start.clone().subtract('d', start_offset);
var weeks = end_sunday.diff(start_sunday, 'days') / 7;
if(start === end){
return daysBetween;
}
start_offset = Math.abs(start_offset);
if (start_offset == 7) {
start_offset = 5;
} else if (start_offset == 1) {
start_offset = 0;
} else {
start_offset -= 2;
};
while (start < end){
if(this.isBusinessDay(start)){
daysBetween++;
}
start = start.businessAdd(1)
}
if (end_offset == 6) {
end_offset--;
};
return signal * (weeks * 5 + start_offset + end_offset);
return daysBetween;
};
moment.fn.businessAdd = function(days) {
var signal = days < 0 ? -1 : 1;
var daysRemaining = Math.abs(days);
var d = this.clone();
while (daysRemaining) {
d.add(signal, 'd');
if (d.isBusinessDay()) {
daysRemaining--;
};
};
return d;
moment.fn.businessAdd = function(number, period = 'days') {
var day = this.clone();
var signal = number < 0 ? -1 : 1;
var remaining = Math.abs(number);
while (remaining) {
day.add(signal, period);
if (day.isBusinessDay()) {
remaining--;
}
}
return day;
};
moment.fn.businessSubtract = function(days) {
return this.businessAdd(-days);
moment.fn.businessSubtract = function(number, period = 'days') {
return this.businessAdd(-number, period);
};

@@ -106,6 +116,6 @@

moment.fn.monthBusinessDays = function() {
moment.fn.monthBusinessDays = function(partialEndDate) {
var me = this.clone();
var day = me.clone().startOf('month');
var end = me.clone().endOf('month');
var end = partialEndDate ? partialEndDate : me.clone().endOf('month');
var daysArr = [];

@@ -189,2 +199,4 @@ var done = false;

module.exports = moment;
if (typeof module != 'undefined' && module.exports) {
module.exports = moment;
}
{
"name": "moment-business-days",
"version": "0.0.10",
"description": "MomentJS pluin to use business days ",
"version": "1.0.0",
"description": "MomentJS plugin to use business days ",
"main": "index.js",
"scripts": {
"test": "node ./test.js",
"mochatest": "mocha tests/*"
"test": "mocha tests/*",
"node-test": "node ./test.js"
},

@@ -21,4 +21,4 @@ "repository": {

],
"dependencies": {
"moment": "2.10.x"
"peerDependencies": {
"moment": "2.x.x"
},

@@ -33,5 +33,5 @@ "engine": "node >= 0.10.26",

"devDependencies": {
"chai": "^3.3.0",
"mocha": "^2.3.3"
"chai": "^3.5.0",
"mocha": "^3.4.1"
}
}

@@ -34,2 +34,16 @@ # moment-business-days

````
#### Use localizaton to customize workingdays:
````javascript
var moment = require('moment-business-days');
moment.locale('us', {
workingWeekdays: [1,2,3,4,5,6]
});
// Specifies days form 1 to 6 as a workingday, thus monday to saturday
// When ommiting this configuration parameter, workingdays as used based on locale default
````
#### Run Tests:

@@ -36,0 +50,0 @@

'use strict';
var moment = require('../index');
var expect = require("chai").expect
var expect = require('chai').expect
var holidayFormat = 'MM-DD-YYYY';
var resetLocale = function (done) {
moment.locale('us', {});
done()
moment.updateLocale('us', {});
done();
};
describe('Moment Business Days', function () {
afterEach(resetLocale);
describe('.prevBusinessDay', function () {

@@ -36,3 +38,47 @@ describe('When today is Monday', function () {

});
});
describe('When today is a holiday determined by a function', function () {
var callCount = 0;
beforeEach(function (done) {
moment.locale('xmas', {
// Every Christmas is a holiday, no matter the year
holiday: function (someMoment) {
callCount++;
// Months indexed starting at 0, so December is 11.
return (someMoment.month() === 11 && someMoment.date() === 25);
}
});
done();
});
afterEach(resetLocale);
it('should be false', function (done) {
// In these years, Christmas was a weekday
expect(moment('2012-12-25').isBusinessDay()).to.be.false;
expect(moment('2013-12-25').isBusinessDay()).to.be.false;
expect(moment('2014-12-25').isBusinessDay()).to.be.false;
expect(moment('2015-12-25').isBusinessDay()).to.be.false;
expect(callCount).to.equal(4);
done();
});
});
describe('When today is custom working day', function(){
beforeEach(function (done) {
moment.updateLocale('us',{
workingWeekdays: [1,2,3,4,5,6]
})
done();
})
it('Should be true', function (done) {
var saturday = moment().endOf('week')
expect(saturday.isBusinessDay()).to.be.true;
done();
})
});
describe('When today is a holiday', function () {

@@ -43,3 +89,3 @@

beforeEach(function (done) {
moment.locale('us', {
moment.updateLocale('us', {
holidays: [july4th],

@@ -62,5 +108,9 @@ holidayFormat: holidayFormat

afterEach(resetLocale);
describe('On Wednesday, September 23rd 2015', function () {
it('should be 17 when there are no holidays', function (done) {
moment.updateLocale('us',{
workingWeekdays: null
});
var businessDaysIntoMonth = moment('09-23-2015', 'MM-DD-YYYY').businessDaysIntoMonth();

@@ -71,3 +121,3 @@ expect(businessDaysIntoMonth).to.eql(17);

it('should be 16 when considering labor day', function (done) {
moment.locale('us', {
moment.updateLocale('us', {
holidays: ['09-07-2015'],

@@ -86,3 +136,3 @@ holidayFormat: holidayFormat

describe('On Tuesday, Novemeber 3rd 2015', function () {
describe('On Tuesday, November 3rd 2015', function () {
it('adds business days only, excluding weekends, even over 2 weeks ', function (done) {

@@ -98,4 +148,9 @@ var newBusinessDay = moment('11-03-2015', 'MM-DD-YYYY').businessAdd(5);

});
it('adds business hours only, excluding weekends ', function (done) {
var newBusinessDay = moment('11-06-2015', 'MM-DD-YYYY').businessAdd(36, 'hours');
expect(newBusinessDay.format('D')).to.eql('9');
done();
});
it('adds business days only, excluding weekends and holidays, if present', function (done) {
moment.locale('us', {
moment.updateLocale('us', {
holidays: ['11-05-2015'],

@@ -109,3 +164,3 @@ holidayFormat: holidayFormat

it('adds business days only, excluding weekends and holidays, if present, even over 2 weeks', function (done) {
moment.locale('us', {
moment.updateLocale('us', {
holidays: ['11-05-2015', '11-12-2015'],

@@ -120,2 +175,22 @@ holidayFormat: holidayFormat

});
describe('Business Diff', function(){
afterEach(resetLocale)
it('Should calculate number of busines days between dates', function(){
var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-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]
});
var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','MM-DD-YYYY'))
expect(diff).to.eql(6)
})
it('Should be zero days if start and end is same', function(){
var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017', 'MM-DD-YYYY'));
expect(diff).to.eql(0)
});
})
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc