npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serde-storage

v0.1.2

Published

SerdeStorage is a TypeScript utility package designed to provide typed access to storage systems, such as `localStorage` in web browsers or any other custom storage system that follows the `Storage` interface. It allows for serialization and deserializati

Downloads

6

Readme

SerdeStorage

SerdeStorage is a TypeScript utility package designed to provide typed access to storage systems, such as localStorage in web browsers or any other custom storage system that follows the Storage interface. It allows for serialization and deserialization (hence "Serde") of structured data while maintaining type safety.

Features

  • Type-safe storage operations: Get, set, and remove items from storage with full TypeScript type safety.
  • Nested property support: Access nested properties within your storage objects using key paths (e.g., "user.profile.name").
  • Custom storage systems: Works with any storage system that implements the Storage interface, making it flexible for various use cases.

Limitations

Array Support

While SerdeStorage provides robust support for nested objects, it has limited support for arrays. Specifically, SerdeStorage does not support accessing array elements directly through key paths (e.g., foo.bar[0]). Arrays can be stored and retrieved as whole entities, but manipulating individual elements within an array via key paths is not supported.

Data Structure Validation

SerdeStorage does not perform validation on the structure of the data already present in localStorage or any other storage system used. This means that if the data in the storage does not match the expected schema or type map provided to SerdeStorage, type inconsistencies or unexpected behavior might occur. It is the consumer's responsibility to ensure that the stored data's structure aligns with the schema expected by the application.

Installation

npm install serde-storage

Or if you prefer using yarn:

yarn add serde-storage

Usage

To use SerdeStorage, first define the structure (schema) of your storage data. Then, create an instance of SerdeStorage with your storage system and the schema.

import { SerdeStorage } from "serde-storage";

// Define your storage schema
const schema = {
    user: {
        name: String,
        age: Number,
    },
    settings: {
        darkMode: Boolean,
    },
};

// Initialize SerdeStorage with localStorage (or any Storage-compatible system)
const storage = new SerdeStorage(localStorage, schema);

// Set items in storage with type safety
storage.setItem("user", { name: "Jane Doe", age: 30 });
storage.setItem("settings.darkMode", true);

// Get items from storage with type safety
const user = storage.getItem("user"); // { name: 'Jane Doe', age: 30 }
const darkMode = storage.getItem("settings.darkMode"); // true

// Remove items from storage
storage.removeItem("user");

API Reference

SerdeStorage<S, T>

Generic class to create a typed storage instance.

  • S: The storage system implementing the Storage interface.
  • T: The schema type representing the structure of your storage data.

constructor(storage: S, schema: T)

  • storage: An instance of the storage system.
  • schema: A schema representing the structure and types of the data to be stored.

setItem<K>(key: K, value: TypeAtPathFor<_T, K>): void

Sets a value for the given key in the storage.

  • key: The key or key path where the value should be stored.
  • value: The value to store, which must match the type defined in the schema for the given key.

getItem<K>(key: K): TypeAtPathFor<_T, K> | null

Retrieves a value from the storage by key.

  • key: The key or key path of the value to retrieve.

Returns the value if found, or null if not found or the key is invalid.

removeItem<K>(key: K): void

Removes a value from the storage by key.

  • key: The key or key path of the value to remove.

Contributing

Contributions are welcome! Please submit pull requests with any enhancements, bug fixes, or documentation improvements.

License

SerdeStorage is released under the MIT License.

Tests

SerdeStorage includes a comprehensive test suite to ensure reliability and type safety. Refer to the tests directory in the repository for test implementations and usage examples.