Skip to content

Commit d2bc39f

Browse files
committed
refa(src/*): refactor improve things
1 parent 8a4bdc2 commit d2bc39f

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function(grunt) {
3030
footer: '})( window, window.angular );'
3131
},
3232
dist: {
33-
src: ['src/*.js'],
33+
src: ['src/common.js', 'src/angular-local-storage.js'],
3434
dest: '<%= dirs.dest %>/<%= pkg.name %>.js'
3535
}
3636
},

src/angular-local-storage.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict';
21
var angularLocalStorage = angular.module('LocalStorageModule', []);
32

43
angularLocalStorage.provider('localStorageService', function() {
@@ -114,10 +113,10 @@ angularLocalStorage.provider('localStorageService', function() {
114113
// Example use: localStorageService.add('library','angular');
115114
var addToLocalStorage = function (key, value) {
116115
// Let's convert undefined values to null to get the value consistent
117-
if (typeof value === "undefined") {
116+
if (isUndefined(value)) {
118117
value = null;
119-
} else if (angular.isObject(value) || angular.isArray(value) || angular.isNumber(+value || value)) {
120-
value = angular.toJson(value);
118+
} else if (isObject(value) || isArray(value) || isNumber(+value || value)) {
119+
value = toJson(value);
121120
}
122121

123122
// If this browser does not support local storage use cookies
@@ -133,8 +132,8 @@ angularLocalStorage.provider('localStorageService', function() {
133132
}
134133

135134
try {
136-
if (angular.isObject(value) || angular.isArray(value)) {
137-
value = angular.toJson(value);
135+
if (isObject(value) || isArray(value)) {
136+
value = toJson(value);
138137
}
139138
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
140139
if (notify.setItem) {
@@ -167,18 +166,12 @@ angularLocalStorage.provider('localStorageService', function() {
167166
}
168167

169168
if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) {
170-
return angular.fromJson(item);
169+
return fromJson(item);
171170
}
172171

173172
return item;
174173
};
175174

176-
// Test if string is only contains numbers
177-
// e.g '1' => true, "'1'" => true
178-
function isStringNumber(num) {
179-
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
180-
}
181-
182175
// Remove an item from local storage
183176
// Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular'
184177
var removeFromLocalStorage = function (key) {
@@ -282,10 +275,10 @@ angularLocalStorage.provider('localStorageService', function() {
282275
// Example use: localStorageService.cookie.add('library','angular');
283276
var addToCookies = function (key, value) {
284277

285-
if (typeof value === "undefined") {
278+
if (isUndefined(value)) {
286279
return false;
287-
} else if(angular.isArray(value) || angular.isObject(value)) {
288-
value = angular.toJson(value);
280+
} else if(isArray(value) || isObject(value)) {
281+
value = toJson(value);
289282
}
290283

291284
if (!browserSupportsCookies()) {
@@ -338,8 +331,8 @@ angularLocalStorage.provider('localStorageService', function() {
338331
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
339332
var storedValues = decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length))
340333
try{
341-
var obj = JSON.parse(storedValues)
342-
return angular.fromJson(obj)
334+
var obj = JSON.parse(storedValues);
335+
return fromJson(obj)
343336
}catch(e){
344337
return storedValues
345338
}
@@ -379,10 +372,10 @@ angularLocalStorage.provider('localStorageService', function() {
379372

380373
var value = getFromLocalStorage(lsKey);
381374

382-
if (value === null && angular.isDefined(def)) {
375+
if (value === null && isDefined(def)) {
383376
value = def;
384-
} else if (angular.isObject(value) && angular.isObject(def)) {
385-
value = angular.extend(def, value);
377+
} else if (isObject(value) && isObject(def)) {
378+
value = extend(def, value);
386379
}
387380

388381
$parse(scopeKey).assign(scope, value);

src/common.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*jshint globalstrict:true*/
2+
'use strict';
3+
4+
var isDefined = angular.isDefined,
5+
isUndefined = angular.isUndefined,
6+
isNumber = angular.isNumber,
7+
isObject = angular.isObject,
8+
isArray = angular.isArray,
9+
extend = angular.extend,
10+
toJson = angular.toJson,
11+
fromJson = angular.fromJson;
12+
13+
14+
// Test if string is only contains numbers
15+
// e.g '1' => true, "'1'" => true
16+
function isStringNumber(num) {
17+
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
18+
}

test/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function(config) {
2828
files: [
2929
bower + 'angular/angular.js',
3030
bower + 'angular-mocks/angular-mocks.js',
31-
'src/angular-local-storage.js',
31+
'src/*.js',
3232
'test/spec/**/*.js'
3333
],
3434

0 commit comments

Comments
 (0)