Comparing version 0.0.3 to 0.0.4
53
index.js
'use strict'; | ||
module.exports = function PetShop({ storage, namespace }) { | ||
module.exports = function PetShop({ storage, namespace, json = false }) { | ||
return Object.defineProperties( | ||
@@ -10,19 +10,36 @@ {}, | ||
enumerable: true, | ||
value(key) { | ||
return storage.getItem(`${namespace}.${key}`) || undefined; | ||
} | ||
value: json | ||
? function get(key) { | ||
const raw = storage.getItem(`${namespace}.${key}`); | ||
return raw !== null ? JSON.parse(raw) : undefined; | ||
} | ||
: function get(key) { | ||
const raw = storage.getItem(`${namespace}.${key}`); | ||
return raw !== null ? raw : undefined; | ||
} | ||
}, | ||
set: { | ||
enumerable: true, | ||
value(key, value) { | ||
if (value === undefined) { | ||
this.remove(key); | ||
} else { | ||
storage.setItem(`${namespace}.${key}`, value); | ||
value: json | ||
? function set(key, value) { | ||
if (value === undefined) { | ||
this.remove(key); | ||
} else { | ||
storage.setItem(`${namespace}.${key}`, JSON.stringify(value)); | ||
} | ||
} | ||
} | ||
: function set(key, value) { | ||
if (value === undefined || value === null) { | ||
this.remove(key); | ||
} else { | ||
if (typeof value !== 'string') { | ||
throw new TypeError('The Value should be a string'); | ||
} | ||
storage.setItem(`${namespace}.${key}`, value); | ||
} | ||
} | ||
}, | ||
remove: { | ||
enumerable: true, | ||
value(key) { | ||
value: function remove(key) { | ||
storage.removeItem(`${namespace}.${key}`); | ||
@@ -58,3 +75,3 @@ } | ||
enumerable: true, | ||
value() { | ||
value: function valueOf() { | ||
return this.keys.reduce( | ||
@@ -66,8 +83,2 @@ (io, key) => ({ [key]: this.get(key), ...io }), | ||
}, | ||
stringify: { | ||
enumerable: true, | ||
value(replacer, space) { | ||
return JSON.stringify(this.valueOf(), replacer, space); | ||
} | ||
}, | ||
size: { | ||
@@ -81,4 +92,4 @@ enumerable: true, | ||
enumerable: true, | ||
value(key) { | ||
return this.get(key) !== undefined; | ||
value: function has(key) { | ||
return storage.getItem(`${namespace}.${key}`) !== null; | ||
} | ||
@@ -88,3 +99,3 @@ }, | ||
enumerable: true, | ||
value() { | ||
value: function clear() { | ||
this.rawKeys.forEach(key => { | ||
@@ -91,0 +102,0 @@ storage.removeItem(key); |
{ | ||
"name": "pet-shop", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A simple wrapper of Web Storage API", | ||
@@ -14,2 +14,4 @@ "license": "MIT", | ||
"data", | ||
"json", | ||
"key-value", | ||
"localStorage", | ||
@@ -16,0 +18,0 @@ "sessionStorage", |
@@ -24,2 +24,3 @@ # pet-shop | ||
```js | ||
// plain-text store | ||
const store = PetShop({ | ||
@@ -32,3 +33,3 @@ namespace: 'pet-shop', | ||
store.get('abc'); // output: 'xyz' | ||
store.size; // output: 0 | ||
store.size; // output: 1 | ||
localStorage.getItem('pet-shop.abc'); // output: 'xyz' | ||
@@ -40,1 +41,19 @@ | ||
``` | ||
```js | ||
// json store | ||
const store = PetShop({ | ||
namespace: 'pet-shop', | ||
storage: localStorage, | ||
json: true | ||
}); | ||
store.set('abc', ['xyz']); | ||
store.get('abc'); // output: ['xyz'] | ||
store.size; // output: 1 | ||
localStorage.getItem('pet-shop.abc'); // output: ['xyz'] | ||
store.remove('abc'); | ||
store.has('abc'); // output: false | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4516
102
57