@studyportals/data-storage
Advanced tools
Comparing version 1.2.1-alpha.1 to 1.2.1
@@ -1,14 +0,7 @@ | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/** | ||
* Create a browser independent storage solution. | ||
*/ | ||
var DataStorage = { | ||
let DataStorage = { | ||
enabled: null, | ||
@@ -33,3 +26,3 @@ | ||
*/ | ||
init: function init() { | ||
init() { | ||
@@ -53,3 +46,4 @@ if (typeof localStorage !== 'undefined') { | ||
this.enabled = true; | ||
} catch (error) { | ||
} | ||
catch (error) { | ||
@@ -66,7 +60,7 @@ this.enabled = false; | ||
* @param {String} $value - LocalStorage key values. | ||
* @param {Number} [$ttl] - LocalStorage custom expiry date ins seconds. | ||
* @param {Number} [$ttl] - LocalStorage custom expiry date in seconds. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
store: function store($key, $value, $ttl) { | ||
store($key, $value, $ttl) { | ||
@@ -81,10 +75,11 @@ if (isNaN($ttl)) { | ||
document.cookie = $key + '=' + $value + ';expires=' + $ttl / 86400; | ||
} else { | ||
document.cookie = `${$key}=${$value};expires=${$ttl / 86400}`; | ||
} | ||
else { | ||
// Manually insert an expiry date | ||
var $time = new Date(); | ||
let $time = new Date(); | ||
$time = $time.setSeconds($time.getSeconds() + $ttl); | ||
var $container = { | ||
let $container = { | ||
@@ -107,3 +102,3 @@ 'expires': $time, | ||
*/ | ||
retrieve: function retrieve($key) { | ||
retrieve($key) { | ||
@@ -113,8 +108,8 @@ // Get the required cookie. | ||
var pattern = RegExp(name + "=.[^;]*"); | ||
var matched = document.cookie.match(pattern); | ||
let pattern = RegExp(name + "=.[^;]*"); | ||
let matched = document.cookie.match(pattern); | ||
if (matched) { | ||
var cookie = matched[0].split('='); | ||
let cookie = matched[0].split('='); | ||
return cookie[1]; | ||
@@ -130,9 +125,11 @@ } | ||
return getCookie($key); | ||
} else { | ||
} | ||
else { | ||
var $decoded = localStorage[$key]; | ||
let $decoded = localStorage[$key]; | ||
try { | ||
$decoded = JSON.parse($decoded); | ||
} catch (error) { | ||
} | ||
catch (error) { | ||
@@ -147,3 +144,3 @@ $decoded = {}; | ||
var $expiryDate = parseInt($decoded.expires, 10); | ||
let $expiryDate = parseInt($decoded.expires, 10); | ||
if (isNaN($expiryDate)) { | ||
@@ -165,3 +162,2 @@ | ||
/** | ||
@@ -173,13 +169,15 @@ * Remove a key from the data source. | ||
*/ | ||
remove: function remove($key) { | ||
remove($key) { | ||
// Clear the specific localStorage or cookie entry. | ||
if (!this.enabled) { | ||
document.cookie = $key + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; | ||
} else { | ||
localStorage[$key] = null; | ||
document.cookie = `${$key}=;expires=Thu, 01 Jan 1970 00:00:01 GMT;`; | ||
} | ||
else { | ||
delete localStorage[$key]; | ||
} | ||
}, | ||
/** | ||
@@ -201,7 +199,6 @@ * Updates an object in local storage | ||
*/ | ||
update: function update($key, $value) { | ||
var $ttl = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; | ||
update($key, $value, $ttl = null) { | ||
var $old = this.retrieve($key); | ||
if ((typeof $old === 'undefined' ? 'undefined' : _typeof($old)) !== 'object') { | ||
let $old = this.retrieve($key); | ||
if (typeof $old !== 'object') { | ||
@@ -220,2 +217,2 @@ $old = {}; | ||
// Export the module. | ||
exports.default = DataStorage; | ||
export default DataStorage; |
@@ -1,1 +0,3 @@ | ||
module.exports.DataStorage = require('./classes/DataStorage').default; | ||
export default { | ||
DataStorage: require('./classes/DataStorage') | ||
}; |
{ | ||
"name": "@studyportals/data-storage", | ||
"version": "1.2.1-alpha.1", | ||
"version": "1.2.1", | ||
"description": "Create a browser independent local storage solution.", | ||
@@ -15,2 +15,3 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"dev": "cypress open", | ||
"prepublishOnly": "npm run test", | ||
@@ -17,0 +18,0 @@ "test:cypress": "cypress run", |
# DataStorage | ||
Create a browser independent local storage solution. | ||
## Installation: | ||
This package is written in ES5 and is available on NPM. | ||
```node | ||
@@ -11,4 +10,3 @@ npm install --save @studyportals/data-storage | ||
## Example: | ||
## Functions: | ||
_Javascript_ | ||
@@ -18,9 +16,26 @@ ```javascript | ||
const AnyObjectStorage = DataStorage.retrieve('AnyObject'); | ||
const PropertiesToStore = { | ||
/** | ||
* Store any type of value with an TTL of 1800 seconds. | ||
*/ | ||
DataStorage.store('AnyObject', { | ||
prop1: 'test-1', | ||
prop2: 'test-2' | ||
}; | ||
DataStorage.store('AnyObject', PropertiesToStore); | ||
}, 1800); | ||
/** | ||
* Retrieve from the storage. | ||
*/ | ||
DataStorage.retrieve('AnyObject'); | ||
/** | ||
* Remove an object from the storage. | ||
*/ | ||
DataStorage.remove('AnyObject'); | ||
/** | ||
* Add properties to an existing object with an TTL of 1800 seconds. | ||
*/ | ||
DataStorage.update('AnyObject', { | ||
prop3: 'test-3' | ||
}, 1800); | ||
``` |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
68770
8
1919
1
40
1