superstore-sync
Advanced tools
Comparing version
@@ -8,3 +8,5 @@ /** | ||
var escapeRegex = require('escape-regexp-component'); | ||
var escapeRegex = function(str){ | ||
return String(str).replace(/([.*+?=^!:${}()|[\]\/\\])/g, '\\$1'); | ||
}; | ||
@@ -23,2 +25,6 @@ var keys = {}; | ||
function Superstore(type) { | ||
this.storage = window[type]; | ||
} | ||
/** | ||
@@ -31,3 +37,3 @@ * get localstorage value for key falling back to in memory for iOS private browsing bug | ||
*/ | ||
exports.get = function(key) { | ||
Superstore.prototype.get = function(key) { | ||
if (arguments.length !== 1) { | ||
@@ -39,3 +45,3 @@ throw Error("get expects 1 argument, " + arguments.length + " given; " + key); | ||
try { | ||
data = localStorage[key]; | ||
data = this.storage[key]; | ||
} catch(e) { | ||
@@ -62,3 +68,3 @@ persist = false; // Safari 8 with Cookies set to 'Never' throws on every read | ||
*/ | ||
exports.set = function(key, value) { | ||
Superstore.prototype.set = function(key, value) { | ||
if (arguments.length !== 2) { | ||
@@ -69,3 +75,3 @@ throw Error("set expects 2 arguments, " + arguments.length + " given; " + key); | ||
try { | ||
localStorage[key] = JSON.stringify(value); | ||
this.storage[key] = JSON.stringify(value); | ||
} catch(err) { | ||
@@ -92,6 +98,6 @@ | ||
*/ | ||
exports.unset = function(key) { | ||
Superstore.prototype.unset = function(key) { | ||
delete store[key]; | ||
delete keys[key]; | ||
localStorage.removeItem(key); | ||
this.storage.removeItem(key); | ||
}; | ||
@@ -105,6 +111,6 @@ | ||
*/ | ||
exports.clear = function(clearPrefix) { | ||
Superstore.prototype.clear = function(clearPrefix) { | ||
if (!clearPrefix) { | ||
if (persist) { | ||
localStorage.clear(); | ||
this.storage.clear(); | ||
} | ||
@@ -124,1 +130,4 @@ store = {}; | ||
}; | ||
module.exports.local = new Superstore('localStorage'); | ||
module.exports.session = new Superstore('sessionStorage'); |
{ | ||
"name": "superstore-sync", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Local storage, without the bugs.", | ||
@@ -30,10 +30,6 @@ "main": "lib/superstore-sync.js", | ||
"keywords": [ | ||
"localstorage" | ||
"localstorage", | ||
"sessionstorage" | ||
], | ||
"author": "Matt Andrews <matthew.andrews@ft.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"setimmediate": "~1.0.1", | ||
"escape-regexp-component": "~1.0" | ||
} | ||
"license": "MIT" | ||
} |
# superstore-sync [](https://travis-ci.org/matthew-andrews/superstore-sync) [](http://badge.fury.io/js/superstore-sync) | ||
Superstore is a simple lightweight synchronous wrapper around localStorage. Its features include: | ||
Superstore is a simple lightweight synchronous wrapper around the Web Storage APIs [localStorage](https://developer.mozilla.org/en/docs/Web/API/Window/localStorage) and [sessionStorage](https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage). Its features include: | ||
@@ -8,2 +8,15 @@ - It is [resilient to iOS's strange behaviour in private browsing mode](http://stackoverflow.com/questions/14555347/html5-localstorage-doesnt-works-in-ios-safari-private-browsing). | ||
If you require an asyncronous version please use [superstore](https://github.com/matthew-andrews/superstore) instead. | ||
## install | ||
### NPM | ||
``` | ||
npm install superstore-sync --save | ||
``` | ||
### bower | ||
``` | ||
bower install superstore-sync --save | ||
``` | ||
## api | ||
@@ -13,12 +26,35 @@ | ||
### #get(key) | ||
## local | ||
### #set(key, value) | ||
### #local.get(key) | ||
### #unset(key) | ||
### #local.set(key, value) | ||
### #clear(prefix) | ||
### #local.unset(key) | ||
### #local.clear(prefix) | ||
## session | ||
### #session.get(key) | ||
### #session.set(key, value) | ||
### #session.unset(key) | ||
### #session.clear(prefix) | ||
## usage | ||
```javascript | ||
var store = require('superstore-sync'); | ||
//Persist a value to local storage | ||
var value = store.local.set('foo', 'bar'); | ||
//Get a value from session storage | ||
var session = store.session.get('baz'); | ||
``` | ||
## todo | ||
- JSDoc comments and automatically generating documentation. |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
8129
21.11%0
-100%8
14.29%163
21.64%59
156.52%3
50%1
Infinity%- Removed
- Removed
- Removed
- Removed