Skip to content

Commit

Permalink
Added date-based tests and startDate to options object
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanrenwick committed Nov 18, 2019
1 parent f1a26f1 commit b508030
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ chronoparse parses the given string, and returns an object using the following s
```js
{
totalSeconds, // an integer representing the total length of the timespan in seconds
startDate, // the current datetime as of parsing completion
startDate, // the given startDate in the options object
endDate, // a datetime totalSeconds from startDate
timezone, // the current local timezone, specified as "UTC+/-X" where X is the offset in hours
parts // an array of each timespan string parsed
Expand Down Expand Up @@ -50,6 +50,7 @@ The options object may contain any or all of the following options:
```js
{
delimiter, // a string to use to separate multiple timespans, defaults to whitespace
startDate, // a Date object specifying when to calculate datetimes from, defaults to now
max // a non-negative integer specifying the maximum number of timespans to parse in the string, defaults to no limit
}
```
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ module.exports = function parse(text, options = {}) {
outputData.parts += res[0];
}

let date = new Date();
let date = options.startDate || new Date();
date.setSeconds(date.getSeconds() + outputData.totalSeconds);
outputData.startDate = new Date();
outputData.startDate = options.startDate || new Date();
outputData.endDate = date;
outputData.timezone = getTimezone();

Expand Down
22 changes: 22 additions & 0 deletions test/chronoparse.text.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,26 @@ describe("parse", function() {
tzMock.register("UTC");
expect(parse("1 second").timezone).to.equal("UTC+0");
});

it ("should return the date of completion as the startDate", function() {
let now = new Date();
let result = parse("29 seconds").startDate;
// Will fail if parsing takes more than 60 seconds
expect(result - now).to.be.lessThan(60000);
});

it ("should return an endDate that matches the given timespan", function() {
let result = parse("29 seconds");
expect(result.endDate - result.startDate).to.equal(29000);
});

it ("should support specifying a different startDate", function() {
let date = new Date();
date.setSeconds(date.getSeconds() + 120);
let endDate = new Date();
endDate.setSeconds(endDate.getSeconds() + 149);
let result = parse("29 seconds", {startDate: date});
expect(result.startDate.toString()).to.equal(date.toString());
expect(result.endDate.toString()).to.equal(endDate.toString());
});
});

0 comments on commit b508030

Please sign in to comment.