1.1.1 • Published 2 years ago

local-storage-superjson v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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%"
// }]
1.1.1

2 years ago

1.1.0

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago