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

cyclestream

v0.0.1

Published

Decycle/retrocycle for objects/arrays in an ongoing sequence

Downloads

7

Readme

Decycle/retrocycle for objects/arrays in an ongoing sequence

Convert objects which might refer to each other into a sequence of instructions for reconstructing them, with backward and forward references. Similar to the JSON cycle/retrocycle except:

  • We don't actually do the serialization/deserialization. You can use JSON.stringify + JSON.parse if you want, or cbor.pack + cbor.unpack, or whatever.

  • We keep ongoing state, like you might want for a network connection, so you can send along an object, then later send along a reference to it. In contrast, classic decycle and retrocycle operate on the entire structure at once.

  • While we're at it, we can also make references to strings, so if you're sending your objects over a network, the strings end up interned. This means that, as in JavaScript, you can use long strings as protocol elements (including object property-keys), knowing that after the first use, long strings are cheap.

installation

npm install --save cyclestream

taking out the cycles

(might be called deflate, toCommands, toStream, wrap)

const cyclestream = require('cyclestream')

// let's make some circular objects:
a = []
b = {}
a.push(b)
b.a = a
console.log(a)
// => [ { a: [Circular] } ]
console.log(b)
// => { a: [ [Circular] ] }

// now make a decycler
dc = cyclestream.decycler(console.log)
de(a)
// Here we see the instructions for rebuilding the structure.
// => ['define', 1, [2]]
// => ['define', 2, {a:1}]
// => 1
de(b)
// => 2

If that seems inefficient, see refstyle='cbor'

Adding the cycles back in

(might be called reconstructor, inflate, toMessages, messagesTo, toObjects, unwrap, unwrapper, unpacker)

let out
rc = cyclestream.retrocycler(x => { out = x})
rc(...)
// out === undefined
rc(...)
// out === undefined
rc(...)
// out === an array in the cycle like a

refstyle: more efficient reference identifiers

For some serializations, an alternative selection of identifiers can be more efficient. For example:


dc.refstyle = 'cbor-neg'

// Here we see the instructions for rebuilding the structure.
// Instructions and references are negative integers, because
// they pack so nicely in cbor.
//
// define item-4 as array of item-5
// => -2, -4, [-5]
// define item-5 as object with key @@@ and value of item-4
// => -2, -5, {a:-4}
// actually pass along value, item-4
// => -4
de(b)
// => -5