-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refa(src/*): refactor improve things
- Loading branch information
Showing
4 changed files
with
34 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
'use strict'; | ||
var angularLocalStorage = angular.module('LocalStorageModule', []); | ||
|
||
angularLocalStorage.provider('localStorageService', function() { | ||
|
@@ -114,10 +113,10 @@ angularLocalStorage.provider('localStorageService', function() { | |
// Example use: localStorageService.add('library','angular'); | ||
var addToLocalStorage = function (key, value) { | ||
// Let's convert undefined values to null to get the value consistent | ||
if (typeof value === "undefined") { | ||
if (isUndefined(value)) { | ||
value = null; | ||
} else if (angular.isObject(value) || angular.isArray(value) || angular.isNumber(+value || value)) { | ||
value = angular.toJson(value); | ||
} else if (isObject(value) || isArray(value) || isNumber(+value || value)) { | ||
value = toJson(value); | ||
} | ||
|
||
// If this browser does not support local storage use cookies | ||
|
@@ -133,8 +132,8 @@ angularLocalStorage.provider('localStorageService', function() { | |
} | ||
|
||
try { | ||
if (angular.isObject(value) || angular.isArray(value)) { | ||
value = angular.toJson(value); | ||
if (isObject(value) || isArray(value)) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
a8m
Author
Collaborator
|
||
value = toJson(value); | ||
} | ||
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)}; | ||
if (notify.setItem) { | ||
|
@@ -167,18 +166,12 @@ angularLocalStorage.provider('localStorageService', function() { | |
} | ||
|
||
if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) { | ||
return angular.fromJson(item); | ||
return fromJson(item); | ||
} | ||
|
||
return item; | ||
}; | ||
|
||
// Test if string is only contains numbers | ||
// e.g '1' => true, "'1'" => true | ||
function isStringNumber(num) { | ||
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, '')); | ||
} | ||
|
||
// Remove an item from local storage | ||
// Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular' | ||
var removeFromLocalStorage = function (key) { | ||
|
@@ -282,10 +275,10 @@ angularLocalStorage.provider('localStorageService', function() { | |
// Example use: localStorageService.cookie.add('library','angular'); | ||
var addToCookies = function (key, value) { | ||
|
||
if (typeof value === "undefined") { | ||
if (isUndefined(value)) { | ||
return false; | ||
} else if(angular.isArray(value) || angular.isObject(value)) { | ||
value = angular.toJson(value); | ||
} else if(isArray(value) || isObject(value)) { | ||
value = toJson(value); | ||
} | ||
|
||
if (!browserSupportsCookies()) { | ||
|
@@ -338,8 +331,8 @@ angularLocalStorage.provider('localStorageService', function() { | |
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) { | ||
var storedValues = decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length)) | ||
try{ | ||
var obj = JSON.parse(storedValues) | ||
return angular.fromJson(obj) | ||
var obj = JSON.parse(storedValues); | ||
return fromJson(obj) | ||
}catch(e){ | ||
return storedValues | ||
} | ||
|
@@ -379,10 +372,10 @@ angularLocalStorage.provider('localStorageService', function() { | |
|
||
var value = getFromLocalStorage(lsKey); | ||
|
||
if (value === null && angular.isDefined(def)) { | ||
if (value === null && isDefined(def)) { | ||
value = def; | ||
} else if (angular.isObject(value) && angular.isObject(def)) { | ||
value = angular.extend(def, value); | ||
} else if (isObject(value) && isObject(def)) { | ||
value = extend(def, value); | ||
} | ||
|
||
$parse(scopeKey).assign(scope, value); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/*jshint globalstrict:true*/ | ||
'use strict'; | ||
|
||
var isDefined = angular.isDefined, | ||
isUndefined = angular.isUndefined, | ||
isNumber = angular.isNumber, | ||
isObject = angular.isObject, | ||
isArray = angular.isArray, | ||
extend = angular.extend, | ||
toJson = angular.toJson, | ||
fromJson = angular.fromJson; | ||
|
||
|
||
// Test if string is only contains numbers | ||
// e.g '1' => true, "'1'" => true | ||
function isStringNumber(num) { | ||
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, '')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@a8m : Why is the value being stringified twice? First at L118, and then again here?