Skip to content

dsacramone/local-storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

local-storage

A small utility to use local or session storage to store/retrieve data across your app. The goal of this script is to just provide a very simple api to add/delete/get values from either local or session storage. The idea here is you will get a unique "table of sorts" for your usage. And all storage goes in this 'table/object'. There are no bells n' whistles, as most of that is cruft and not used :-)

API

A few methods added to the Storage obj prototype. Instantiate a new Storage object, and now you have availability to these methods on your new object.

Object Instatiation / Usages

  • Constructor can take 1 required, 1 optional args: @string.isReqired name, @string type
  • name: Must be supplied to your instantiation. An arbitrary string to denote uniqueness.
  • type: defaults to "sessionStorage" if neither "localStorage" or "sessionStorage" is passed.
With 'name' only.

    var Storage = new Storage('Storage') // remember, you could have named it "db__090989" or "somewhereInTime", arbitrary.
  
With 'name' and 'type'.

    var Storage = new Storage('Storage', 'localStorage')
  
## Available Attributes

    Storage.storageAvailable // @boolean : return for browser support
    Storage.storageType // @string : are you using "localStorage" or "sessionStorage"

Available Methods

Storage.setStorageValue(@array[@object])

  • This method takes an array of objects key:value pairs as you can set more than one value at once.

examples


  Storage.setStorageValue([{
    id: 'uyxloyx__nt'
  }]);

Storage.setStorageValue([{ id: 'uyxloyx__nt', color: 'blue', year: 2015, age: 100 }]);

Storage.getStorageValue(@string or @array)

  • You have two options here
  • Pass in your request args via strings, or as an array
  • @string or @array

Examples

A single @string or an @array length of 1, request, will return the value.


Storage.getStorageValue('color');
# returns @string blue

A multi @string or an @array length > 1, request, will return an object of key/value pairs


Storage.getStorageValue('color', 'year', 'age');
# returns @object {color: 'blue', year: 2015, 'age': 100 }

Storage.getStorageValue(['id','year']);
# returns @object {id: 'uyxloyx__nt', age: 100}

Storage.getStorageDB()

returns an @object of all stored items within the instance of the created storage api

Storage.removeStorageValue(@string or @array)

  • removes keys from the storage. The method takes either a @string or an @array
  • returns the new storage db sans the removed items

Examples


Storage.removeStorageValue('id');
# returns @object {color: 'blue' , year: 2015, age: 100}

Storage.removeStorageValue('id', 'color', 'year');

returns @object {age: 100}

Storage.removeStorageValue(['id', 'color', 'year']);

returns @object {age: 100}

Storage.getStorageLength()

  • Returns the length of the object. Does not count nested items. Top level only.

Examples


Storage.getStorageLength();
# returns 3

Storage.clearStorageDB()

Clears your storage db of values, but otherwise keeps it available for additional updates if needed.

Storage.deleteStorageDB()

Deletes your storage db. It is now gone. Keep in mind your object instance is still around.

TODO:

tests. coming soon.

About

A small utility to use localStorage or sessionStorage storage to store/retrieve data across your app.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published