Nathan Thomas Nathan Thomas

Cargo for your various luggage

Fetching, setting and deleting data in localStorage with an expressive syntax.

Local storage

By default, localStorage will be used when creating a new instance:

let user = new Cargo("user");

Session storage

To use sessionStorage, which exists only within the pages session, just pass sessionStorage as a second argument when creating a new instance:

let user = new Cargo("user", sessionStorage);

set()

The set method takes two parameters. The first parameter can be a string representing a specific key or a dot seperated list of keys. The second parameter is the value that is stored:

user.set("profile.name", {
    first: "Johnny",
    last: "Appleseed"
});

// {user: {profile: {name: {first: "Johnny", last: "Appleseed"}}}}

get()

The get method takes a single parameter. The value can be a string representing a specific key or a dot seperated list of keys. If no match is found, undefined is returned.

user.get("profile.name.first");

// "Johnny"

delete()

The delete method takes a single parameter. The value can be a string representing a specific key or a dot seperated list of keys:

user.delete("profile.name.least");

// {user: {profile: {name: {first: "Johnny"}}}}

destroy()

The destroy method destroys all data stored at the specified key, essentially erasing everything under the instance:

user.destroy();

// undefined