diff --git a/bower.json b/bower.json index 6788478..3a99b00 100644 --- a/bower.json +++ b/bower.json @@ -27,7 +27,10 @@ "Gruntfile.js" ], "devDependencies": { - "angular": "~1.x", - "angular-mocks": "~1.2.1" + "angular": "~1.4.0", + "angular-mocks": "~1.4.0" + }, + "resolutions": { + "angular": "1.4.3" } } diff --git a/package.json b/package.json index caeef92..4ccfbae 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "grunt-contrib-jshint": "~0.8.0", "grunt-contrib-uglify": "*", "grunt-karma": "latest", - "karma": "~0.12.16", + "karma": "~0.13.0", "karma-jasmine": "~0.1.5", "karma-coverage": "^0.2.6", "karma-phantomjs-launcher": "~0.1.4", diff --git a/src/angular-local-storage.js b/src/angular-local-storage.js index 0e3048e..6f0145c 100644 --- a/src/angular-local-storage.js +++ b/src/angular-local-storage.js @@ -1,5 +1,27 @@ var angularLocalStorage = angular.module('LocalStorageModule', []); +angularLocalStorage.service('jsonDateParser', function jsonDateParser () { + var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/, + reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/; + + return function (key, value) { + if (typeof value === 'string') { + var a = reISO.exec(value); + + if (a) { + return new Date(value); + } + a = reMsAjax.exec(value); + + if (a) { + var b = a[1].split(/[-+,.]/); + return new Date(b[0] ? +b[0] : 0 - +b[1]); + } + } + return value; + }; +}); + angularLocalStorage.provider('localStorageService', function() { // You should set a prefix to avoid overwriting any local storage variables from the rest of your app @@ -62,7 +84,7 @@ angularLocalStorage.provider('localStorageService', function() { return this; }; - this.$get = ['$rootScope', '$window', '$document', '$parse', function($rootScope, $window, $document, $parse) { + this.$get = ['$rootScope', '$window', '$document', '$parse', 'jsonDateParser', function($rootScope, $window, $document, $parse, jsonDateParser) { var self = this; var prefix = self.prefix; var cookie = self.cookie; @@ -164,7 +186,7 @@ angularLocalStorage.provider('localStorageService', function() { } try { - return JSON.parse(item); + return JSON.parse(item, jsonDateParser); } catch (e) { return item; } diff --git a/test/spec/localStorageSpec.js b/test/spec/localStorageSpec.js index 7390c94..70ff62b 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -270,6 +270,15 @@ describe('localStorageService', function() { ) }); + it('should be able to set and get objects contains date - issue #241', function() { + var t = {x: new Date(946684800000)}; // 2000-JAN-01 00:00:00 + inject( + addItem('key', t), + expectAdding('ls.key', angular.toJson(t)), + expectMatching('key', t) + ); + }); + it('should be able to get items', inject( getItem('key'), expectGetting('ls.key')