Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #241 #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 24 additions & 2 deletions src/angular-local-storage.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -164,7 +186,7 @@ angularLocalStorage.provider('localStorageService', function() {
}

try {
return JSON.parse(item);
return JSON.parse(item, jsonDateParser);
} catch (e) {
return item;
}
Expand Down
9 changes: 9 additions & 0 deletions test/spec/localStorageSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down