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 🙏

© 2025 – Pkg Stats / Ryan Hefner

o2s2o

v1.0.0

Published

o2s2o: Object → Storage (JSON) → back to Object. Safe, readable de/serializer with class revival and built-ins.

Readme

o2s2o

Object → Storage (JSON) → back to Object.
A tiny, readable de/serializer that dehydrates class instances into JSON-safe data and hydrates them back to real instances — with support for built-ins (Date, URL, RegExp (flags kept), Map, Set, Error, BigInt).

Why?

  • Keep your class methods after persisting to localStorage, DB, or sending over the wire.
  • No magic: uses explicit envelopes (handlerId, ctorName, data) for clarity.
  • Deterministic revival across ESM/CJS/minified builds using a registry or a ctorMap.

Install

npm i o2s2o
# or
yarn add o2s2o
# or
pnpm add o2s2o

Quick start

import Serializer from 'o2s2o';
import type { AutoHandler } from 'o2s2o';

const S = new Serializer();

class Point {
  constructor(public x: number, public y: number) {}
  len() { return Math.hypot(this.x, this.y); }
}

// Register your classes (version ids recommended)
S.register<Point>({ id: 'Point@1', ctor: Point });

const state = { p: new Point(3,4), when: new Date('2024-01-02T03:04:05Z') };

// Save
const json = S.stringify(state);
localStorage.setItem('app', json);

// Restore
const restored = S.parse<typeof state>(localStorage.getItem('app')!, {
  ctorMap: { Point }   // deterministic mapping by constructor name
});

restored.p instanceof Point;   // true
restored.p.len();              // 5
restored.when instanceof Date; // true

API

new Serializer()

register<T>(handler: AutoHandler<T>)

Registers a class-type for auto dehydration/hydration.

  • id: stable string, versioned like "Point@1"
  • ctor: the class constructor
  • keys?: (instance) => string[] — which own keys to persist (default: Object.keys(instance))
  • construct?: (plain) => T — custom builder on hydration (default: create a blank object with the prototype and Object.assign the hydrated fields)

dehydrateAny(value: any): any

Recursively converts any value to a JSON-safe shape. Class instances become envelopes:

{ handlerId: 'Point@1', data: { x: 3, y: 4 } }

Non-registered classes become:

{ ctorName: 'ClassName', data: { ... } }

Built-ins are handled out of the box.

hydrateAny(value: any, opts?: HydrationOptions): any

Rebuilds values back to live instances. Options:

type HydrationOptions = {
  ctorMap?: Record<string, Constructor>; // deterministic ctor name -> ctor
  coerceScalars?: boolean;               // turns "true","42","null" -> proper types (off by default)
  allowGlobalLookup?: boolean;           // last-ditch globalThis lookup (off by default)
};

stringify(obj: any): string / parse<T>(text: string, opts?: HydrationOptions): T

Friendly helpers around JSON.stringify/JSON.parse + (de)hydrate.

Built-ins supported

  • Date ↔ ISO string
  • URL ↔ string
  • RegExp{source, flags}
  • Map ↔ array of [key, value] (both sides recursively processed)
  • Set ↔ array of values
  • Error{name, message, stack}
  • BigInt ↔ string

Patterns

Version your handlers

S.register<Point>({ id: 'Point@2', ctor: Point, construct: (p) => new Point(p.x, p.y) });

Enforce invariants

class Money { constructor(public cents: number, public currency: 'USD'|'EUR') { if (!Number.isInteger(cents)) throw new Error('int'); } }
S.register<Money>({ id: 'Money@1', ctor: Money, keys: m => ['cents','currency'], construct: p => new Money(p.cents, p.currency) });

Deterministic hydration across bundles

const restored = S.parse(json, { ctorMap: { Point, Money } });

FAQ

Q: Does it support circular references?
No. JSON doesn’t either. You can layer an ID-based cycle encoder if needed.

Q: What about functions / symbols / undefined?
Same as JSON: they are dropped.

Q: Do I have to register built-ins?
No. They are included out of the box.

License

MIT © Alireza Tabatabaeian