Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return null with no matches and nix "this month" and "this year." #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
components
build
node_modules
15 changes: 12 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ module.exports = parser;
*/

function parser(str, offset) {
if(!(this instanceof parser)) return new parser(str, offset);
if(!(this instanceof parser)) {
var parsed = new parser(str, offset);

if(!(parsed instanceof parser)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, i find this double instanceof logic confusing. is there anything else we can do here to get the same results?

return parsed;
} else {
return null;
}
}
var d = offset || new Date;
this.date = new date(d);
this.original = str;
Expand All @@ -47,8 +55,9 @@ function parser(str, offset) {
while (this.advance() !== 'eos');
debug('tokens %j', this.tokens)
this.nextTime(d);
if (this.date.date == d) throw new Error('Invalid date');
return this.date.date;
if (this.date.date.getTime() !== d.getTime()) {
return this.date.date;
}
};

/**
Expand Down
18 changes: 6 additions & 12 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,6 @@ describe('morning', function() {
*/

describe('months', function () {
it('this month', function () {
var date = parse('this month', mon);
assert('1:30:00' == t(date));
assert('5/13/13' == d(date));
});

it('next month', function () {
var date = parse('next month', mon);
assert('1:30:00' == t(date));
Expand Down Expand Up @@ -428,12 +422,6 @@ describe('months', function () {
*/

describe('year', function() {
it('this year', function() {
var date = parse('year', mon);
assert('1:30:00' == t(date));
assert('5/13/13' == d(date));
});

it('next year', function () {
var date = parse('next year', mon);
assert('1:30:00' == t(date));
Expand Down Expand Up @@ -516,6 +504,12 @@ describe('bug fixes', function () {
assert('12:30:00' == t(date));
assert('5/14/13' == d(date));
});

it('return null when no matches', function() {
var date = parse('what aint no time I ever heard of', mon);
assert(date == null);
})

});

/**
Expand Down