Skip to content

Commit

Permalink
refa(src/*): refactor improve things
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Oct 6, 2014
1 parent 8a4bdc2 commit d2bc39f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = function(grunt) {
footer: '})( window, window.angular );'
},
dist: {
src: ['src/*.js'],
src: ['src/common.js', 'src/angular-local-storage.js'],
dest: '<%= dirs.dest %>/<%= pkg.name %>.js'
}
},
Expand Down
35 changes: 14 additions & 21 deletions src/angular-local-storage.js
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() {
Expand Down Expand Up @@ -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
Expand All @@ -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.

Copy link
@kumarharsh

kumarharsh Feb 11, 2015

Contributor

@a8m : Why is the value being stringified twice? First at L118, and then again here?

This comment has been minimized.

Copy link
@a8m

a8m Feb 11, 2015

Author Collaborator

After L118 it will be a string
i.e: it's only stringified when the value in null(L117), never twice.
anyway, it should remove from here.
PR welcome.
thx

value = toJson(value);
}
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
if (notify.setItem) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/common.js
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, ''));
}
2 changes: 1 addition & 1 deletion test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function(config) {
files: [
bower + 'angular/angular.js',
bower + 'angular-mocks/angular-mocks.js',
'src/angular-local-storage.js',
'src/*.js',
'test/spec/**/*.js'
],

Expand Down

0 comments on commit d2bc39f

Please sign in to comment.