moment-business-time
Advanced tools
Comparing version 0.8.0 to 1.0.0
@@ -17,3 +17,3 @@ var moment = require('moment'), | ||
} | ||
return hours[d.day()].map(function (time) { | ||
return toWorkingTimeSegments(hours[d.day()].map(function (time) { | ||
time = time.split(':'); | ||
@@ -26,3 +26,3 @@ var _d = d.clone(); | ||
return _d; | ||
}); | ||
})); | ||
} | ||
@@ -47,4 +47,14 @@ | ||
} | ||
var i = 0; | ||
while (n > 0) { | ||
var jump = openingTimes(d)[1].diff(d, unit); | ||
var segment = openingTimes(d)[i]; | ||
if (!segment || d.isBefore(segment[0])) { | ||
i = 0; | ||
continue; | ||
} | ||
if (d.isAfter(segment[1])) { | ||
i++; | ||
continue; | ||
} | ||
var jump = segment[1].diff(d, unit); | ||
if (jump > n) { | ||
@@ -58,7 +68,7 @@ jump = n; | ||
n -= jump; | ||
if (then.isWorkingTime()) { | ||
if (then.isSameOrBefore(segment[1])) { | ||
d = d.add(jump, unit); | ||
} else { | ||
var next = then.nextWorkingTime(); | ||
var diff = then.diff(openingTimes(d)[1], unit, true); | ||
var diff = then.diff(segment[1], unit, true); | ||
d = next.add(diff,unit); | ||
@@ -76,4 +86,14 @@ } | ||
} | ||
var i = 0; | ||
while (n > 0) { | ||
var jump = -1 * openingTimes(d)[0].diff(d, unit); | ||
var segment = openingTimes(d)[i]; | ||
if (!segment || d.isBefore(segment[0])) { | ||
i = 0; | ||
continue; | ||
} | ||
if (d.isAfter(segment[1])) { | ||
i++; | ||
continue; | ||
} | ||
var jump = -1 * segment[0].diff(d, unit); | ||
if (jump > n) { | ||
@@ -91,3 +111,3 @@ jump = n; | ||
var next = then.lastWorkingTime(); | ||
var diff = then.diff(openingTimes(d)[0], unit, true); | ||
var diff = then.diff(segment[0], unit, true); | ||
d = next.add(diff,unit); | ||
@@ -119,2 +139,5 @@ } | ||
d = incrementDays('add')(num, d); | ||
// prevent calculation error when break between two working time segments is less than 1 hour | ||
} else if (unit === 'hour') { | ||
d = addUnit('minute')(num * 60, d); | ||
} else if (unit) { | ||
@@ -134,2 +157,5 @@ d = addUnit(unit)(num, d); | ||
d = incrementDays('subtract')(num, d); | ||
// prevent calculation error when break between two working time segments is less than 1 hour | ||
} else if (unit === 'hour') { | ||
d = subtractUnit('minute')(num * 60, d); | ||
} else if (unit) { | ||
@@ -165,2 +191,9 @@ d = subtractUnit(unit)(num, d); | ||
function toWorkingTimeSegments(openingTimes) { | ||
return openingTimes.reduce(function (rows, key, index) { | ||
return (index % 2 === 0 ? rows.push([key]) | ||
: rows[rows.length-1].push(key)) && rows; | ||
}, []); | ||
} | ||
moment.fn.addWorkingTime = addOrSubtractMethod(add); | ||
@@ -176,7 +209,10 @@ moment.fn.subtractWorkingTime = addOrSubtractMethod(subtract); | ||
moment.fn.isWorkingTime = function isWorkingTime() { | ||
var openinghours = openingTimes(this); | ||
if (!openinghours) { | ||
var segments = openingTimes(this); | ||
if (!segments) { | ||
return false; | ||
} else { | ||
return this.isSameOrAfter(openinghours[0]) && this.isSameOrBefore(openinghours[1]); | ||
var self = this; | ||
return segments.some(function(openinghours) { | ||
return self.isSameOrAfter(openinghours[0]) && self.isSameOrBefore(openinghours[1]); | ||
}); | ||
} | ||
@@ -188,3 +224,3 @@ }; | ||
today = this.format('YYYY-MM-DD'); | ||
getLocaleData('holidays').forEach(function (holiday) { | ||
(getLocaleData('holidays') || []).forEach(function (holiday) { | ||
if (minimatch(today, holiday)) { | ||
@@ -217,12 +253,20 @@ isHoliday = true; | ||
if (this.isWorkingDay()) { | ||
var openinghours = openingTimes(this); | ||
if (this.isBefore(openinghours[0])) { | ||
return openinghours[0]; | ||
} else if (this.isAfter(openinghours[1])) { | ||
return openingTimes(this.nextWorkingDay())[0]; | ||
} else { | ||
return this.clone(); | ||
var segments = openingTimes(this); | ||
var openinghours, lastSegment; | ||
for(i = 0; i < segments.length; i++) { | ||
openinghours = segments[i]; | ||
lastSegment = i === segments.length -1; | ||
if (this.isBefore(openinghours[0])) { | ||
return openinghours[0]; | ||
} else if (this.isAfter(openinghours[1])) { | ||
if (!lastSegment) { | ||
continue; | ||
} | ||
return openingTimes(this.nextWorkingDay())[0][0]; | ||
} else { | ||
return this.clone(); | ||
} | ||
} | ||
} else { | ||
return openingTimes(this.nextWorkingDay())[0]; | ||
return openingTimes(this.nextWorkingDay())[0][0]; | ||
} | ||
@@ -233,12 +277,20 @@ }; | ||
if (this.isWorkingDay()) { | ||
var openinghours = openingTimes(this); | ||
if (this.isAfter(openinghours[1])) { | ||
return openinghours[1]; | ||
} else if (this.isBefore(openinghours[0])) { | ||
return openingTimes(this.lastWorkingDay())[1]; | ||
} else { | ||
return this.clone(); | ||
var segments = openingTimes(this); | ||
var openinghours, firstSegment; | ||
for(i = segments.length - 1; i >= 0; i--) { | ||
openinghours = segments[i]; | ||
firstSegment = i === 0; | ||
if (this.isAfter(openinghours[1])) { | ||
return openinghours[1]; | ||
} else if (this.isBefore(openinghours[0])) { | ||
if (!firstSegment) { | ||
continue; | ||
} | ||
return openingTimes(this.lastWorkingDay()).slice(-1)[0][1]; | ||
} else { | ||
return this.clone(); | ||
} | ||
} | ||
} else { | ||
return openingTimes(this.lastWorkingDay())[1]; | ||
return openingTimes(this.lastWorkingDay()).slice(-1)[0][1]; | ||
} | ||
@@ -281,24 +333,32 @@ }; | ||
// iterate to the same day | ||
while(from.format('L') !== to.format('L')) { | ||
if (unit === 'day') { | ||
if (unit === 'day') { | ||
// iterate to the same day | ||
while(!from.isSame(to, 'day')) { | ||
diff--; | ||
from = from.addWorkingTime(1, 'day'); | ||
} else { | ||
diff += from.diff(openingTimes(from)[1], unit, true); | ||
from = openingTimes(from.nextWorkingDay())[0]; | ||
} | ||
} | ||
if (detail) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn('WARNING: passing `true` as a third argument to `workingDiff` may lead to ambiguous results'); | ||
console.warn('See https://github.com/lennym/moment-business-time/issues/12#issuecomment-199710566'); | ||
} | ||
var hours = from.diff(to, 'hour', true), | ||
denominator = comparator.isWorkingDay() ? comparator : comparator.nextWorkingDay(), | ||
total = openingTimes(denominator).slice(-1)[0][1].diff(openingTimes(denominator)[0][0], 'hour', true); | ||
diff += hours/total; | ||
} | ||
if (unit !== 'day') { | ||
diff += from.diff(to, unit, true); | ||
} else if (detail) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn('WARNING: passing `true` as a third argument to `workingDiff` may lead to ambiguous results'); | ||
console.warn('See https://github.com/lennym/moment-business-time/issues/12#issuecomment-199710566'); | ||
} else { | ||
var segments = openingTimes(from); | ||
while (from.isBefore(to)) { | ||
for (var i = 0; i < segments.length; i++) { | ||
var segment = segments[i]; | ||
if (from.isAfter(segment[1])) { | ||
continue; | ||
} | ||
diff += moment.max(from, segment[0]).diff(moment.min(segment[1], to), unit, true); | ||
} | ||
segments = openingTimes(from.nextWorkingDay()); | ||
from = segments[0][0]; | ||
} | ||
var hours = from.diff(to, 'hour', true), | ||
denominator = comparator.isWorkingDay() ? comparator : comparator.nextWorkingDay(), | ||
total = openingTimes(denominator)[1].diff(openingTimes(denominator)[0], 'hour', true); | ||
diff += hours/total; | ||
} | ||
@@ -305,0 +365,0 @@ |
{ | ||
"name": "moment-business-time", | ||
"version": "0.8.0", | ||
"version": "1.0.0", | ||
"description": "Query and manipulate moment objects within the context of business/working hours", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -156,2 +156,3 @@ # moment-business-time | ||
// set opening time to 09:30 and close early on Wednesdays | ||
// close for a one hour lunch break on Thursdays | ||
moment.locale('en', { | ||
@@ -163,3 +164,3 @@ workinghours: { | ||
3: ['09:30:00', '13:00:00'], | ||
4: ['09:30:00', '17:00:00'], | ||
4: ['09:30:00', '12:00:00', '13:00:00', '17:00:00'], | ||
5: ['09:30:00', '17:00:00'], | ||
@@ -166,0 +167,0 @@ 6: null |
@@ -61,2 +61,20 @@ var moment = require('../lib/business-hours'); | ||
it('considers multiple opening times per day', function(){ | ||
moment.locale('en', { | ||
workinghours: { | ||
0: null, | ||
1: ['09:00:00', '12:00:00', '13:00:00', '17:00:00'], | ||
2: ['09:00:00', '17:00:00'], | ||
3: ['09:00:00', '17:00:00'], | ||
4: ['09:00:00', '17:00:00'], | ||
5: ['09:00:00', '17:00:00'], | ||
6: null | ||
} | ||
}); | ||
moment('2017-06-26T10:00:00.000').isWorkingTime().should.be.true; | ||
moment('2017-06-26T12:30:00.000').isWorkingTime().should.be.false; | ||
moment('2017-06-26T16:00:00.000').isWorkingTime().should.be.true; | ||
}); | ||
}); | ||
@@ -146,2 +164,20 @@ | ||
it('handles working times with breaks', function() { | ||
moment.locale('en', { | ||
workinghours: { | ||
0: null, | ||
1: ['09:00:00', '17:00:00'], | ||
2: ['09:00:00', '17:00:00'], | ||
3: ['09:00:00', '17:00:00'], | ||
4: ['09:00:00', '12:00:00', '13:00:00', '18:00:00'], | ||
5: ['09:00:00', '12:00:00', '12:30:00', '17:00:00'], | ||
6: null | ||
} | ||
}); | ||
moment('2020-01-02T10:00:00').addWorkingTime(4, 'hour').format(full).should.equal('2020-01-02 15:00:00.000'); | ||
moment('2020-01-03T11:00:00').addWorkingTime(2, 'hour').format(full).should.equal('2020-01-03 13:30:00.000'); | ||
moment('2020-01-02T10:00:00').addWorkingTime(16, 'hour').format(full).should.equal('2020-01-06 10:30:00.000'); | ||
moment('2020-01-02T12:30:00').addWorkingTime(2, 'hour').format(full).should.equal('2020-01-02 15:00:00.000'); | ||
moment('2020-01-03T11:30:00').addWorkingTime(1, 'hour').format(full).should.equal('2020-01-03 13:00:00.000'); | ||
}); | ||
}); | ||
@@ -188,3 +224,3 @@ | ||
it('doesn\'t break on leap years', function () { | ||
moment('2020-01-06T13:00:00.000z').addWorkingTime(1, 'seconds').format(full).should.equal('2020-01-06 13:00:01.000'); | ||
moment.utc('2020-01-06T13:00:00.000Z').addWorkingTime(1, 'seconds').format(full).should.equal('2020-01-06 13:00:01.000'); | ||
}); | ||
@@ -278,2 +314,20 @@ | ||
it('handles working times with breaks', function() { | ||
moment.locale('en', { | ||
workinghours: { | ||
0: null, | ||
1: ['09:00:00', '17:00:00'], | ||
2: ['09:00:00', '17:00:00'], | ||
3: ['09:00:00', '17:00:00'], | ||
4: ['09:00:00', '12:00:00', '13:00:00', '18:00:00'], | ||
5: ['09:00:00', '12:00:00', '12:30:00', '17:00:00'], | ||
6: null | ||
} | ||
}); | ||
moment('2020-01-02T15:00:00').subtractWorkingTime(4, 'hour').format(full).should.equal('2020-01-02 10:00:00.000'); | ||
moment('2020-01-03T13:30:00').subtractWorkingTime(2, 'hour').format(full).should.equal('2020-01-03 11:00:00.000'); | ||
moment('2020-01-06T10:30:00').subtractWorkingTime(16, 'hour').format(full).should.equal('2020-01-02 10:00:00.000'); | ||
moment('2020-01-02T15:00:00').subtractWorkingTime(2, 'hour').format(full).should.equal('2020-01-02 13:00:00.000'); | ||
moment('2020-01-03T13:00:00').subtractWorkingTime(1, 'hour').format(full).should.equal('2020-01-03 11:30:00.000'); | ||
}); | ||
}); | ||
@@ -431,2 +485,23 @@ | ||
it('calculates the diff of working hours while respecting breaks', function() { | ||
moment.locale('en', { | ||
workinghours: { | ||
0: null, | ||
1: ['09:00:00', '17:00:00'], | ||
2: ['09:00:00', '17:00:00'], | ||
3: ['09:00:00', '17:00:00'], | ||
4: ['09:00:00', '12:00:00', '13:00:00', '18:00:00'], | ||
5: ['09:00:00', '12:00:00', '12:30:00', '17:00:00'], | ||
6: null | ||
} | ||
}); | ||
moment('2020-01-02T15:00:00').workingDiff('2020-01-02T10:00:00', 'hour').should.equal(4); | ||
moment('2020-01-03T13:30:00').workingDiff('2020-01-03T11:00:00', 'hour').should.equal(2); | ||
moment('2020-01-06T10:00:00').workingDiff('2020-01-02T10:00:00', 'hour').should.equal(15); | ||
moment('2020-01-06T10:00:00').workingDiff('2020-01-02T10:00:00', 'hour', true).should.equal(15.5); | ||
moment('2020-01-02T15:00:00').workingDiff('2020-01-02T12:30:00', 'hour').should.equal(2); | ||
moment('2020-01-03T13:00:00').workingDiff('2020-01-03T11:30:00', 'hour').should.equal(1); | ||
}) | ||
it('calculates the difference between dates in working days', function () { | ||
@@ -508,3 +583,3 @@ var from = moment('2015-02-27T10:00:00'), | ||
moment('2015-02-26').addWorkingTime(3, 'days').format(date).should.equal('2015-03-04'); | ||
moment('2015-02-26T12:00:00Z').addWorkingTime(8, 'hours').format(full).should.equal('2015-03-02 12:00:00.000'); | ||
moment.utc('2015-02-26T12:00:00Z').addWorkingTime(8, 'hours').format(full).should.equal('2015-03-02 12:00:00.000'); | ||
}); | ||
@@ -511,0 +586,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
44406
796
0
206