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

arrow-code

v0.3.0

Published

Arrow Code

Downloads

61

Readme

Arrow Code

Arrow Code use unicode combining character "͢ " in string to store types that normally not allowed in JSON

examples

// import Arrow from 'arrow-code';
const Arrow = require('arrow-code').default;

// encode as JSON
Arrow.encodeJSON( [
  NaN,
  -Infinity, 
  9007199254740997n,
  new Date(),
  new Uint8Array([1,2,3,4]), 
  undefined
], 1);
// returns:
[
 "͢NaN",
 "͢-Inf",
 "͢n:9007199254740997",  // BigInt
 "͢Date:2018-02-07T19:07:18.207Z",  // Date
 "͢Bin:wFg{A",  // Uint8Array
 "͢"  // undefined
]

Advantage

(Arrow.encodeJSON vs MsgPack and BSON)

Arrow Code allows additional data types to be serialized in JSON, such as binary date (Uint8Array) and Date, while still keeps the verbose nature of JSON. The output string is still a 100% valid JSON, and compatible with any JSON editing/parsing tool or library. This makes Arrow Code much easier to debug and trouble shoot than binary formats like BSON and MsgPack

Performance

Modern browsers and nodejs are highly optimized for JSON. This allows Arrow Code to be encoded and decoded faster than the other 2 formats in most of the cases.

benchmark result

Benchmark with sample data on Chrome 77, Firefox 69

Time are all in ms, smaller is better

||ChromeEncode|ChromeDecode|FirefoxEncode|FirefoxDecode| |:----:|:----:|:----:|:----:|:----:| |Arrow Code|0.1434|0.1742|0.1708|0.1301| |MsgPack|0.2893|0.1818|0.6689|0.1933| |BSON|0.1573|0.1879|0.3945|0.5648|

Constructor

new Arrow({
  // whether to encode Binary ( Uint8Array ), default true, which uses Base93 encoding
  encodeBinary?: boolean | 'base64',

  // whether to encode Date, default true
  encodeDate?: boolean,

  // whether to encode BigInt, default true
  encodeBigInt?: boolean,
})

API

| API | comments | |:--------------------------------------------------------------------------------|:--------------------------------------------| | encodeJSON( inpt: unknown, space?: number, sortKeys = false) | encode as JSON, can be used as static api | | decodeJSON( inpt: string) | decode JSON, can be used as static api | | encode( inpt: unknown) | encode to String, can be used as static api | | decode( inpt: string) | decode String, can be used as static api | | register( key: string, type: Constructor, encoder, decoder) | register a custom type |

Custom Types

Arrow Code's register API make it really easy to add custom type

// custom type
class MyClass {
  constructor(str) {
    this.myStr = str;
  }
}

let arrow = new Arrow();
arrow.register(
  'My', // prefix
  MyClass,  // type
  (obj) => obj.myStr, // custom encoder
  (str) => new MyClass(str) // custom decoder
);

myJson.encodeJSON(new MyClass("hello"));
// "͢My:hello"

Base93 encoding

Arrow Code use Base93 by default to encode binary data, it's more compact than Base64.

If you prefer Base64, set binaryFormat to base64 in the Arrow Code constructor

const Arrow = require('arrow-code').default;

// use Base64 instead of Base93
const arrow = new Arrow({binaryFormat: 'base64'});

arrow.encodeJSON({binary: new Uint8Array([1,2,3,4])});
// {"binary":"͢B64:AQIDBA=="}