chronoshift
Advanced tools
Comparing version 0.7.0 to 0.7.1
@@ -267,3 +267,3 @@ 'use strict'; | ||
if (!spans.week) | ||
throw new Error("Duration can not be empty"); | ||
throw new Error("Duration can not have empty weeks"); | ||
} | ||
@@ -361,10 +361,22 @@ else if (matches = periodRegExp.exec(durationStr)) { | ||
Duration.fromCanonicalLength = function (length) { | ||
if (length <= 0) | ||
throw new Error('length must be positive'); | ||
var spans = {}; | ||
for (var i = 0; i < spansWithWeek.length; i++) { | ||
var span = spansWithWeek[i]; | ||
var spansUsed = 0; | ||
var lengthLeft = length; | ||
for (var i = 0; i < spansWithoutWeek.length; i++) { | ||
var span = spansWithoutWeek[i]; | ||
var spanLength = shifters[span].canonicalLength; | ||
var count = Math.floor(length / spanLength); | ||
length -= spanLength * count; | ||
spans[span] = count; | ||
var count = Math.floor(lengthLeft / spanLength); | ||
if (count) { | ||
lengthLeft -= spanLength * count; | ||
spans[span] = count; | ||
spansUsed++; | ||
} | ||
} | ||
if (length % shifters['week'].canonicalLength === 0 && | ||
(spansUsed > 1 || | ||
spans['day'])) { | ||
spans = { week: length / shifters['week'].canonicalLength }; | ||
} | ||
return new Duration(spans); | ||
@@ -371,0 +383,0 @@ }; |
@@ -17,3 +17,3 @@ import { second, shifters } from '../floor-shift-ceil/floor-shift-ceil'; | ||
if (!spans.week) | ||
throw new Error("Duration can not be empty"); | ||
throw new Error("Duration can not have empty weeks"); | ||
} | ||
@@ -112,10 +112,22 @@ else if (matches = periodRegExp.exec(durationStr)) { | ||
Duration.fromCanonicalLength = function (length) { | ||
if (length <= 0) | ||
throw new Error('length must be positive'); | ||
var spans = {}; | ||
for (var i = 0; i < spansWithWeek.length; i++) { | ||
var span = spansWithWeek[i]; | ||
var spansUsed = 0; | ||
var lengthLeft = length; | ||
for (var i = 0; i < spansWithoutWeek.length; i++) { | ||
var span = spansWithoutWeek[i]; | ||
var spanLength = shifters[span].canonicalLength; | ||
var count = Math.floor(length / spanLength); | ||
length -= spanLength * count; | ||
spans[span] = count; | ||
var count = Math.floor(lengthLeft / spanLength); | ||
if (count) { | ||
lengthLeft -= spanLength * count; | ||
spans[span] = count; | ||
spansUsed++; | ||
} | ||
} | ||
if (length % shifters['week'].canonicalLength === 0 && | ||
(spansUsed > 1 || | ||
spans['day'])) { | ||
spans = { week: length / shifters['week'].canonicalLength }; | ||
} | ||
return new Duration(spans); | ||
@@ -122,0 +134,0 @@ }; |
@@ -23,3 +23,3 @@ import { expect } from 'chai'; | ||
expect(function () { return Duration.fromJS("P0YT0H"); }).to.throw(Error, "Duration can not be empty"); | ||
expect(function () { return Duration.fromJS("P0W").shift(new Date(), TZ_LA); }).to.throw(Error, "Duration can not be empty"); | ||
expect(function () { return Duration.fromJS("P0W").shift(new Date(), TZ_LA); }).to.throw(Error, "Duration can not have empty weeks"); | ||
expect(function () { return Duration.fromJS("P0Y0MT0H0M0S").shift(new Date(), TZ_LA); }).to.throw(Error, "Duration can not be empty"); | ||
@@ -48,5 +48,14 @@ }); | ||
describe("fromCanonicalLength", function () { | ||
it("works", function () { | ||
it("handles zero", function () { | ||
expect(function () { | ||
Duration.fromCanonicalLength(0); | ||
}).to.throw('length must be positive'); | ||
}); | ||
it("works 1", function () { | ||
expect(Duration.fromCanonicalLength(86400000).toJS()).to.eql("P1D"); | ||
}); | ||
it("works 2", function () { | ||
var len = new Date('2018-03-01T00:00:00Z').valueOf() - new Date('2016-02-22T00:00:00Z').valueOf(); | ||
expect(Duration.fromCanonicalLength(len).toJS()).to.eql("P2Y8D"); | ||
}); | ||
}); | ||
@@ -242,3 +251,3 @@ describe("construct from span", function () { | ||
var d = Duration.fromJS("P1W"); | ||
expect(d.multiply(5).toJS()).to.eql("P1M5D"); | ||
expect(d.multiply(5).toJS()).to.eql("P5W"); | ||
}); | ||
@@ -245,0 +254,0 @@ it("throws an error if result is going to be negative", function () { |
{ | ||
"name": "chronoshift", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "A tiny library for shifting time with timezones", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
113409
2434