Skip to content

Commit fef5215

Browse files
committed
fixed: parsing dates from state when using location store
1 parent 92b58eb commit fef5215

9 files changed

Lines changed: 239 additions & 200 deletions

ne-modules-all.js

Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,7 +4227,7 @@ angular.module('neObject',[])
42274227
for(var key in parentObj){
42284228
if(angular.isObject(parentObj[key])) deepReplace(parentObj[key], cb, (keyPath==='' ? key : keyPath + '.' + key));
42294229
value = cb((keyPath==='' ? key : keyPath + '.' + key), key, parentObj[key]);
4230-
if(!value) delete parentObj[key];
4230+
if(value===undefined) delete parentObj[key];
42314231
else parentObj[key] = value;
42324232
}
42334233
}
@@ -4429,6 +4429,69 @@ angular.module('neObject',[])
44294429
};
44304430
}
44314431

4432+
// auto parse dates
4433+
var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,})(Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/;
4434+
var regexIsoJson = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
4435+
4436+
function dateStringsToDates(input, useIsoJson) {
4437+
var value, match, milliseconds;
4438+
4439+
// try to parse if input is string
4440+
if(typeof input === 'string' && (match = input.match(useIsoJson ? regexIsoJson : regexIso8601))) {
4441+
milliseconds = Date.parse(match[0]);
4442+
if (!isNaN(milliseconds)) {
4443+
input = new Date(milliseconds);
4444+
}
4445+
return input;
4446+
}
4447+
4448+
// Ignore things that aren't objects
4449+
else if(typeof input !== 'object') return input;
4450+
4451+
for(var key in input){
4452+
value = input[key];
4453+
4454+
// Check for string properties which look like dates.
4455+
if(typeof value === 'string' && (match = value.match(useIsoJson ? regexIsoJson : regexIso8601))) {
4456+
milliseconds = Date.parse(match[0]);
4457+
if (!isNaN(milliseconds)) {
4458+
input[key] = new Date(milliseconds);
4459+
}
4460+
}
4461+
else if (typeof value === 'object') {
4462+
// Recurse into object
4463+
dateStringsToDates(value, useIsoJson);
4464+
}
4465+
}
4466+
return input;
4467+
}
4468+
4469+
function fromJson(str, useIsoJson){
4470+
var result;
4471+
try {
4472+
result = JSON.parse(str);
4473+
}
4474+
catch(err){}
4475+
4476+
return dateStringsToDates(result, useIsoJson);
4477+
}
4478+
4479+
function removePrefixedProps(input, prefix) {
4480+
4481+
// Ignore things that aren't objects.
4482+
if(typeof input !== 'object' || !prefix) return input;
4483+
4484+
for(var key in input) {
4485+
if(input.hasOwnProperty(key)) {
4486+
var value = input[key];
4487+
4488+
if(key.indexOf(prefix)===0) delete input[key];
4489+
else if(typeof value === 'object') removePrefixedProps(value, prefix);
4490+
}
4491+
}
4492+
}
4493+
4494+
44324495
return {
44334496
extendReservedInstances: [File, FileList, Blob],
44344497
extend: extend,
@@ -4442,6 +4505,10 @@ angular.module('neObject',[])
44424505
deepEqual: deepEquals,
44434506
objectToArray: objectToArray,
44444507
arrayToObject: arrayToObject,
4508+
dateStringsToDates: dateStringsToDates,
4509+
fromJson: fromJson,
4510+
fromJSON: fromJson,
4511+
removePrefixedProps: removePrefixedProps,
44454512
debounce: debounce
44464513
};
44474514
}]);
@@ -5894,6 +5961,12 @@ angular.module('neRest',['neObject','neNotifications','neLoading'])
58945961
// add default XHR header
58955962
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
58965963
}])
5964+
.factory('NeResourceTransformators.dateStringsToDates', ['neObject', function(object){
5965+
return object.dateStringsToDates;
5966+
}])
5967+
.factory('NeResourceTransformators.removePrefixedProps', ['neObject', function(object){
5968+
return object.removePrefixedProps;
5969+
}])
58975970
.factory('NeRestResource', ['$http',
58985971
'$timeout',
58995972
'neObject',
@@ -6445,66 +6518,6 @@ angular.module('neRest',['neObject','neNotifications','neLoading'])
64456518

64466519
return Resource;
64476520

6448-
}])
6449-
.factory('NeResourceTransformators.dateStringsToDates', [function(){
6450-
6451-
// auto parse dates
6452-
var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,})(Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/;
6453-
var regexIsoJson = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
6454-
6455-
function dateStringsToDates(input, useIsoJson) {
6456-
var value, match, milliseconds;
6457-
6458-
// try to parse if input is string
6459-
if(typeof input === 'string' && (match = input.match(useIsoJson ? regexIsoJson : regexIso8601))) {
6460-
milliseconds = Date.parse(match[0]);
6461-
if (!isNaN(milliseconds)) {
6462-
input = new Date(milliseconds);
6463-
}
6464-
return input;
6465-
}
6466-
6467-
// Ignore things that aren't objects
6468-
else if(typeof input !== 'object') return input;
6469-
6470-
for(var key in input){
6471-
value = input[key];
6472-
6473-
// Check for string properties which look like dates.
6474-
if(typeof value === 'string' && (match = value.match(useIsoJson ? regexIsoJson : regexIso8601))) {
6475-
milliseconds = Date.parse(match[0]);
6476-
if (!isNaN(milliseconds)) {
6477-
input[key] = new Date(milliseconds);
6478-
}
6479-
}
6480-
else if (typeof value === 'object') {
6481-
// Recurse into object
6482-
dateStringsToDates(value, useIsoJson);
6483-
}
6484-
}
6485-
return input;
6486-
}
6487-
6488-
return dateStringsToDates;
6489-
}])
6490-
.factory('NeResourceTransformators.removePrefixedProps', [function(){
6491-
6492-
function removePrefixedProps(input, prefix) {
6493-
6494-
// Ignore things that aren't objects.
6495-
if(typeof input !== 'object' || !prefix) return input;
6496-
6497-
for(var key in input) {
6498-
if(input.hasOwnProperty(key)) {
6499-
var value = input[key];
6500-
6501-
if(key.indexOf(prefix)===0) delete input[key];
6502-
else if(typeof value === 'object') removePrefixedProps(value, prefix);
6503-
}
6504-
}
6505-
}
6506-
6507-
return removePrefixedProps;
65086521
}]);
65096522
/**
65106523
* NE STATE
@@ -6533,7 +6546,7 @@ angular.module('neState', ['ngCookies'])
65336546

65346547
try {
65356548
if(encryptLocation) locationString = decryptString(locationString);
6536-
return angular.fromJson(locationString || '{}') || {};
6549+
return object.fromJson(locationString || '{}') || {};
65376550
}
65386551
catch(err){}
65396552
return {};

ne-modules-all.min.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ne-modules-all.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ne-modules.js

Lines changed: 75 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,7 +4227,7 @@ angular.module('neObject',[])
42274227
for(var key in parentObj){
42284228
if(angular.isObject(parentObj[key])) deepReplace(parentObj[key], cb, (keyPath==='' ? key : keyPath + '.' + key));
42294229
value = cb((keyPath==='' ? key : keyPath + '.' + key), key, parentObj[key]);
4230-
if(!value) delete parentObj[key];
4230+
if(value===undefined) delete parentObj[key];
42314231
else parentObj[key] = value;
42324232
}
42334233
}
@@ -4429,6 +4429,69 @@ angular.module('neObject',[])
44294429
};
44304430
}
44314431

4432+
// auto parse dates
4433+
var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,})(Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/;
4434+
var regexIsoJson = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
4435+
4436+
function dateStringsToDates(input, useIsoJson) {
4437+
var value, match, milliseconds;
4438+
4439+
// try to parse if input is string
4440+
if(typeof input === 'string' && (match = input.match(useIsoJson ? regexIsoJson : regexIso8601))) {
4441+
milliseconds = Date.parse(match[0]);
4442+
if (!isNaN(milliseconds)) {
4443+
input = new Date(milliseconds);
4444+
}
4445+
return input;
4446+
}
4447+
4448+
// Ignore things that aren't objects
4449+
else if(typeof input !== 'object') return input;
4450+
4451+
for(var key in input){
4452+
value = input[key];
4453+
4454+
// Check for string properties which look like dates.
4455+
if(typeof value === 'string' && (match = value.match(useIsoJson ? regexIsoJson : regexIso8601))) {
4456+
milliseconds = Date.parse(match[0]);
4457+
if (!isNaN(milliseconds)) {
4458+
input[key] = new Date(milliseconds);
4459+
}
4460+
}
4461+
else if (typeof value === 'object') {
4462+
// Recurse into object
4463+
dateStringsToDates(value, useIsoJson);
4464+
}
4465+
}
4466+
return input;
4467+
}
4468+
4469+
function fromJson(str, useIsoJson){
4470+
var result;
4471+
try {
4472+
result = JSON.parse(str);
4473+
}
4474+
catch(err){}
4475+
4476+
return dateStringsToDates(result, useIsoJson);
4477+
}
4478+
4479+
function removePrefixedProps(input, prefix) {
4480+
4481+
// Ignore things that aren't objects.
4482+
if(typeof input !== 'object' || !prefix) return input;
4483+
4484+
for(var key in input) {
4485+
if(input.hasOwnProperty(key)) {
4486+
var value = input[key];
4487+
4488+
if(key.indexOf(prefix)===0) delete input[key];
4489+
else if(typeof value === 'object') removePrefixedProps(value, prefix);
4490+
}
4491+
}
4492+
}
4493+
4494+
44324495
return {
44334496
extendReservedInstances: [File, FileList, Blob],
44344497
extend: extend,
@@ -4442,6 +4505,10 @@ angular.module('neObject',[])
44424505
deepEqual: deepEquals,
44434506
objectToArray: objectToArray,
44444507
arrayToObject: arrayToObject,
4508+
dateStringsToDates: dateStringsToDates,
4509+
fromJson: fromJson,
4510+
fromJSON: fromJson,
4511+
removePrefixedProps: removePrefixedProps,
44454512
debounce: debounce
44464513
};
44474514
}]);
@@ -5894,6 +5961,12 @@ angular.module('neRest',['neObject','neNotifications','neLoading'])
58945961
// add default XHR header
58955962
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
58965963
}])
5964+
.factory('NeResourceTransformators.dateStringsToDates', ['neObject', function(object){
5965+
return object.dateStringsToDates;
5966+
}])
5967+
.factory('NeResourceTransformators.removePrefixedProps', ['neObject', function(object){
5968+
return object.removePrefixedProps;
5969+
}])
58975970
.factory('NeRestResource', ['$http',
58985971
'$timeout',
58995972
'neObject',
@@ -6445,66 +6518,6 @@ angular.module('neRest',['neObject','neNotifications','neLoading'])
64456518

64466519
return Resource;
64476520

6448-
}])
6449-
.factory('NeResourceTransformators.dateStringsToDates', [function(){
6450-
6451-
// auto parse dates
6452-
var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,})(Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/;
6453-
var regexIsoJson = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/;
6454-
6455-
function dateStringsToDates(input, useIsoJson) {
6456-
var value, match, milliseconds;
6457-
6458-
// try to parse if input is string
6459-
if(typeof input === 'string' && (match = input.match(useIsoJson ? regexIsoJson : regexIso8601))) {
6460-
milliseconds = Date.parse(match[0]);
6461-
if (!isNaN(milliseconds)) {
6462-
input = new Date(milliseconds);
6463-
}
6464-
return input;
6465-
}
6466-
6467-
// Ignore things that aren't objects
6468-
else if(typeof input !== 'object') return input;
6469-
6470-
for(var key in input){
6471-
value = input[key];
6472-
6473-
// Check for string properties which look like dates.
6474-
if(typeof value === 'string' && (match = value.match(useIsoJson ? regexIsoJson : regexIso8601))) {
6475-
milliseconds = Date.parse(match[0]);
6476-
if (!isNaN(milliseconds)) {
6477-
input[key] = new Date(milliseconds);
6478-
}
6479-
}
6480-
else if (typeof value === 'object') {
6481-
// Recurse into object
6482-
dateStringsToDates(value, useIsoJson);
6483-
}
6484-
}
6485-
return input;
6486-
}
6487-
6488-
return dateStringsToDates;
6489-
}])
6490-
.factory('NeResourceTransformators.removePrefixedProps', [function(){
6491-
6492-
function removePrefixedProps(input, prefix) {
6493-
6494-
// Ignore things that aren't objects.
6495-
if(typeof input !== 'object' || !prefix) return input;
6496-
6497-
for(var key in input) {
6498-
if(input.hasOwnProperty(key)) {
6499-
var value = input[key];
6500-
6501-
if(key.indexOf(prefix)===0) delete input[key];
6502-
else if(typeof value === 'object') removePrefixedProps(value, prefix);
6503-
}
6504-
}
6505-
}
6506-
6507-
return removePrefixedProps;
65086521
}]);
65096522
/**
65106523
* NE STATE
@@ -6533,7 +6546,7 @@ angular.module('neState', ['ngCookies'])
65336546

65346547
try {
65356548
if(encryptLocation) locationString = decryptString(locationString);
6536-
return angular.fromJson(locationString || '{}') || {};
6549+
return object.fromJson(locationString || '{}') || {};
65376550
}
65386551
catch(err){}
65396552
return {};

ne-modules.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ne-modules.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)