Skip to content

Commit

Permalink
feat($urlMatcherFactory): date type support
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Apr 11, 2014
1 parent aeb6d9b commit b7f074f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,23 @@ function $UrlMatcherFactory() {
pattern: /0|1/
},
string: {
pattern: /.*/
pattern: /[^\/]*/
},
date: {
equals: function (a, b) {
return a.toISOString() === b.toISOString();
},
decode: function (val) {
return new Date(val);
},
encode: function (val) {
return [
val.getFullYear(),
('0' + (val.getMonth() + 1)).slice(-2),
('0' + val.getDate()).slice(-2)
].join("-");
},
pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/
}
};

Expand Down
9 changes: 9 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,14 @@ describe("urlMatcherFactory", function () {
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
});

it("should encode/decode dates", function () {
var m = new UrlMatcher("/calendar/{date:date}"),
result = m.exec("/calendar/2014-03-26");

expect(result.date instanceof Date).toBe(true);
expect(result.date.toUTCString()).toEqual('Wed, 26 Mar 2014 00:00:00 GMT');
expect(m.format({ date: new Date(2014, 2, 26) })).toBe("/calendar/2014-03-26");
});
});
});

0 comments on commit b7f074f

Please sign in to comment.