1/** 2 * Creates a new LocalStorageService 3 * @class 4 * @implements {StorageServiceInterface} 5 */ 6var LocalStorageService = function () { 7 /** 8 * Implements StorageServiceInterface 9 */ 10 StorageServiceInterface.call(this); 11 12 /** 13 * @property {(object|null)} [lastResponse=null] - Store the last response 14 */ 15 this.lastResponse = null; 16 17 /** 18 * @property {object} lastError - Store the last error 19 */ 20 this.lastError = null; 21 22 /** 23 * @constructs LocalStorageService 24 */ 25 this.setup = function () { 26 return this; 27 } 28 29 /** 30 * @inheritdoc 31 */ 32 this.has = function (key) { 33 if (!this.hasLocalStorage()) { 34 return false; 35 } else if (helper.isEmpty(key)) { 36 this.lastError = 'No key were supplied for this method'; 37 return false; 38 } 39 40 var hasItem = false; 41 try { 42 var item = this.formatItem(key, localStorage.getItem(key)); 43 // If fetched item has been expired 44 if (this.expired(item.expiration)) { 45 item = this.remove(item.key) ? null : item; 46 } 47 48 this.lastResponse = item || this.lastResponse; 49 50 hasItem = item ? true : false; 51 } catch (error) { 52 this.lastError = error; 53 } 54 55 return hasItem; 56 }; 57 58 /** 59 * @inheritdoc 60 */ 61 this.fetch = function (key) { 62 if (!this.hasLocalStorage()) { 63 return false; 64 } else if (helper.isEmpty(key)) { 65 this.lastError = 'No key were supplied for this method'; 66 return false; 67 } 68 69 var item = null; 70 try { 71 var tempItem = localStorage.getItem(key); 72 73 // If fetched item exists 74 if (tempItem) { 75 tempItem = this.formatItem(key, tempItem); 76 77 // If fetched item has been expired 78 if (this.expired(tempItem.expiration)) { 79 tempItem = this.remove(tempItem.key) ? null : tempItem; 80 } 81 82 item = tempItem; 83 } 84 85 this.lastResponse = item || this.lastResponse; 86 } catch (error) { 87 console.error(error); 88 this.lastError = error; 89 } 90 91 return item; 92 }; 93 94 /** 95 * @inheritdoc 96 */ 97 this.save = function (key, value, expiration) { 98 if (!this.hasLocalStorage()) { 99 return false; 100 } else if (helper.isEmpty(key)) { 101 this.lastError = 'No key were supplied for this method'; 102 return false; 103 } else if (value == null) { 104 this.lastError = 'The value parameter can not be null'; 105 return false; 106 } 107 108 expiration = expiration || null; 109 110 var saved = false; 111 try { 112 var item = this.formatItem(key, value, expiration); 113 if (item) { 114 item = JSON.stringify(item); 115 } 116 117 localStorage.setItem(key, item); 118 this.lastResponse = item || this.lastResponse; 119 saved = true; 120 } catch (error) { 121 console.error(error); 122 this.lastError = error; 123 } 124 125 return saved; 126 }; 127 128 /** 129 * @inheritdoc 130 */ 131 this.remove = function (key) { 132 if (!this.hasLocalStorage()) { 133 return false; 134 } else if (helper.isEmpty(key)) { 135 this.lastError = 'No key were supplied for this method'; 136 return false; 137 } 138 139 var deleted = true; 140 141 try { 142 localStorage.removeItem(key); 143 } catch (error) { 144 deleted = false; 145 this.lastError = error; 146 } 147 148 return deleted; 149 }; 150 151 /** 152 * Return true if 'localStorage' exists, otherwise, false 153 * @returns {boolean} 154 */ 155 this.hasLocalStorage = function () { 156 if (typeof localStorage === 'undefined' || !localStorage) { 157 this.lastError = 'There is no localStorage available on this browser'; 158 return false; 159 } 160 161 return true; 162 } 163 164 /** 165 * Format and return an item 166 * @param {string} key 167 * @param {*} value 168 * @param {(number|null)} [expiration = null] - Expiration time(miliseconds) 169 * @return {({key: string, value: *, expiration: number}
169|null)} - Return a formatted item 170 */ 171 this.formatItem = function (key, value, expiration) { 172 if (helper.isEmpty(key) || value == null) { 173 return null; 174 } 175 176 try { 177 value = JSON.parse(value); 178 if (this.isStorageItem(value)) { 179 return value; 180 } 181 } catch (error) { 182 this.lastError = error; 183 } 184 185 expiration = expiration || null; 186 if (helper.isNumeric(expiration)) { 187 expiration = Date.now() + expiration; 188 } 189 190 var item = { 191 key: key, 192 value: value, 193 expiration: expiration 194 } 195 196 return item; 197 } 198 199 /** 200 * Return true if item is a StorageItem, otherwise, false 201 * @param {object} item 202 * @returns {boolean} 203 */ 204 this.isStorageItem = function (item) { 205 if (helper.isEmpty(item)) { 206 this.lastError = 'No item has been provided'; 207 return false; 208 } else if (!('key' in item) || !('value' in item) || !('expiration' in item)) { 209 this.lastError = 'Item provided is not a StorageItem'; 210 return false; 211 } 212 213 return true; 214 } 215 216 /** 217 * Return true if the param has been expired, otherwise, false 218 * @param {number} expiration 219 * @returns {boolean} 220 */ 221 this.expired = function(expiration) { 222 if (!helper.isNumeric(expiration)) { 223 return false; 224 } else if (!(Date.now() > expiration)) { 225 return false; 226 } 227 228 return true; 229 } 230} 231 232/** 233 * Assign the StorageServiceInterface prototype 234 */ 235LocalStorageService.prototype = Object.create(StorageServiceInterface.prototype); 236 237/** 238 * Assign the LocalStorageService.setup() method as prototype constructor 239 */ 240LocalStorageService.prototype.constructor = (new LocalStorageService()).setup();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.