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

@bemoje/serializer

v1.0.0

Published

Buffer, string + circular references in a single serialization module that basically just merges three different solutions: v8's Buffer serializer, the native JSON * stringifier and the drop-in replacer for JSON.stringify: 'safe-stable-stringify', which s

Downloads

12

Readme

@bemoje/serializer

Buffer, string + circular references in a single serialization module that basically just merges three different solutions: v8's Buffer serializer, the native JSON stringifier and the drop-in replacer for JSON.stringify: 'safe-stable-stringify', which supplies the support for circular references.

install

npm install --save @bemoje/serializer

API

export default { toBufferV8, toJson, toJsonPretty, deserialize }

/**
 * Serializes with v8 engine.
 * If that fails for any reason, JSON.stringify will be used, after which that resulting stringwill be
 * converted to a Buffer as to not change types.
 * @param {*} data - What to serialize
 * @returns {Buffer}
 */
export function toBufferV8(data: any): Buffer

/**
 * Serializes with JSON.stringify.
 * If that fails for any reason, 'safe-stable-stringify', as it supports circular references. If that
 * fails, the v8 engine is tried. It's returned Buffer will be converted to a string before it is
 * returned as to not change types, suddenly.
 * @param {*} data - What to serialize
 * @param {function} [replacer] - See the JSON.stringify() documentation
 * @param {integer} [space=0] - See the JSON.stringify() documentation
 * @returns {string}
 */
export function toJson(data: any, replacer?: Function, space?: Number): String

/**
 * Serializes with safe-stable-stringify, since that supports circular references. That circular
 * references error from
 * JSON is not pretty :)
 * @param {*} data - What to serialize
 * @param {function} [replacer] - See the JSON.stringify() documentation
 * @param {integer} [space=3] - See the JSON.stringify() documentation
 * @returns {string}
 */
export function toJsonPretty(data: any, replacer?: Function, space?: Number = 3): String

/**
 * Deserializes with v8 engine if the input is a Buffer and with JSON.parse if it's a string.
 * @param {Buffer|string} serialized - What to deserialize
 * @returns {*}
 */
export function deserialize(serialized: Buffer|String, reviver?: Function): any

usage

const data = { a: 3, b: { a: 32 } }

const _toBufferV8 = toBufferV8(data)
const _toJson = toJson(data)
const _toJsonPretty = toJsonPretty(data)

const __toBufferV8 = deserialize(_toBufferV8)
const __toJson = deserialize(_toJson)
const __toJsonPretty = deserialize(_toJsonPretty)

console.log({
	data,
	_toBufferV8,
	_toJson,
	_toJsonPretty,
	__toBufferV8,
	__toJson,
	__toJsonPretty,
})

/*{
  data: { a: 3, b: { a: 32 } },
  _toBufferV8: <Buffer ff 0d 6f 22 01 61 49 06 22 01 62 6f 22 01 61 49 40 7b 01 7b 02>,
  _toJson: '{"a":3,"b":{"a":32}}',
  _toJsonPretty: '{\n   "a": 3,\n   "b": {\n      "a": 32\n   }\n}',
  __toBufferV8: { a: 3, b: { a: 32 } },
  __toJson: { a: 3, b: { a: 32 } },
  __toJsonPretty: { a: 3, b: { a: 32 } }
}*/

const circular = { a: 3, b: { a: 32 } }
circular.b.b = circular.b

const c_toBufferV8 = toBufferV8(circular)
const c_toJson = toJson(circular)
const c_toJsonPretty = toJsonPretty(circular)

const c__toBufferV8 = deserialize(c_toBufferV8)
const c__toJson = deserialize(c_toJson)
const c__toJsonPretty = deserialize(c_toJsonPretty)

console.log({
	circular,
	c_toBufferV8,
	c_toJson,
	c_toJsonPretty,
	c__toBufferV8,
	c__toJson,
	c__toJsonPretty,
})

/*{
  circular: { a: 3, b: { a: 32, b: [Circular] } },
  c_toBufferV8: <Buffer ff 0d 6f 22 01 61 49 06 22 01 62 6f 22 01 61 49 40 22 01 62 5e 01 7b 02 7b 02>,
  c_toJson: '{"a":3,"b":{"a":32,"b":"[Circular]"}}',
  c_toJsonPretty: '{\n   "a": 3,\n   "b": {\n      "a": 32,\n      "b": "[Circular]"\n   }\n}',
  c__toBufferV8: { a: 3, b: { a: 32, b: [Circular] } },
  c__toJson: { a: 3, b: { a: 32, b: '[Circular]' } },
  c__toJsonPretty: { a: 3, b: { a: 32, b: '[Circular]' } }
}*/