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

flatbed

v0.1.1

Published

Advanced JavaScript object serializer - packs objects into a flat, JSON-friendly structure, keeping track of circular references and object constructors

Downloads

15

Readme

flatbed

Advanced JavaScript object serializer written in TypeScript. Packs objects into a flat, JSON-friendly structure, keeping track of circular references and object constructors.

Table of contents

Installation

For Node.js and bundlers (Webpack, Rollup, etc)

npm i flatbed
# or
yarn add flatbed

For browsers (script tag)

Browser builds aren't available yet, but they are planned for the future.

Release notes

See information about breaking changes and release notes here.

Usage

Flatbed provides serialize and deserialize function that convert objects to and from a flat JSON-friendly structure.

It also provides convenience stringify and parse functions that directly turn serialized objects to strings and viceversa, and can be used for the majority of use cases.

import { stringify, parse } from 'flatbed';

const foo = { hello: 'world!', circularReference: [] };
const root = { listOfThings: [foo] };
foo.circularReference = root.listOfThings;

const json = stringify(root);

// JSON can be transferred to disk, memory, network, etc.

const deserialized = parse(json);
console.log(`Hello ${deserialized.listOfThings[0].circularReference[0].hello}`);
// Outputs: "Hello world!"

API documentation

The most up-to-date documentation for this package is automatically generated from code and available at https://danielegarciav.github.io/flatbed/.

Brief simplified API summary

serialize(obj: object): object

Serializes the given object into a flat JSON-friendly structure.

deserialize(obj: object, classes?: class[]): object

Deserializes a previously serialized object into its original state. An array can be supplied with the classes/constructors of the serialized objects.

stringify(obj: object): string

Serializes the given object and stringifies it into JSON.

parse(json: string, classes?: class[]): object

Parses the given JSON string as a serialized object, deserializes it, and returns the object in its original state. An array can be supplied with the classes/constructors of the serialized objects.

Built-in classes

Flatbed already knows how to construct the following built-in JS objects and won't require you to pass them in:

  • Object
  • Array
  • Map
  • Set

Deeper example

Flatbed will generate structures that keep track of the constructors of the serialized objects. When deserializing objects of custom classes, you must pass these constructors to deserialize.

This example is written in TypeScript for clarity, but works the same in plain JavaScript.

import { stringify, parse } from 'flatbed';

class Player {
  x = 0;
  y = 0;
  name: string;
  target?: Player;

  constructor(name: string) {
    this.name = name;
  }

  setTarget(t: Player) {
    this.target = t;
  }

  sayTarget() {
    this.target && console.log(`My target is ${this.target.name}`);
  }
}

class World {
  players = new Set<Player>();

  logPlayers() {
    console.log(`Players: ${[...this.players].map(p => p.name).join(', ')}`);
  }
}

const world = new World();
const player1 = new Player('Alice');
const player2 = new Player('Bob');
player1.setTarget(player2);
player2.setTarget(player1);
world.players.add(player1);
world.players.add(player2);

const json = stringify(serialized);

// JSON can be transferred to disk, memory, network, etc.

const deserialized = parse<World>(json, [World, Player]);

deserialized instanceof World; // true
deserialized.logPlayers(); // "Players: Alice, Bob"

deserialized.players instanceof Set; // true
[...deserialized.players][0] instanceof Player; // true
[...deserialized.players][0].sayTarget(); // "My target is Bob"

Development and contributions

Check package.json to find scripts related to installing dependencies, building, testing, linting and generating documentation. I am open to new issues and pull requests!

License

MIT