Skip to content

Commit b7f074f

Browse files
committed
feat($urlMatcherFactory): date type support
1 parent aeb6d9b commit b7f074f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/urlMatcherFactory.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,23 @@ function $UrlMatcherFactory() {
327327
pattern: /0|1/
328328
},
329329
string: {
330-
pattern: /.*/
330+
pattern: /[^\/]*/
331+
},
332+
date: {
333+
equals: function (a, b) {
334+
return a.toISOString() === b.toISOString();
335+
},
336+
decode: function (val) {
337+
return new Date(val);
338+
},
339+
encode: function (val) {
340+
return [
341+
val.getFullYear(),
342+
('0' + (val.getMonth() + 1)).slice(-2),
343+
('0' + val.getDate()).slice(-2)
344+
].join("-");
345+
},
346+
pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/
331347
}
332348
};
333349

test/urlMatcherFactorySpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,14 @@ describe("urlMatcherFactory", function () {
187187
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
188188
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
189189
});
190+
191+
it("should encode/decode dates", function () {
192+
var m = new UrlMatcher("/calendar/{date:date}"),
193+
result = m.exec("/calendar/2014-03-26");
194+
195+
expect(result.date instanceof Date).toBe(true);
196+
expect(result.date.toUTCString()).toEqual('Wed, 26 Mar 2014 00:00:00 GMT');
197+
expect(m.format({ date: new Date(2014, 2, 26) })).toBe("/calendar/2014-03-26");
198+
});
190199
});
191200
});

0 commit comments

Comments
 (0)