simple-storage
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -14,6 +14,6 @@ export declare type SimpleStorageType = "session" | "local"; | ||
readonly length: number; | ||
getAllItems(): any; | ||
getAllItemsAsync(): Promise<any>; | ||
getAllItems(): any[]; | ||
getAllItemsAsync(): Promise<any[]>; | ||
} | ||
export declare const simpleSessionStorage: SimpleStorage; | ||
export declare const simpleLocalStorage: SimpleStorage; |
@@ -53,3 +53,3 @@ "use strict"; | ||
SimpleStorage.prototype.setItem = function (key, rawValue) { | ||
var value = JSON.stringify(rawValue); | ||
var value = typeof rawValue === "string" ? rawValue : JSON.stringify(rawValue); | ||
this.storageSource.setItem(key, value); | ||
@@ -84,10 +84,12 @@ }; | ||
SimpleStorage.prototype.getAllItems = function () { | ||
var data = {}; | ||
var items = []; | ||
for (var i = this.length - 1; i >= 0; i--) { | ||
var item = {}; | ||
var key = this.storageSource.key(i); | ||
if (key) { | ||
data[key] = this.getItem(key); | ||
if (key !== null) { | ||
item[key] = this.getItem(key); | ||
items.push(item); | ||
} | ||
} | ||
return data; | ||
return items; | ||
}; | ||
@@ -94,0 +96,0 @@ SimpleStorage.prototype.getAllItemsAsync = function () { |
{ | ||
"name": "simple-storage", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Simple localStorage / sessionStorage supporting objects and arrays", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/tuxracer/simple-storage", |
@@ -23,7 +23,7 @@ # simple-storage | ||
const items = simpleSessionStorage.getAllItems(); | ||
console.log(items); // { pets: {dogs: 3, cats: 1} } | ||
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} } | ||
console.log(i); // [{ pets: {dogs: 3, cats: 1} }] | ||
@@ -55,7 +55,7 @@ // remove item | ||
const items = simpleLocalStorage.getAllItems(); | ||
console.log(items); // { pets: {dogs: 3, cats: 1} } | ||
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} } | ||
console.log(i); // [{ pets: {dogs: 3, cats: 1} }] | ||
@@ -62,0 +62,0 @@ // remove item |
@@ -63,3 +63,3 @@ export type SimpleStorageType = "session" | "local"; | ||
setItem(key: string, rawValue: any) { | ||
const value = JSON.stringify(rawValue); | ||
const value = typeof rawValue === "string" ? rawValue : JSON.stringify(rawValue); | ||
this.storageSource.setItem(key, value); | ||
@@ -96,16 +96,18 @@ } | ||
getAllItems() { | ||
const data: any = {}; | ||
const items: any[] = []; | ||
for(let i = this.length - 1; i >= 0; i--) { | ||
const item: any = {}; | ||
const key = this.storageSource.key(i); | ||
if (key) { | ||
data[key] = this.getItem(key); | ||
if (key !== null) { | ||
item[key] = this.getItem(key); | ||
items.push(item); | ||
} | ||
} | ||
return data; | ||
return items; | ||
} | ||
getAllItemsAsync(): Promise<any> { | ||
getAllItemsAsync(): Promise<any[]> { | ||
return new Promise((resolve) => setTimeout(() => resolve(this.getAllItems()))); | ||
@@ -112,0 +114,0 @@ } |
10490
226