-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateInterceptor.js
47 lines (42 loc) · 1.82 KB
/
dateInterceptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(function (app) {
app.factory('dateInterceptor', function () {
var regexIsoUtc = /(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})(.\d*)?\+(\d{2})\:(\d{2})/;
function matchDate(dateString) {
return dateString.match(regexIsoUtc);
};
function convertDateStringsToDates(object) {
// ensure that we're processing an object
if (typeof object !== "object") {
return object;
}
for (var key in object) {
if (!object.hasOwnProperty(key)) {
continue;
}
var value = object[key];
// check for string properties with a date format
if (typeof value === "string" && matchDate(value)) {
var date = new Date(value); // create the date from the date string
object[key] = date; // we're mutating the response directly
} else if (typeof value === "object") {
convertDateStringsToDates(value); // recurse into object
}
}
return null;
}
var interceptor = {
'response': function (response) {
if (response.data) {
convertDateStringsToDates(response.data);
}
return response;
}
};
return interceptor;
})
app.config(HttpConfig);
HttpConfig.$inject = ['$httpProvider'];
function HttpConfig($httpProvider) {
$httpProvider.interceptors.push('dateInterceptor'); // intercept responses and convert date strings into real dates
}
})(angular.module('dateInterceptor',[]));