Skip to content

Commit

Permalink
v5.2.0: add Duration#divide() method that takes another Duration and …
Browse files Browse the repository at this point in the history
…outputs a unitless number.
  • Loading branch information
Rogier Schouten committed Nov 6, 2016
1 parent 84530ba commit 6e3740d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
23 changes: 13 additions & 10 deletions dist/timezonecomplete.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/timezonecomplete.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ duration2 = duration.add(tc.hours(5)); // 18002 seconds
duration2 = duration.sub(tc.milliseconds(500)); // 1.5 seconds
duration2 = duration.multiply(3); // 6 seconds
duration2 = duration.divide(3); // 0.67 seconds
tc.years(1).divide(tc.months(2)); // 1 year divided by 2 months = 6

// note that e.g. adding hours to months gives an approximate value
// as not all months are equally long
Expand Down
4 changes: 4 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# Changelog

## 5.2.0 (2016-11-06)

* Add Duration#divide() method that takes another Duration and outputs a unitless number.

## 5.1.2 (2016-10-28)

* Inline the sourcemaps as requested in issue #28
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "timezonecomplete",
"version": "5.1.2",
"version": "5.2.0",
"description": "DateTime, TimeZone, Duration and Period library aimed at providing a consistent and complete date-time interface, away from the original JavaScript Date class.",
"keywords": [
"Date",
Expand Down
28 changes: 21 additions & 7 deletions src/lib/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,24 +477,38 @@ export class Duration {
}

/**
* Approximate if the durations have units that cannot be converted
* Multiply with a fixed number.
* Approximate if the durations have units that cannot be converted
* @return a new Duration of (this * value)
*/
public multiply(value: number): Duration {
return new Duration(this._amount * value, this._unit);
}

/**
* Approximate if the durations have units that cannot be converted
* Divide by a fixed number.
* Divide by a unitless number. The result is a Duration, e.g. 1 year / 2 = 0.5 year
* The result is approximate if this duration as a unit that cannot be converted to a number (e.g. 1 month has variable length)
* @return a new Duration of (this / value)
*/
public divide(value: number): Duration {
if (value === 0) {
throw new Error("Duration.divide(): Divide by zero");
public divide(value: number): Duration;
/**
* Divide this Duration by a Duration. The result is a unitless number e.g. 1 year / 1 month = 12
* The result is approximate if this duration as a unit that cannot be converted to a number (e.g. 1 month has variable length)
* @return a new Duration of (this / value)
*/
public divide(value: Duration): number;
public divide(value: number | Duration): Duration | number {
if (typeof value === "number") {
if (value === 0) {
throw new Error("Duration.divide(): Divide by zero");
}
return new Duration(this._amount / value, this._unit);
} else {
if (value._amount === 0) {
throw new Error("Duration.divide(): Divide by zero duration");
}
return this.milliseconds() / value.milliseconds();
}
return new Duration(this._amount / value, this._unit);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/test/test-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe("Duration()", (): void => {
});
});

describe("divide()", (): void => {
describe("divide() by number", (): void => {
it("should divide by positive number", (): void => {
expect(Duration.milliseconds(6).divide(3).milliseconds()).to.equal(2);
});
Expand All @@ -351,6 +351,20 @@ describe("Duration()", (): void => {
});
});

describe("divide() by Duration", (): void => {
it("should divide by positive Duration", (): void => {
expect(Duration.years(1).divide(Duration.months(2))).to.equal(6);
});
it("should throw on divide by 0", (): void => {
assert.throws((): void => {
Duration.milliseconds(6).divide(Duration.months(0));
});
});
it("should divide by negative number", (): void => {
expect(Duration.years(1).divide(Duration.months(-2))).to.equal(-6);
});
});

describe("add()", (): void => {
it("should add positive number", (): void => {
expect(Duration.milliseconds(2).add(Duration.milliseconds(3)).milliseconds()).to.equal(5);
Expand Down

0 comments on commit 6e3740d

Please sign in to comment.