diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index b22cf6b41..4dc9db43f 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -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])/ } }; diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index d70fc86c4..fdec89661 100644 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -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"); + }); }); });