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

sffc-encoder

v0.1.12

Published

Encode/decode numbers to and from SFFC encoding format

Downloads

37

Readme

sffc-encoder

Build Status Coverage Status npm version npm version

js-standard-style

sffc-encoder provides the encode/decode functions to and from SFFC (Significant Figures First Code) format.

SFFC format is an Integer to binary encoding format with a changing byte size (full bytes) based on the number of significant digits in base 10 that are needed to represent the Integer. This format benefits the more "rounded" (Higher powers of 10) Integer with a lower byte size encoding.

The decoding Scheme:

  • First 2 or 3 bits are length flag:
  1. If first 2 bits are 1 then the flag is 2 bits.
  2. Else the flag is the first 3 bits.
  • Remove the flag bits and concat the rest with the next extra amount of bytes using the conversion table (appendix A).
  • Split the bits to mantis and exponent to their respective bits using table 1.
  • Multiply the mantis by 10^exponent in base 10 to get the encoded Integer.

There is also some encoding optimization, for example, even though 23 has 2 significant figures we can still encode it using 3 flag bits and 5 binary bits in only 1 byte.

Installation

$ npm install sffc-encoder

Encode

Params:

  • number - takes any integer between 0 to 9007199254740991 (Number.MAX_SAFE_INTEGER)

Returns a new Buffer holding the encoded Integer

var sffc = require('sffc-encoder')
console.log(sffc.encode(1321321321)) // Will print: <Buffer 82 76 0e 1b 48>
console.log(sffc.encode(1)) // Will print: <Buffer 01>

Decode

Params:

  • consume - takes a consumable buffer (You can use buffer-consumer like in the example to create one)

Returns the Integer as number

var sffc = require('sffc-encoder')
var consumer = require('buffer-consumer')

var codeBuffer = new Buffer([0x82,0x76,0x0e,0x1b,0x48])

console.log(sffc.decode(consumer(codeBuffer))) // Will print: 1321321321

Testing

In order to test you need to install mocha globaly on your machine

$ cd /"module-path"/sffc-encoder
$ mocha

Apendix A


|Byte Flag | Significant Digits | Mantis bit size | Exponent bit size | Extra Bytes | Total Bytes| |----------|---------------------------------------------|-----------------|-------------------|-------------|------------| |000 | 1-31 (Number Value, not significant digits) | 5 | 0 | 0 | 1 | |001 | 2 | 9 | 4 | 1 | 2 | |010 | 5 | 17 | 4 | 2 | 3 | |011 | 7 | 25 | 4 | 3 | 4 | |100 | 10 | 34 | 3 | 4 | 5 | |101 | 12 | 42 | 3 | 5 | 6 | |11* | 16 | 54 | 0 | 6 | 7 |

License

Apache-2.0