diff --git a/README.md b/README.md index 9e5e751..ceca978 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 } ``` diff --git a/src/index.js b/src/index.js index b8e30fb..2271582 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); diff --git a/test/chronoparse.text.js b/test/chronoparse.text.js index 0809906..899198a 100644 --- a/test/chronoparse.text.js +++ b/test/chronoparse.text.js @@ -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()); + }); }); \ No newline at end of file