interface StoreBuddy<T> {
    clear: (() => void);
    load: (() => IsEmptyObject<T>);
    reset: (() => void);
    save: ((data: IsEmptyObject<T>) => void);
}

Type Parameters

  • T

Properties

Properties

clear: (() => void)

Remove all data set using this instance.

import storeBuddy from "store-buddy";

const storage = storeBuddy("foo").init("bar");
storage.load(); // returns "bar"
storage.clear();
storage.load(); // throws an error (no data exists)
load: (() => IsEmptyObject<T>)

Retrieve the current data set using this instance.

Type declaration

    • (): IsEmptyObject<T>
    • Returns IsEmptyObject<T>

      The data from localStorage or sessionStorage, throwing an error if no data is found.

import storeBuddy from "store-buddy";

const storage1 = storeBuddy("foo").init("bar");
storage1.load(); // returns "bar"
reset: (() => void)

Reset the data back to its original value set with the init method.

import storeBuddy from "store-buddy";

const storage = storeBuddy("foo").init("bar");
storage.save("hello");
storage.reset();
storage.load(); // returns "bar"
save: ((data: IsEmptyObject<T>) => void)

Set new data or overwrite old data in localStorage or sessionStorage. For TS developers, saving data of a different type to the one specified in the type parameter is prevented.

Type declaration

    • (data): void
    • Parameters

      • data: IsEmptyObject<T>

        The data to save to localStorage or sessionStorage.

      Returns void

import storeBuddy from "store-buddy";

// Saves the string "bar" to the localStorage entry with the key "foo"
const storage1 = storeBuddy("foo").init("bar");

// Overwrites that same string with different data. Note that, without
// specifying a specific type when initialising, there is no type safety
// provided for TypeScript developers
storage1.save("baz");
storage1.save(123);

// This is type-safe...
const storage2 = storeBuddy<number>("foo").init(123);

// ...so this works...
storage2.save(456);

// ...and this does not work :)
storage2.save("I am not a number");