0.0.1 • Published 5 years ago

browser-storage-parser v0.0.1

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

browser-storage-parser

Provides a helper to save to and retrieve objects and strings from the browser's sessionStorage and localStorage.

Motivation

When a application makes extensive use of browser storage, several calls are made to storage getItem and setItem. This is fine, until there is a need to store more than a simple string. For example, if a JSON object needs to be stored, it must be stringified upon setItem and then parsed upon getItem. This library takes care of that for you.

There are also situation where localStorage may not be available, this library allows for using sessionStorage as a fallback in these scenarios.

Usage

saveToSessionStorage

No matter the object type of the input, it will store as a string in sessionStorage. This prevents the need for sessionStorage.setItem("objectName", JSON.stringify({ key: "value" }))

Examples:

saveToSessionStorage("objectName", "string storage")

-- or --

saveToSessionStorage("objectName", {key: "value"})

getFromSessionStorage

Will attempt to parse the item being retrieved and will either return as a string or object. This prevents the need for JSON.parse(sessionStorage.getItem("objectName") and takes care of null checking. Example:

getFromSessionStorage('objectName`)

Values returned: "string stored" -- or --

{
  key: "value",
  key2: "value2"
}

saveToLocalStorage

No matter the object type of the input, it will store as a string in localStorage. This prevents the need for localStorage.setItem("objectName", JSON.stringify({ key: "value" }))

Examples:

saveToLocalStorage("objectName", "string storage")

-- or --

saveToLocalStorage("objectName", {key: "value"})

Optional third parameter: sessionFallback If localStorage is unavailable, sessionStorage can be used as a fallback. Defaults to false, Example:

saveToLocalStorage("objectName", {key: "value"}, true)

getFromLocalStorage

Will attempt to parse the item being retrieved and will either return as a string or object. This prevents the need for JSON.parse(localStorage.getItem("objectName") and takes care of null checking. Example:

getFromLocalStorage('objectName`)

Values returned: "string stored" -- or --

{
  key: "value",
  key2: "value2"
}

Optional third parameter: sessionFallback If localStorage is unavailable, sessionStorage can be used as a fallback. Defaults to false, Example:

getFromLocalStorage("objectName", true)