simple-storage
Advanced tools
Comparing version 3.0.0 to 3.1.0
export declare type SimpleStorageType = "session" | "local"; | ||
export interface StringDictionary { | ||
[key: string]: string; | ||
} | ||
export declare class SimpleStorage { | ||
@@ -8,5 +11,9 @@ private storageSource; | ||
removeItem(key: string): void; | ||
/** Remove all items from storage */ | ||
clear(): void; | ||
readonly length: number; | ||
getAllItems(): any; | ||
getAllItemsAsync(): Promise<any>; | ||
} | ||
export declare const simpleSessionStorage: SimpleStorage; | ||
export declare const simpleLocalStorage: SimpleStorage; |
"use strict"; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
; | ||
/** Fallback storage provider for environments where localStorage isn't available */ | ||
/** Fallback storage provider for environments where the Storage API isn't available */ | ||
var AltStorage = /** @class */ (function () { | ||
@@ -21,2 +21,15 @@ function AltStorage() { | ||
}; | ||
AltStorage.prototype.getData = function () { | ||
return this.data; | ||
}; | ||
AltStorage.prototype.key = function (index) { | ||
return Object.keys(this.data)[index]; | ||
}; | ||
Object.defineProperty(AltStorage.prototype, "length", { | ||
get: function () { | ||
return Object.keys(this.data).length; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return AltStorage; | ||
@@ -46,6 +59,11 @@ }()); | ||
var value = this.storageSource.getItem(key); | ||
if (value) { | ||
if (typeof value !== "string") { | ||
return value; | ||
} | ||
try { | ||
return JSON.parse(value); | ||
} | ||
return value; | ||
catch (error) { | ||
return value; | ||
} | ||
}; | ||
@@ -55,5 +73,27 @@ SimpleStorage.prototype.removeItem = function (key) { | ||
}; | ||
/** Remove all items from storage */ | ||
SimpleStorage.prototype.clear = function () { | ||
return this.storageSource.clear(); | ||
}; | ||
Object.defineProperty(SimpleStorage.prototype, "length", { | ||
get: function () { | ||
return this.storageSource.length; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
SimpleStorage.prototype.getAllItems = function () { | ||
var data = {}; | ||
for (var i = this.length - 1; i >= 0; i--) { | ||
var key = this.storageSource.key(i); | ||
if (key) { | ||
data[key] = this.getItem(key); | ||
} | ||
} | ||
return data; | ||
}; | ||
SimpleStorage.prototype.getAllItemsAsync = function () { | ||
var _this = this; | ||
return new Promise(function (resolve) { return setTimeout(function () { return resolve(_this.getAllItems()); }); }); | ||
}; | ||
return SimpleStorage; | ||
@@ -60,0 +100,0 @@ }()); |
{ | ||
"name": "simple-storage", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Simple localStorage / sessionStorage supporting objects and arrays", | ||
@@ -21,5 +21,2 @@ "homepage": "https://github.com/tuxracer/simple-storage", | ||
"types": "./dist/storage.d.ts", | ||
"scripts": { | ||
"prepublish": "tsc" | ||
}, | ||
"dependencies": {}, | ||
@@ -26,0 +23,0 @@ "keywords": [ |
@@ -1,10 +0,15 @@ | ||
sessionStorage | ||
# simple-storage | ||
Store strings and objects to local or session storage. Falls back to storing data in memory if run on platforms where the [Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Storage) is unavailable (such as node). | ||
## sessionStorage | ||
Store items for current session | ||
```javascript | ||
import { simpleSessionStorage } from "simple-storage"; | ||
// set items | ||
// set item | ||
simpleSessionStorage.setItem("pets", { | ||
dogs: 3, | ||
cats: 1 | ||
cats: 1, | ||
}); | ||
@@ -16,11 +21,19 @@ | ||
// get all items | ||
const items = simpleSessionStorage.getAllItems(); | ||
console.log(items); // { pets: {dogs: 3, cats: 1} } | ||
// get all items async | ||
const i = await simpleSessionStorage.getAllItemsAsync(); | ||
console.log(i); // { pets: {dogs: 3, cats: 1} } | ||
// remove item | ||
simpleSessionStorage.removeItem("pets"); | ||
console.log(pets); // undefined | ||
// clear all | ||
// remove all items | ||
simpleSessionStorage.clear(); | ||
``` | ||
localStorage | ||
## localStorage | ||
Store items for longer than current session when possible | ||
@@ -30,6 +43,6 @@ ```javascript | ||
// set items | ||
// set item | ||
simpleLocalStorage.setItem("pets", { | ||
dogs: 3, | ||
cats: 1 | ||
cats: 1, | ||
}); | ||
@@ -41,7 +54,14 @@ | ||
// get all items | ||
const items = simpleLocalStorage.getAllItems(); | ||
console.log(items); // { pets: {dogs: 3, cats: 1} } | ||
// get all items async | ||
const i = await simpleLocalStorage.getAllItemsAsync(); | ||
console.log(i); // { pets: {dogs: 3, cats: 1} } | ||
// remove item | ||
simpleLocalStorage.removeItem("pets"); | ||
console.log(pets); // undefined | ||
// clear all | ||
// remove all items | ||
simpleLocalStorage.clear(); | ||
@@ -48,0 +68,0 @@ ``` |
export type SimpleStorageType = "session" | "local"; | ||
interface StringDictionary { | ||
export interface StringDictionary { | ||
[key: string]: string; | ||
}; | ||
/** Fallback storage provider for environments where localStorage isn't available */ | ||
/** Fallback storage provider for environments where the Storage API isn't available */ | ||
class AltStorage { | ||
@@ -26,2 +26,14 @@ private data: StringDictionary = {} | ||
} | ||
getData() { | ||
return this.data; | ||
} | ||
key(index: number) { | ||
return Object.keys(this.data)[index]; | ||
} | ||
get length() { | ||
return Object.keys(this.data).length; | ||
} | ||
}; | ||
@@ -58,6 +70,12 @@ | ||
const value = this.storageSource.getItem(key); | ||
if (value) { | ||
if (typeof value !== "string") { | ||
return value; | ||
} | ||
try { | ||
return JSON.parse(value); | ||
} catch (error) { | ||
return value; | ||
} | ||
return value; | ||
} | ||
@@ -69,5 +87,28 @@ | ||
/** Remove all items from storage */ | ||
clear() { | ||
return this.storageSource.clear(); | ||
} | ||
get length() { | ||
return this.storageSource.length; | ||
} | ||
getAllItems() { | ||
const data: any = {}; | ||
for(let i = this.length - 1; i >= 0; i--) { | ||
const key = this.storageSource.key(i); | ||
if (key) { | ||
data[key] = this.getItem(key); | ||
} | ||
} | ||
return data; | ||
} | ||
getAllItemsAsync(): Promise<any> { | ||
return new Promise((resolve) => setTimeout(() => resolve(this.getAllItems()))); | ||
} | ||
}; | ||
@@ -74,0 +115,0 @@ |
@@ -5,2 +5,4 @@ { | ||
"outDir": "./dist", | ||
"target": "es5", | ||
"lib": ["es2015", "dom"], | ||
"declaration": true, | ||
@@ -7,0 +9,0 @@ "strictNullChecks": true, |
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
10253
222
72
0