From d2bc39f2ba4097568a573873ab039fb7b6788850 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Tue, 7 Oct 2014 01:12:16 +0300 Subject: [PATCH] refa(src/*): refactor improve things --- Gruntfile.js | 2 +- src/angular-local-storage.js | 35 ++++++++++++++--------------------- src/common.js | 18 ++++++++++++++++++ test/karma.conf.js | 2 +- 4 files changed, 34 insertions(+), 23 deletions(-) create mode 100644 src/common.js diff --git a/Gruntfile.js b/Gruntfile.js index feb8df2..772c4a5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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' } }, diff --git a/src/angular-local-storage.js b/src/angular-local-storage.js index 360a9ab..9b2e58e 100644 --- a/src/angular-local-storage.js +++ b/src/angular-local-storage.js @@ -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)) { + 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); diff --git a/src/common.js b/src/common.js new file mode 100644 index 0000000..74172d4 --- /dev/null +++ b/src/common.js @@ -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, '')); +} diff --git a/test/karma.conf.js b/test/karma.conf.js index 08ee9fb..50459db 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -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' ],