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

@jamesbontempo/bsonx

v0.2.0

Published

Extension of BSON for (de)serializing a wider range of types

Downloads

4

Readme

bsonx

bsonx is a serialization library, inspired by bson, that handles a wider range of native JavaScript object types, including BigInts, Sets, and Maps---even undefined and Errors. In addition, bsonx does not require input to be a JSON object: it can just as easily serialize solitary items, like primitive values and Arrays. And it can be used to create deep clones, too.

The "x" equals "extra"!

Examples

Import bsonx

import { BSONX } from "@jamesbontempo/bsonx";

... or ...

const { BSONX } = require("@jamesbontempo/bsonx");

Serialize a standard JSON object

const serialized = BSONX.serialize({ id: 1, string: "test", number: 123, array: [3, 4, 5] });
// <Buffer 12 04 00 00 00 17 02 00 00 00 69 64 11 01 00 00 00 31 17 ... >
const deserialized = BSONX.deserialize(serialized);
// { id: 1, string: 'test', number: 123, array: [ 3, 4, 5 ] }

Serialize a JSON object that contains a BigInt

const serialized = BSONX.serialize({ bigint: 99999999999999999999999n });
// <Buffer 12 01 00 00 00 17 06 00 00 00 62 69 67 69 6e 74 02 17 00 ... >
const deserialized = BSONX.deserialize(serialized);
// { bigint: 99999999999999999999999n }

Serialize a solitary Map

const serialized = BSONX.serialize(new Map([[1, ["one", "uno"]], [2, ["two", "dos"]]]));
// <Buffer 0f 02 00 00 00 11 01 00 00 00 31 01 02 00 00 00 17 03 00 ... >
const deserialized = BSONX.deserialize(serialized);
// Map(2) { 1 => [ 'one', 'uno' ], 2 => [ 'two', 'dos' ] }

Create a deep clone of a Map

const map = new Map([[1, ["one", "uno"]], [2, ["two", "dos"]]]);
const clone = BSONX.clone(map);
// Map(2) { 1 => [ 'one', 'uno' ], 2 => [ 'two', 'dos' ] }

Methods

serialize

serialize(item)

Serializes an item.

Parameters:

Name|Type|Description ----|----|----------- item|any|Any item of an allowable type (see Allowable types)

Returns a buffer containing the serialized version of the item.

deserialize

deserialize(buffer)

Deserializes an item previously serialized with serialize.

Parameters:

Name|Type|Description ----|----|----------- buffer|buffer|The buffer produced by a previous call to serialize

Returns the original item as a native JavaScript object.

clone

clone(item)

Craetes a deep clone of an item.

Parameters:

Name|Type|Description ----|----|----------- item|any|Any item of an allowable type (see Allowable types)

Returns a deep clone of the item.

Allowable types

bsonx can serialize the following types:

  • Array
  • BigInt
  • BigInt64Array
  • BigUInt64Array
  • Boolean
  • Date
  • Error
  • EvalError
  • Float32Array
  • Float64Array
  • Function
  • Int8Array
  • Int16Array
  • Int32Array
  • Map
  • null
  • Number
  • Object
  • RangeRrror
  • ReferenceError
  • RegExp
  • Set
  • Sring
  • Symbol
  • SyntaxError
  • TypeError
  • UInt8Array
  • UInt16Array
  • UInt32Array
  • UInt8ClampedArray
  • undefined
  • URIError

Drop-in bson replacement

bsonx does not implment the entire bson interface, only the serialize and deserialize methods. However, if that's all you're using bson for, you can use bsonx as a drop-in replacement and start serializing a wider range of objects:

import { BSONX as BSON } from "@jamesbontempo/bsonx";

... or ...

const { BSONX: BSON } = require("@jamesbontempo/bsonx");

Change log

0.2.0

  • Added clone method