local-storage-superjson
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Local Storage superjson

This package includes some CRUD utilities for saving superjson serializable objects into LocalStorage in a type safe manner.

Exposed functions

// localStorageJson.ts
function getObject<T>(key: string): T | null
function setObject<T>(key: string, object: T): void
function updateObject<T>(key: string, handleUpdate: (previousObject: T) => T): void
function upsertObject<T>(key: string, valueIfMissing: T, handleUpdate: (previousObject: T) => T): void

Sample usage

import localStorageJson from "local-storage-superjson";
// or import {getObject, setObject, ...} from "local-storage-superjson";

type Score = {points: number, accuracy: string}

// Stores the given object into localStorage with help of superjson.stringify.
localStorageJson.setObject<Score[]>("scores", [{
  points: 85,
  accuracy: "65%"
}]);

// You can then read this object with getObject
const scores = localStorageJson.getObject<Score[]>("scores");
// [{
//   points: 85,
//   accuracy: "65%"
// }]

// You can also update an existing object with updateObject.
// This will however throw an Error if the key does not exist.
const currentScore = {
  points: 50,
  accuracy: "100%"
};
localStorageJson.updateObject<Score[]>("scores", previousScores => [
  ...previousScores,
  currentScore
]);

// upsertObject can be used instead if you're not sure if the key exists or not.
// If the given key is not in localStorage it will set the given valueIfMissing.
localStorageJson.upsertObject<Score[]>("easy-scores", /*valueIfMissing:*/ [currentScore], previousScores => [
  ...previousScores,
  currentScore
]);

// When the key already exists the handleUpdate function will be called to
// provide a new object based on the previousObject.
localStorageJson.upsertObject<Score[]>("easy-scores", [currentScore], /*handleUpdate:*/ previousScores => [
  ...previousScores,
  currentScore
]);
// [{
//   points: 50,
//   accuracy: "100%"
// }, {
//   points: 50,
//   accuracy: "100%"
// }]

Readme

Keywords

none

Package Sidebar

Install

npm i local-storage-superjson

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

9.26 kB

Total Files

7

Last publish

Collaborators

  • cymantex