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

buffer-more-ints

v1.0.0

Published

Add support for more integer widths to Buffer

Downloads

5,187,147

Readme

buffer-more-ints: Add support for more integer widths to Buffer

Build Status

Node's Buffer only supports reading and writing integers of a limited range of widths. This module provides support for more widths, so that integers from 1 to 8 bytes (64 bits) can be accessed. The support takes two forms. Firstly, as stand-alone functions similar to the integer reading/writing methods of Buffer:

$ node
> var moreints = require('buffer-more-ints')
undefined
> moreints.readInt64BE(new Buffer("0000deadbeef0000", "hex"), 0).toString(16)
'deadbeef0000'

Read and write functions for the regular widths (8, 16, 32) are also present in this module, for consistency.

The second form is methods patched into Buffer.prototype, installed by requiring 'buffer-more-ints/polyfill':

$ node
> require('buffer-more-ints/polyfill')
{}
> new Buffer("0000deadbeef0000", "hex").readInt64BE(0).toString(16)
'deadbeef0000'

buffer-more-ints/polyfill also adds methods readIntBE, writeIntBE, and their LE and UInt counterparts, which take an initial argument giving the width of the integer in bytes:

> var b = new Buffer(3);
> b.writeIntLE(3, -123456, 0);
> b.toString('hex')
'c01dfe'
> b.readIntLE(3, 0);
-123456

The functions added by buffer-more-ints are all implemented in terms of the core Buffer functions. Part way through writing the code, I discovered that node.js currently implements those in JavaScript, so this doesn't lead to performance benefits. But should node ever switch to implementing its Buffer operations natively, this module should get a speed boost.

Limitations

As JavaScript uses IEEE754 doubles for numbers, the contiguous range of integers it can represent is [-2^53 - 1, 2^53 - 1]. So only integer widths up to 6 bytes or 48 bits can be read exactly. Reads of 7 or 8 bytes (56 or 64 bits) will return the closest value that can be represented as a JavaScript number.

In certain situations it might be important to check that a JavaScript number was read exactly. The isContiguousInt or Buffer.isContiguousInt (polyfill) function will determine this:

> Buffer.isContiguousInt(0x1fffffffffffff);
true
> Buffer.isContiguousInt(0x20000000000000);
false

And assertContiguousInt asserts that a number is so:

> Buffer.assertContiguousInt(0x1fffffffffffff);
undefined
> Buffer.assertContiguousInt(0x20000000000000);
AssertionError: number cannot be represented as a contiguous integer